diff --git a/Components/Pages/Movie.razor b/Components/Pages/Movie.razor deleted file mode 100644 index a415d3b..0000000 --- a/Components/Pages/Movie.razor +++ /dev/null @@ -1,11 +0,0 @@ -@page "/movie/{id}" - -Movie Details - -

TODO

-

Movie ID: @id

- -@code { - [Parameter] - public string id { get; set; } -} diff --git a/Components/Pages/MovieDetail.razor b/Components/Pages/MovieDetail.razor new file mode 100644 index 0000000..86afece --- /dev/null +++ b/Components/Pages/MovieDetail.razor @@ -0,0 +1,76 @@ +@page "/movie/{id}" + +Movie Details + + + +
+ @if (movie is not null) + { +

@movie.Title

+
+ @movie.Title Poster +
+

@movie.Runtime

+

@movie.Plot

+ @if(ManageFavorite.IsFavorite(id)) + { + + } + else + { + + } +
+ } + else + { +

Loading movie details...

+ } +
+ + +@inject OmdbService OmdbService +@inject ManageFavorite ManageFavorite + +@code { + [Parameter] + public required string id { get; set; } + private Movie? movie; + + protected override async Task OnInitializedAsync() + { + movie = await OmdbService.FetchMovieDetail(id); + } + + protected override async Task OnAfterRenderAsync(bool firstRender) + { + if (firstRender) + { + await ManageFavorite.LoadFavorites(); + StateHasChanged(); + } + } + + private async Task ToggleFavoriteMovie() + { + await ManageFavorite.ToggleFavoriteMovie(id); + } +} diff --git a/pdfs/Progetto Test 2026.pdf b/Progetto Test 2026.pdf similarity index 100% rename from pdfs/Progetto Test 2026.pdf rename to Progetto Test 2026.pdf diff --git a/Program.cs b/Program.cs index e4ac6c2..27eb826 100644 --- a/Program.cs +++ b/Program.cs @@ -1,13 +1,16 @@ using test_alebro.Components; +using Blazored.LocalStorage; var builder = WebApplication.CreateBuilder(args); // Add services to the container. builder.Services.AddRazorComponents() .AddInteractiveServerComponents(); +builder.Services.AddBlazoredLocalStorage(); // Mine services builder.Services.AddSingleton(); +builder.Services.AddScoped(); var app = builder.Build(); diff --git a/Services/ManageFavorite.cs b/Services/ManageFavorite.cs new file mode 100644 index 0000000..e3a7e4a --- /dev/null +++ b/Services/ManageFavorite.cs @@ -0,0 +1,63 @@ + + +/// +/// Service to manage favorite movies using local storage +/// +/// The local storage service +class ManageFavorite(Blazored.LocalStorage.ILocalStorageService localStorage) +{ + // Inject the local storage service + private readonly Blazored.LocalStorage.ILocalStorageService LocalStorage = localStorage; + private readonly List MoviesIDs = []; + + /// + /// Load favorite movies from local storage + /// Use Immediately after service instantiation otherwise the list will be empty + /// + /// + public async Task LoadFavorites() + { + var favorites = await LocalStorage.GetItemAsync>("favorite") ?? []; + MoviesIDs.Clear(); + MoviesIDs.AddRange(favorites); + } + + /// + /// Check if a movie is favorite or not + /// + /// The movie ID to check + /// True if the movie is favorite, otherwise false + public bool IsFavorite(string movieID) + { + return MoviesIDs.Contains(movieID); + } + + /// + /// Toggle favorite status of a movie (by ID) + /// + /// The movie ID to toggle + /// The new favorite status + public async Task ToggleFavoriteMovie(string movieID) + { + if (!IsFavorite(movieID)) + { + MoviesIDs.Add(movieID); + } + else + { + MoviesIDs.RemoveAll(id => id == movieID); + } + + await LocalStorage.SetItemAsync("favorite", MoviesIDs); + return IsFavorite(movieID); + } + + /// + /// Get the list of favorite movie IDs + /// + /// The list of favorite movie IDs + public List GetFavoriteMovies() + { + return MoviesIDs; + } +} \ No newline at end of file diff --git a/test-alebro.csproj b/test-alebro.csproj index 9ad1317..131067e 100644 --- a/test-alebro.csproj +++ b/test-alebro.csproj @@ -9,4 +9,8 @@ true + + + +