4 Commits

Author SHA1 Message Date
65685afa8c removed unnecessary css 2026-01-20 15:23:30 +01:00
1ce2ad400c fix 2026-01-20 15:21:24 +01:00
f19c1a7216 refactor table for reuse 2026-01-20 15:12:37 +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
11 changed files with 182 additions and 56 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

@@ -1,11 +0,0 @@
@page "/movie/{id}"
<PageTitle>Movie Details</PageTitle>
<h1>TODO</h1>
<p>Movie ID: @id</p>
@code {
[Parameter]
public string id { get; set; }
}

View File

@@ -0,0 +1,58 @@
@page "/movie/{id}"
<PageTitle>Movie Details</PageTitle>
<div class="row g-4" style="max-width: 800px;">
@if (movie is not null)
{
<h1>@movie.Title</h1>
<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 btn-warning" @onclick="ToggleFavoriteMovie">Remove Favorite</button>
}
else
{
<button class="btn btn-secondary" @onclick="ToggleFavoriteMovie">Add to Favorites</button>
}
</div>
}
else
{
<p>Loading movie details...</p>
}
</div>
@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(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

@@ -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<OmdbService>();
builder.Services.AddScoped<ManageFavorite>();
var app = builder.Build();

View File

@@ -0,0 +1,63 @@
/// <summary>
/// Service to manage favorite movies using local storage
/// </summary>
/// <param name="localStorage">The local storage service</param>
class ManageFavorite(Blazored.LocalStorage.ILocalStorageService localStorage)
{
// Inject the local storage service
private readonly Blazored.LocalStorage.ILocalStorageService LocalStorage = localStorage;
private readonly List<Movie> Movies = [];
/// <summary>
/// Load favorite movies from local storage
/// Use Immediately after service instantiation otherwise the list will be empty
/// </summary>
/// <returns></returns>
public async Task LoadFavorites()
{
var favorites = await LocalStorage.GetItemAsync<List<Movie>>("favorite") ?? [];
Movies.Clear();
Movies.AddRange(favorites);
}
/// <summary>
/// Check if a movie is favorite or not
/// </summary>
/// <param name="movieID">The movie ID to check</param>
/// <returns>True if the movie is favorite, otherwise false</returns>
public bool IsFavorite(string movieID)
{
return Movies.Any(m => m.IdIMDB == movieID);
}
/// <summary>
/// Toggle favorite status of a movie (by ID)
/// </summary>
/// <param name="movie">The movie ID to toggle</param>
/// <returns>The new favorite status</returns>
public async Task<bool> ToggleFavoriteMovie(Movie movie)
{
if (!IsFavorite(movie.IdIMDB))
{
Movies.Add(movie);
}
else
{
Movies.RemoveAll(m => m.IdIMDB == movie.IdIMDB);
}
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<Movie> GetFavoriteMovies()
{
return Movies;
}
}

View File

@@ -9,4 +9,8 @@
<BlazorDisableThrowNavigationException>true</BlazorDisableThrowNavigationException>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Blazored.LocalStorage" Version="4.3.0" />
</ItemGroup>
</Project>