Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 1b2ec9a1eb | |||
| ae2c3c6855 | |||
| 54814cbcce | |||
| 34aef8b8bc |
3
.gitignore
vendored
3
.gitignore
vendored
@@ -396,3 +396,6 @@ FodyWeavers.xsd
|
||||
# JetBrains Rider
|
||||
*.sln.iml
|
||||
|
||||
# =======
|
||||
# MINE
|
||||
APIKey.txt
|
||||
|
||||
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,6 +2,25 @@
|
||||
|
||||
<PageTitle>Favorites</PageTitle>
|
||||
|
||||
<h1>TODO</h1>
|
||||
<MovieTable Movies="movies" />
|
||||
|
||||
@inject ManageFavorite ManageFavorite
|
||||
|
||||
@code {
|
||||
public MovieSearch? movies = null;
|
||||
|
||||
protected override async Task OnAfterRenderAsync(bool firstRender)
|
||||
{
|
||||
if (firstRender)
|
||||
{
|
||||
await ManageFavorite.LoadFavorites();
|
||||
var favoriteMovies = ManageFavorite.GetFavoriteMovies();
|
||||
movies = new MovieSearch
|
||||
{
|
||||
Search = favoriteMovies.ToArray(),
|
||||
TotalResults = favoriteMovies.Count.ToString()
|
||||
};
|
||||
StateHasChanged();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,26 @@
|
||||
|
||||
<PageTitle>Home</PageTitle>
|
||||
|
||||
<h1>Hello, world!</h1>
|
||||
<form class="mb-3" @onsubmit="OnSearch">
|
||||
<input type="text" class="form-control" placeholder="Search for movies..."
|
||||
style="width: 40%; margin-bottom: 10px;"
|
||||
@bind="searchTitle" @bind:event="oninput" />
|
||||
<div class="d-flex align-items-center gap-2">
|
||||
<button class="btn btn-primary" type="submit">Search</button>
|
||||
<p class="mb-0">@((movies == null) ? "" : movies.TotalResults + " results found")</p>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
Welcome to your new app.
|
||||
<MovieTable Movies="movies" />
|
||||
|
||||
@inject OmdbService OmdbService
|
||||
|
||||
@code {
|
||||
private string searchTitle = "";
|
||||
private MovieSearch? movies = null;
|
||||
|
||||
private async Task OnSearch()
|
||||
{
|
||||
movies = await OmdbService.FetchMovies(searchTitle);
|
||||
}
|
||||
}
|
||||
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,4 +1,5 @@
|
||||
<Router AppAssembly="typeof(Program).Assembly" NotFoundPage="typeof(Pages.NotFound)">
|
||||
@rendermode InteractiveServer
|
||||
<Router AppAssembly="typeof(Program).Assembly" NotFoundPage="typeof(Pages.NotFound)">
|
||||
<Found Context="routeData">
|
||||
<RouteView RouteData="routeData" DefaultLayout="typeof(Layout.MainLayout)" />
|
||||
<FocusOnNavigate RouteData="routeData" Selector="h1" />
|
||||
|
||||
17
Models/Movie.cs
Normal file
17
Models/Movie.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
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>
|
||||
public class Movie
|
||||
{
|
||||
public required string Title { get; set; }
|
||||
public required string Year { get; set; }
|
||||
[JsonPropertyName("imdbID")]
|
||||
public required string IdIMDB { get; set; }
|
||||
public required string Poster { get; set; }
|
||||
public string Runtime { get; set; } = "N/A";
|
||||
public string Plot { get; set; } = "N/A";
|
||||
|
||||
}
|
||||
12
Models/MovieSearch.cs
Normal file
12
Models/MovieSearch.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
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>
|
||||
public class MovieSearch
|
||||
{
|
||||
public required Movie[] Search { get; set; }
|
||||
[JsonPropertyName("totalResults")]
|
||||
public required string TotalResults { get; set; }
|
||||
}
|
||||
@@ -1,10 +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();
|
||||
|
||||
|
||||
11
README.md
11
README.md
@@ -1,3 +1,12 @@
|
||||
# test-alebro
|
||||
|
||||
Test per colloquio
|
||||
Test per colloquio
|
||||
|
||||
## Istruzioni per l'esecuzione
|
||||
|
||||
Dopo aver clonato il repository, crea un file APIKey.txt nella cartella radice del progetto e inserisci l'API Key di OMDb.
|
||||
|
||||
Poi esegui il progetto con:
|
||||
```bash
|
||||
dotnet run
|
||||
```
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
61
Services/OmdbService.cs
Normal file
61
Services/OmdbService.cs
Normal file
@@ -0,0 +1,61 @@
|
||||
using System.Net;
|
||||
using System.Text.Json;
|
||||
|
||||
/// <summary>
|
||||
/// Servizio per interagire con l'API OMDB.
|
||||
/// Semplifica le richieste per ottenere dettagli sui film.
|
||||
/// </summary>
|
||||
class OmdbService
|
||||
{
|
||||
private readonly string url;
|
||||
private readonly HttpClient httpClient = new();
|
||||
|
||||
public OmdbService()
|
||||
{
|
||||
var apiKey = File.ReadAllText("APIKey.txt").Trim();
|
||||
apiKey = WebUtility.UrlEncode(apiKey);
|
||||
|
||||
url = "http://www.omdbapi.com/?apikey=" + apiKey
|
||||
+ "&type=movie"
|
||||
+ "&r=json";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Recupera i dettagli di un film dato il suo ID IMDB.
|
||||
/// </summary>
|
||||
/// <param name="id">ID IMDB del film da cercare.</param>
|
||||
/// <returns>Un oggetto Movie con i dettagli del film.</returns>
|
||||
public async Task<Movie?> FetchMovieDetail(string id)
|
||||
{
|
||||
return await FetchAsync<Movie>("i", id);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Recupera una lista di film che corrispondono al titolo di ricerca.
|
||||
/// </summary>
|
||||
/// <param name="searchTitle">Titolo dei film da cercare.</param>
|
||||
/// <returns>>Una lista di oggetti Movie che corrispondono alla ricerca.</returns>
|
||||
public async Task<MovieSearch?> FetchMovies(string searchTitle)
|
||||
{
|
||||
return await FetchAsync<MovieSearch>("s", searchTitle);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Esegue una richiesta HTTP GET e deserializza la risposta JSON in un oggetto del tipo specificato.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Tipo dell'oggetto di ritorno.</typeparam>
|
||||
/// <param name="query">Parametro della query HTTP.</param>
|
||||
/// <param name="value">Valore del parametro della query HTTP.</param>
|
||||
/// <returns>Un oggetto del tipo specificato deserializzato dalla risposta JSON.</returns>
|
||||
private async Task<T?> FetchAsync<T>(string query, string value)
|
||||
{
|
||||
var requestUrl = url + "&" + query + "=" + WebUtility.UrlEncode(value);
|
||||
var response = await httpClient.GetStringAsync(requestUrl);
|
||||
try {
|
||||
return JsonSerializer.Deserialize<T>(response)!;
|
||||
} catch (JsonException) {
|
||||
return default;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -9,4 +9,8 @@
|
||||
<BlazorDisableThrowNavigationException>true</BlazorDisableThrowNavigationException>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Blazored.LocalStorage" Version="4.3.0" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
24
test-alebro.sln
Normal file
24
test-alebro.sln
Normal file
@@ -0,0 +1,24 @@
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio Version 17
|
||||
VisualStudioVersion = 17.5.2.0
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "test-alebro", "test-alebro.csproj", "{80C175CB-7DB0-93BB-681F-01F9DB017FB2}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
Release|Any CPU = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{80C175CB-7DB0-93BB-681F-01F9DB017FB2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{80C175CB-7DB0-93BB-681F-01F9DB017FB2}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{80C175CB-7DB0-93BB-681F-01F9DB017FB2}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{80C175CB-7DB0-93BB-681F-01F9DB017FB2}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {4E9E6522-3307-4F89-B6D6-2D4D61E7B65B}
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
Reference in New Issue
Block a user