4 Commits

Author SHA1 Message Date
1ab403b3b3 Home finished 2026-01-20 12:08:36 +01:00
2751b4181d models Movie, MovieSearch 2026-01-19 20:22:55 +01:00
5a561f9fff Implement movie fetching 2026-01-19 18:46:55 +01:00
1cce273057 omdb init 2026-01-19 17:45:01 +01:00
11 changed files with 210 additions and 4 deletions

3
.gitignore vendored
View File

@@ -396,3 +396,6 @@ FodyWeavers.xsd
# JetBrains Rider
*.sln.iml
# =======
# MINE
APIKey.txt

View File

@@ -4,4 +4,15 @@
<h1>TODO</h1>
<button class="btn btn-primary" @onclick="FaiCose">FaiCose</button>
@inject OmdbService OmdbService
@inject IJSRuntime JSRuntime
@code {
private async Task FaiCose()
{
var movies = await OmdbService.FetchMovies("Matrix");
var movieDetail = await OmdbService.FetchMovieDetail("tt11749868");
}
}

View File

@@ -2,6 +2,54 @@
<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.
<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 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

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

View File

@@ -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" />

20
Models/Movie.cs Normal file
View File

@@ -0,0 +1,20 @@
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 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";
}

15
Models/MovieSearch.cs Normal file
View File

@@ -0,0 +1,15 @@
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 required Movie[] Search { get; set; }
[JsonPropertyName("totalResults")]
public required string TotalResults { get; set; }
}

View File

@@ -6,6 +6,9 @@ var builder = WebApplication.CreateBuilder(args);
builder.Services.AddRazorComponents()
.AddInteractiveServerComponents();
// Mine services
builder.Services.AddSingleton<OmdbService>();
var app = builder.Build();
// Configure the HTTP request pipeline.

View File

@@ -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
```

61
Services/OmdbService.cs Normal file
View 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;
}
}
}

24
test-alebro.sln Normal file
View 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