Compare commits
2 Commits
movie-deta
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 1b2ec9a1eb | |||
| ae2c3c6855 |
35
Components/Layout/MovieTable.razor
Normal file
35
Components/Layout/MovieTable.razor
Normal 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}");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,17 +2,25 @@
|
|||||||
|
|
||||||
<PageTitle>Favorites</PageTitle>
|
<PageTitle>Favorites</PageTitle>
|
||||||
|
|
||||||
<h1>TODO</h1>
|
<MovieTable Movies="movies" />
|
||||||
|
|
||||||
<button class="btn btn-primary" @onclick="FaiCose">FaiCose</button>
|
@inject ManageFavorite ManageFavorite
|
||||||
|
|
||||||
@inject OmdbService OmdbService
|
|
||||||
@inject IJSRuntime JSRuntime
|
|
||||||
|
|
||||||
@code {
|
@code {
|
||||||
private async Task FaiCose()
|
public MovieSearch? movies = null;
|
||||||
|
|
||||||
|
protected override async Task OnAfterRenderAsync(bool firstRender)
|
||||||
{
|
{
|
||||||
var movies = await OmdbService.FetchMovies("Matrix");
|
if (firstRender)
|
||||||
var movieDetail = await OmdbService.FetchMovieDetail("tt11749868");
|
{
|
||||||
|
await ManageFavorite.LoadFavorites();
|
||||||
|
var favoriteMovies = ManageFavorite.GetFavoriteMovies();
|
||||||
|
movies = new MovieSearch
|
||||||
|
{
|
||||||
|
Search = favoriteMovies.ToArray(),
|
||||||
|
TotalResults = favoriteMovies.Count.ToString()
|
||||||
|
};
|
||||||
|
StateHasChanged();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,42 +12,14 @@
|
|||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
|
|
||||||
<table class="table table-striped" id="moviesTable">
|
<MovieTable Movies="movies" />
|
||||||
<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 OmdbService OmdbService
|
@inject OmdbService OmdbService
|
||||||
@inject NavigationManager NavigationManager
|
|
||||||
|
|
||||||
@code {
|
@code {
|
||||||
private string searchTitle = "";
|
private string searchTitle = "";
|
||||||
private MovieSearch? movies = null;
|
private MovieSearch? movies = null;
|
||||||
|
|
||||||
private async Task OnMovieSelected(string imdbID)
|
|
||||||
{
|
|
||||||
NavigationManager.NavigateTo($"/movie/{imdbID}");
|
|
||||||
}
|
|
||||||
|
|
||||||
private async Task OnSearch()
|
private async Task OnSearch()
|
||||||
{
|
{
|
||||||
movies = await OmdbService.FetchMovies(searchTitle);
|
movies = await OmdbService.FetchMovies(searchTitle);
|
||||||
|
|||||||
@@ -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; }
|
|
||||||
}
|
|
||||||
58
Components/Pages/MovieDetail.razor
Normal file
58
Components/Pages/MovieDetail.razor
Normal 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!);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,13 +1,10 @@
|
|||||||
|
|
||||||
|
|
||||||
using System.Text.Json.Serialization;
|
using System.Text.Json.Serialization;
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Rappresenta un film con le sue proprietà principali.
|
/// Rappresenta un film con le sue proprietà principali.
|
||||||
/// Non ho preso tutte le proprietà disponibili dall'API OMDB, solo quelle richieste.
|
/// Non ho preso tutte le proprietà disponibili dall'API OMDB, solo quelle richieste.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
class Movie
|
public class Movie
|
||||||
{
|
{
|
||||||
public required string Title { get; set; }
|
public required string Title { get; set; }
|
||||||
public required string Year { get; set; }
|
public required string Year { get; set; }
|
||||||
|
|||||||
@@ -1,13 +1,10 @@
|
|||||||
|
|
||||||
|
|
||||||
using System.Text.Json.Serialization;
|
using System.Text.Json.Serialization;
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Modello per rappresentare i risultati di una ricerca di film.
|
/// Modello per rappresentare i risultati di una ricerca di film.
|
||||||
/// Ha solo scopo di deserializzazione della risposta JSON dell'API OMDB.
|
/// Ha solo scopo di deserializzazione della risposta JSON dell'API OMDB.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
class MovieSearch
|
public class MovieSearch
|
||||||
{
|
{
|
||||||
public required Movie[] Search { get; set; }
|
public required Movie[] Search { get; set; }
|
||||||
[JsonPropertyName("totalResults")]
|
[JsonPropertyName("totalResults")]
|
||||||
|
|||||||
@@ -1,13 +1,16 @@
|
|||||||
using test_alebro.Components;
|
using test_alebro.Components;
|
||||||
|
using Blazored.LocalStorage;
|
||||||
|
|
||||||
var builder = WebApplication.CreateBuilder(args);
|
var builder = WebApplication.CreateBuilder(args);
|
||||||
|
|
||||||
// Add services to the container.
|
// Add services to the container.
|
||||||
builder.Services.AddRazorComponents()
|
builder.Services.AddRazorComponents()
|
||||||
.AddInteractiveServerComponents();
|
.AddInteractiveServerComponents();
|
||||||
|
builder.Services.AddBlazoredLocalStorage();
|
||||||
|
|
||||||
// Mine services
|
// Mine services
|
||||||
builder.Services.AddSingleton<OmdbService>();
|
builder.Services.AddSingleton<OmdbService>();
|
||||||
|
builder.Services.AddScoped<ManageFavorite>();
|
||||||
|
|
||||||
var app = builder.Build();
|
var app = builder.Build();
|
||||||
|
|
||||||
|
|||||||
63
Services/ManageFavorite.cs
Normal file
63
Services/ManageFavorite.cs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -9,4 +9,8 @@
|
|||||||
<BlazorDisableThrowNavigationException>true</BlazorDisableThrowNavigationException>
|
<BlazorDisableThrowNavigationException>true</BlazorDisableThrowNavigationException>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Blazored.LocalStorage" Version="4.3.0" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
Reference in New Issue
Block a user