@page "/searchshows" @using MovieActorLookup.Models @inject ShowService ShowService @rendermode InteractiveServer

Search for Actor by TV Show and Character Name

@if (_isSearching) {

Searching...

} else if (_actor != null) {

Actor: @_actor.Name

@if (_matchedShow != null) {
Show Poster
@_matchedShow.ShowTitle
@_matchedShow.CharacterName
First Air Date: @_matchedShow.FirstAirDate
} @foreach (var group in _actor.TvCharacters .GroupBy(tc => tc.Franchise) .OrderBy(g => g.Key == "Standalone") .ThenBy(g => g.Key)) {
@foreach (var show in (group.Key == "Standalone" ? group.OrderByDescending(tc => DateTime.TryParse(tc.FirstAirDate, out var d) ? d : DateTime.MinValue) : group.OrderBy(tc => DateTime.TryParse(tc.FirstAirDate, out var d) ? d : DateTime.MaxValue))) {
Poster
@show.ShowTitle
@show.CharacterName

@show.Overview

First Air Date: @show.FirstAirDate
}
} } else {

Enter a TV show title and character name to begin your search.

} @code { private string _tvTitle; private string _characterName; private Actor _actor; private TvCharacter _matchedShow; private bool _isSearching = false; private async Task SearchForActorAndCharacters() { _isSearching = true; _actor = null; _matchedShow = null; StateHasChanged(); if (!string.IsNullOrWhiteSpace(_tvTitle) && !string.IsNullOrWhiteSpace(_characterName)) { var shows = await ShowService.GetShowsByTitle(_tvTitle); foreach (var show in shows) { var credits = await ShowService.GetShowCreditsById(show.Id); foreach (var cast in credits.Cast) { if (cast.Character.ToLower().Contains(_characterName.ToLower())) { if (_actor == null) { _actor = new Actor(cast.Name); int personId = cast.Id; var filmography = await ShowService.GetActorTvCreditsById(personId); if (filmography?.Cast != null) { foreach (var role in filmography.Cast) { var fullShow = await ShowService.GetShowDetailsById(role.Id); var tvCharacter = new TvCharacter { ShowTitle = role.ShowTitle, CharacterName = role.Character, ShowPosterPath = role.PosterPath, FirstAirDate = DateTime.TryParse(fullShow?.FirstAirDate, out var parsedDate) ? parsedDate.ToString("MM/dd/yyyy") : "N/A", Overview = fullShow?.Overview ?? "", Franchise = fullShow?.BelongsToCollection?.Name ?? "Standalone" }; _actor.TvCharacters.Add(tvCharacter); if (_matchedShow == null && role.ShowTitle == show.Name && role.Character == cast.Character) { _matchedShow = tvCharacter; } } } } } } } } _isSearching = false; StateHasChanged(); } }