61 lines
2.8 KiB
C#
61 lines
2.8 KiB
C#
using Netina.Domain.Entities.Brands;
|
|
|
|
namespace Netina.Api.Controllers;
|
|
|
|
public class BrandController : ICarterModule
|
|
{
|
|
|
|
public virtual void AddRoutes(IEndpointRouteBuilder app)
|
|
{
|
|
var group = app.NewVersionedApi("Brand")
|
|
.MapGroup($"api/brand");
|
|
|
|
group.MapGet("", GetAllAsync)
|
|
.WithDisplayName("Get Brands")
|
|
.HasApiVersion(1.0);
|
|
|
|
group.MapGet("{id}", GetAsync)
|
|
.WithDisplayName("Get Brand")
|
|
.HasApiVersion(1.0);
|
|
|
|
group.MapPost("", Post)
|
|
.HasApiVersion(1.0)
|
|
.WithDisplayName("Create Brand")
|
|
.RequireAuthorization(builder => builder.AddAuthenticationSchemes("Bearer").RequireAuthenticatedUser().RequireClaim(CustomClaimType.Permission, ApplicationPermission.ManageBrands));
|
|
|
|
group.MapPut("", Put)
|
|
.HasApiVersion(1.0)
|
|
.WithDisplayName("Update Brand")
|
|
.RequireAuthorization(builder => builder.AddAuthenticationSchemes("Bearer").RequireAuthenticatedUser().RequireClaim(CustomClaimType.Permission, ApplicationPermission.ManageBrands));
|
|
|
|
group.MapDelete("{id}", Delete)
|
|
.HasApiVersion(1.0)
|
|
.WithDisplayName("Delete Brand")
|
|
.RequireAuthorization(builder => builder.AddAuthenticationSchemes("Bearer").RequireAuthenticatedUser().RequireClaim(CustomClaimType.Permission, ApplicationPermission.ManageBrands));
|
|
}
|
|
|
|
// GET:Get All Entity
|
|
public async Task<IResult> GetAllAsync([FromQuery] int? page, [FromQuery]string? brandName, [FromQuery]Guid? categoryId, IMediator mediator, CancellationToken cancellationToken)
|
|
{
|
|
return TypedResults.Ok(await mediator.Send(new GetBrandsQuery(Page: page, BrandName: brandName, CategoryId : categoryId ?? default),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));
|
|
} |