Api/NetinaShop.Api/Controller/BrandController.cs

73 lines
2.9 KiB
C#

using NetinaShop.Domain.Entities.Brands;
namespace NetinaShop.Api.Controller;
public class BrandController : ICarterModule
{
public virtual void AddRoutes(IEndpointRouteBuilder app)
{
var group = app.NewVersionedApi("Brand")
.MapGroup($"api/brand");
group.MapGet("", GetAllAsync)
.WithDisplayName("GetAllBrands")
.HasApiVersion(1.0);
group.MapGet("{id}", GetAsync)
.WithDisplayName("GetBlogBrand")
.HasApiVersion(1.0);
group.MapPost("", Post)
.HasApiVersion(1.0)
.RequireAuthorization(builder => builder.AddAuthenticationSchemes("Bearer").RequireAuthenticatedUser());
group.MapPut("", Put)
.HasApiVersion(1.0)
.RequireAuthorization(builder => builder.AddAuthenticationSchemes("Bearer").RequireAuthenticatedUser());
group.MapDelete("{id}", Delete)
.HasApiVersion(1.0)
.RequireAuthorization(builder => builder.AddAuthenticationSchemes("Bearer").RequireAuthenticatedUser());
}
// GET:Get All Entity
public async Task<IResult> GetAllAsync([FromQuery] int? page, IRepositoryWrapper repositoryWrapper,
CancellationToken cancellationToken)
{
if (page != null)
{
return TypedResults.Ok(await repositoryWrapper.SetRepository<Brand>().TableNoTracking
.OrderByDescending(b => b.CreatedAt).Skip(page.Value * 10).Take(10)
.Select(BrandMapper.ProjectToSDto)
.ToListAsync(cancellationToken));
}
else
{
return TypedResults.Ok(await repositoryWrapper.SetRepository<Brand>().TableNoTracking
.OrderByDescending(b => b.CreatedAt)
.Select(BrandMapper.ProjectToSDto)
.ToListAsync(cancellationToken));
}
}
// GET:Get An Entity By Id
public async Task<IResult> GetAsync(Guid id, IRepositoryWrapper repositoryWrapper, CancellationToken cancellationToken)
=> TypedResults.Ok(await repositoryWrapper.SetRepository<Brand>()
.TableNoTracking.Where(b => b.Id == id)
.Select(BrandMapper.ProjectToLDto)
.FirstOrDefaultAsync(cancellationToken));
// POST:Create Entity
public async Task<IResult> Post([FromBody] CreateBrandCommand request, IMediator mediator, CancellationToken cancellationToken)
=> TypedResults.Ok(await mediator.Send(request, cancellationToken));
// PUT:Update Entity
public async Task<IResult> Put([FromBody] UpdateBrandCommand request, IMediator mediator, CancellationToken cancellationToken)
=> TypedResults.Ok(await mediator.Send(request, cancellationToken));
// DELETE:Delete Entity
public async Task<IResult> Delete(Guid id, IMediator mediator, CancellationToken cancellationToken)
=> TypedResults.Ok(await mediator.Send(new DeleteBrandCommand(id), cancellationToken));
}