2 Commits

Author SHA1 Message Date
1b2ec9a1eb Favorites (#4)
* Added favorites page
* Refactor table to a reusable component

Reviewed-on: #4
Co-authored-by: Berack96 <giacomobertolazzi7@gmail.com>
Co-committed-by: Berack96 <giacomobertolazzi7@gmail.com>
2026-01-20 15:24:26 +01:00
ae2c3c6855 Movie Detail (#3)
* Implemented Movie Detail
* Implemented basic favorites

Reviewed-on: #3
Co-authored-by: Berack96 <giacomobertolazzi7@gmail.com>
Co-committed-by: Berack96 <giacomobertolazzi7@gmail.com>
2026-01-20 14:42:31 +01:00
7 changed files with 76 additions and 85 deletions

View File

@@ -0,0 +1,35 @@
<table class="table table-striped" id="moviesTable">
<thead class="table-primary">
<tr>
<th></th>
<th style="width: 100%;">Title</th>
<th>Year</th>
</tr>
</thead>
<tbody>
@if (Movies?.Search != null)
{
@foreach (var movie in Movies.Search)
{
<tr>
<td><button class="btn btn-secondary" @onclick="() => OnMovieSelected(movie.IdIMDB)">Details</button></td>
<td>@movie.Title</td>
<td>@movie.Year</td>
</tr>
}
}
</tbody>
</table>
@inject NavigationManager NavigationManager
@code {
[Parameter]
public MovieSearch? Movies { get; set; }
private async Task OnMovieSelected(string imdbID)
{
NavigationManager.NavigateTo($"/movie/{imdbID}");
}
}

View File

@@ -2,17 +2,25 @@
<PageTitle>Favorites</PageTitle>
<h1>TODO</h1>
<MovieTable Movies="movies" />
<button class="btn btn-primary" @onclick="FaiCose">FaiCose</button>
@inject OmdbService OmdbService
@inject IJSRuntime JSRuntime
@inject ManageFavorite ManageFavorite
@code {
private async Task FaiCose()
public MovieSearch? movies = null;
protected override async Task OnAfterRenderAsync(bool firstRender)
{
var movies = await OmdbService.FetchMovies("Matrix");
var movieDetail = await OmdbService.FetchMovieDetail("tt11749868");
if (firstRender)
{
await ManageFavorite.LoadFavorites();
var favoriteMovies = ManageFavorite.GetFavoriteMovies();
movies = new MovieSearch
{
Search = favoriteMovies.ToArray(),
TotalResults = favoriteMovies.Count.ToString()
};
StateHasChanged();
}
}
}

View File

@@ -12,42 +12,14 @@
</div>
</form>
<table class="table table-striped" id="moviesTable">
<thead class="table-primary">
<tr>
<th></th>
<th style="width: 100%;">Title</th>
<th>Year</th>
</tr>
</thead>
<tbody>
@if (movies?.Search != null)
{
@foreach (var movie in movies.Search)
{
<tr>
<td><button class="btn btn-secondary" @onclick="() => OnMovieSelected(movie.IdIMDB)">Details</button></td>
<td>@movie.Title</td>
<td>@movie.Year</td>
</tr>
}
}
</tbody>
</table>
<MovieTable Movies="movies" />
@inject OmdbService OmdbService
@inject NavigationManager NavigationManager
@code {
private string searchTitle = "";
private MovieSearch? movies = null;
private async Task OnMovieSelected(string imdbID)
{
NavigationManager.NavigateTo($"/movie/{imdbID}");
}
private async Task OnSearch()
{
movies = await OmdbService.FetchMovies(searchTitle);

View File

@@ -2,41 +2,23 @@
<PageTitle>Movie Details</PageTitle>
<style>
.my-grid {
display: grid;
grid-template-columns: 1fr 2fr;
grid-template-rows: auto auto;
gap: 20px;
max-width: 800px;
margin-right: auto;
}
.favorite {
background-color: gold;
color: black;
}
.not-favorite {
background-color: gray;
color: white;
}
</style>
<div class="my-grid">
<div class="row g-4" style="max-width: 800px;">
@if (movie is not null)
{
<h1>@movie.Title</h1>
<div></div>
<img src="@movie.Poster" alt="@movie.Title Poster" />
<div class="details">
<div class="col-md-4">
<img src="@movie.Poster" alt="@movie.Title Poster" class="img-fluid" />
</div>
<div class="col-md-8">
<p>@movie.Runtime</p>
<p>@movie.Plot</p>
@if(ManageFavorite.IsFavorite(id))
{
<button class="btn-secondary favorite" @onclick="ToggleFavoriteMovie">Remove Favorite</button>
<button class="btn btn-warning" @onclick="ToggleFavoriteMovie">Remove Favorite</button>
}
else
{
<button class="btn-secondary not-favorite" @onclick="ToggleFavoriteMovie">Add to Favorites</button>
<button class="btn btn-secondary" @onclick="ToggleFavoriteMovie">Add to Favorites</button>
}
</div>
}
@@ -71,6 +53,6 @@
private async Task ToggleFavoriteMovie()
{
await ManageFavorite.ToggleFavoriteMovie(id);
await ManageFavorite.ToggleFavoriteMovie(movie!);
}
}

View File

@@ -1,13 +1,10 @@
using System.Text.Json.Serialization;
/// <summary>
/// Rappresenta un film con le sue proprietà principali.
/// Non ho preso tutte le proprietà disponibili dall'API OMDB, solo quelle richieste.
/// </summary>
class Movie
public class Movie
{
public required string Title { get; set; }
public required string Year { get; set; }

View File

@@ -1,13 +1,10 @@
using System.Text.Json.Serialization;
/// <summary>
/// Modello per rappresentare i risultati di una ricerca di film.
/// Ha solo scopo di deserializzazione della risposta JSON dell'API OMDB.
/// </summary>
class MovieSearch
public class MovieSearch
{
public required Movie[] Search { get; set; }
[JsonPropertyName("totalResults")]

View File

@@ -8,7 +8,7 @@ class ManageFavorite(Blazored.LocalStorage.ILocalStorageService localStorage)
{
// Inject the local storage service
private readonly Blazored.LocalStorage.ILocalStorageService LocalStorage = localStorage;
private readonly List<string> MoviesIDs = [];
private readonly List<Movie> Movies = [];
/// <summary>
/// Load favorite movies from local storage
@@ -17,9 +17,9 @@ class ManageFavorite(Blazored.LocalStorage.ILocalStorageService localStorage)
/// <returns></returns>
public async Task LoadFavorites()
{
var favorites = await LocalStorage.GetItemAsync<List<string>>("favorite") ?? [];
MoviesIDs.Clear();
MoviesIDs.AddRange(favorites);
var favorites = await LocalStorage.GetItemAsync<List<Movie>>("favorite") ?? [];
Movies.Clear();
Movies.AddRange(favorites);
}
/// <summary>
@@ -29,35 +29,35 @@ class ManageFavorite(Blazored.LocalStorage.ILocalStorageService localStorage)
/// <returns>True if the movie is favorite, otherwise false</returns>
public bool IsFavorite(string movieID)
{
return MoviesIDs.Contains(movieID);
return Movies.Any(m => m.IdIMDB == movieID);
}
/// <summary>
/// Toggle favorite status of a movie (by ID)
/// </summary>
/// <param name="movieID">The movie ID to toggle</param>
/// <param name="movie">The movie ID to toggle</param>
/// <returns>The new favorite status</returns>
public async Task<bool> ToggleFavoriteMovie(string movieID)
public async Task<bool> ToggleFavoriteMovie(Movie movie)
{
if (!IsFavorite(movieID))
if (!IsFavorite(movie.IdIMDB))
{
MoviesIDs.Add(movieID);
Movies.Add(movie);
}
else
{
MoviesIDs.RemoveAll(id => id == movieID);
Movies.RemoveAll(m => m.IdIMDB == movie.IdIMDB);
}
await LocalStorage.SetItemAsync("favorite", MoviesIDs);
return IsFavorite(movieID);
await LocalStorage.SetItemAsync("favorite", Movies);
return IsFavorite(movie.IdIMDB);
}
/// <summary>
/// Get the list of favorite movie IDs
/// </summary>
/// <returns>The list of favorite movie IDs</returns>
public List<string> GetFavoriteMovies()
public List<Movie> GetFavoriteMovies()
{
return MoviesIDs;
return Movies;
}
}