34 lines
1.3 KiB
C#
34 lines
1.3 KiB
C#
using PostFundManagement.Domain.Abstractions;
|
|
using PostFundManagement.Domain.Api;
|
|
using PostFundManagement.Domain.Extensions;
|
|
using PostFundManagement.Infrastructure.Database;
|
|
|
|
namespace PostFundManagement.Api.Endpoints.Projects;
|
|
|
|
[ApiVersionTarget(1)]
|
|
public class GetProjectByIdEndpoint : IEndpoint
|
|
{
|
|
public void Map(IEndpointRouteBuilder builder)
|
|
{
|
|
builder.MapGet("api/projects/{id:long}", async (long id, IDbContextFactory<ApplicationDbContext> contextFactory,
|
|
CancellationToken cancellationToken = default) =>
|
|
{
|
|
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
|
|
|
var project = await context.Projects.AsNoTracking().FirstOrDefaultAsync(p => p.Id == id, cancellationToken);
|
|
|
|
return project != null
|
|
? Results.Ok(project)
|
|
: Results.NotFound();
|
|
})
|
|
.RequireAuthorization()
|
|
.WithDescription("Get project by ID")
|
|
.WithName(typeof(GetProjectByIdEndpoint).ToEndpointName())
|
|
.MapToApiVersion(new ApiVersion(1))
|
|
.Produces<Domain.Entities.Project>(StatusCodes.Status200OK)
|
|
.Produces(StatusCodes.Status404NotFound)
|
|
.Produces(StatusCodes.Status401Unauthorized)
|
|
.WithTags(EndpointTags.Projects);
|
|
}
|
|
}
|