-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGetMovieByIdEndpoint.cs
More file actions
93 lines (84 loc) · 3.39 KB
/
Copy pathGetMovieByIdEndpoint.cs
File metadata and controls
93 lines (84 loc) · 3.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
using Microsoft.AspNetCore.Mvc;
using SmartMovieCatalog.Application;
using SmartMovieCatalog.Application.Features.Movies;
using SmartMovieCatalog.Contracts.Movies;
using Wolverine;
namespace SmartMovieCatalog.Api.Features.Movies.GetMovieById;
public static class GetMovieByIdEndpoint
{
private const string ProblemJsonContentType = "application/problem+json";
public static void MapGetMovieById(this IEndpointRouteBuilder app)
{
app.MapGet("/api/movies/{id}", HandleAsync)
.WithName("GetMovieById")
.WithTags("Movies")
.AllowAnonymous()
.Produces<MovieDetailsResponse>(StatusCodes.Status200OK)
.Produces<ProblemDetails>(StatusCodes.Status400BadRequest, ProblemJsonContentType)
.Produces<ProblemDetails>(StatusCodes.Status404NotFound, ProblemJsonContentType);
}
private static async Task<IResult> HandleAsync(
string id,
HttpContext httpContext,
IMessageBus messageBus,
CancellationToken cancellationToken)
{
if (!Guid.TryParse(id, out Guid movieId))
{
return Results.Json(
CreateInvalidMovieIdProblem(httpContext),
statusCode: StatusCodes.Status400BadRequest,
contentType: ProblemJsonContentType);
}
Result<MovieDetails, GetMovieByIdFailure> result =
await messageBus.InvokeAsync<Result<MovieDetails, GetMovieByIdFailure>>(
new GetMovieByIdQuery(movieId),
cancellationToken);
return result.Match<IResult>(
details => Results.Ok(new MovieDetailsResponse(
details.Id.ToString(),
details.Title,
details.OriginalTitle,
details.ReleaseYear,
details.CountryCode,
details.OriginalLanguage,
details.Genres,
details.Director,
details.Synopsis,
details.DurationMinutes,
details.AgeRating,
details.ExternalId,
details.PosterUrl,
details.CreatedAt)),
_ => Results.Json(
CreateMovieNotFoundProblem(httpContext, id),
statusCode: StatusCodes.Status404NotFound,
contentType: ProblemJsonContentType));
}
private static ProblemDetails CreateInvalidMovieIdProblem(HttpContext httpContext)
{
ProblemDetails problemDetails = new()
{
Status = StatusCodes.Status400BadRequest,
Title = "Invalid movie id.",
Detail = "Movie id must be a valid GUID.",
Type = "https://smartmoviecatalog/problems/invalid-movie-id",
Instance = httpContext.Request.Path
};
problemDetails.Extensions["traceId"] = httpContext.TraceIdentifier;
return problemDetails;
}
private static ProblemDetails CreateMovieNotFoundProblem(HttpContext httpContext, string id)
{
ProblemDetails problemDetails = new()
{
Status = StatusCodes.Status404NotFound,
Title = "Movie not found.",
Detail = $"Movie '{id}' was not found.",
Type = "https://smartmoviecatalog/problems/movie-not-found",
Instance = httpContext.Request.Path
};
problemDetails.Extensions["traceId"] = httpContext.TraceIdentifier;
return problemDetails;
}
}