Add CRUDs and validation/controllers
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
using Imprink.Application.Domains.Categories;
|
||||
using Imprink.Application.Products.Dtos;
|
||||
using Imprink.Application.Products.Query;
|
||||
using MediatR;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace Imprink.WebApi.Controllers;
|
||||
@@ -10,8 +11,38 @@ namespace Imprink.WebApi.Controllers;
|
||||
public class CategoriesController(IMediator mediator) : ControllerBase
|
||||
{
|
||||
[HttpGet]
|
||||
public async Task<ActionResult<IEnumerable<CategoryDto>>> GetCategories([FromQuery] GetCategoriesQuery query)
|
||||
[AllowAnonymous]
|
||||
public async Task<ActionResult<IEnumerable<CategoryDto>>> GetCategories(
|
||||
[FromQuery] GetCategoriesQuery query)
|
||||
{
|
||||
return Ok(await mediator.Send(query));
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[Authorize(Roles = "Admin")]
|
||||
public async Task<ActionResult<CategoryDto>> CreateCategory(
|
||||
[FromBody] CreateCategoryCommand command)
|
||||
{
|
||||
var result = await mediator.Send(command);
|
||||
return CreatedAtAction(nameof(GetCategories), new { id = result.Id }, result);
|
||||
}
|
||||
|
||||
[HttpPut("{id:guid}")]
|
||||
[Authorize(Roles = "Admin")]
|
||||
public async Task<ActionResult<CategoryDto>> UpdateCategory(
|
||||
Guid id,
|
||||
[FromBody] UpdateCategoryCommand command)
|
||||
{
|
||||
if (id != command.Id) return BadRequest("ID mismatch");
|
||||
|
||||
return Ok(await mediator.Send(command));
|
||||
}
|
||||
|
||||
[HttpDelete("{id:guid}")]
|
||||
[Authorize(Roles = "Admin")]
|
||||
public async Task<ActionResult> DeleteCategory(Guid id)
|
||||
{
|
||||
await mediator.Send(new DeleteCategoryCommand { Id = id });
|
||||
return NoContent();
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
using Imprink.Application.Domains.ProductVariants;
|
||||
using Imprink.Application.Products.Dtos;
|
||||
using Imprink.Application.Products.Query;
|
||||
using MediatR;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace Imprink.WebApi.Controllers;
|
||||
@@ -10,9 +11,38 @@ namespace Imprink.WebApi.Controllers;
|
||||
public class ProductVariantsController(IMediator mediator) : ControllerBase
|
||||
{
|
||||
[HttpGet]
|
||||
[AllowAnonymous]
|
||||
public async Task<ActionResult<IEnumerable<ProductVariantDto>>> GetProductVariants(
|
||||
[FromQuery] GetProductVariantsQuery query)
|
||||
{
|
||||
return Ok(await mediator.Send(query));
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[Authorize(Roles = "Admin")]
|
||||
public async Task<ActionResult<ProductVariantDto>> CreateProductVariant(
|
||||
[FromBody] CreateProductVariantCommand command)
|
||||
{
|
||||
var result = await mediator.Send(command);
|
||||
return CreatedAtAction(nameof(GetProductVariants), new { id = result.Id }, result);
|
||||
}
|
||||
|
||||
[HttpPut("{id:guid}")]
|
||||
[Authorize(Roles = "Admin")]
|
||||
public async Task<ActionResult<ProductVariantDto>> UpdateProductVariant(
|
||||
Guid id,
|
||||
[FromBody] UpdateProductVariantCommand command)
|
||||
{
|
||||
if (id != command.Id) return BadRequest("ID mismatch");
|
||||
|
||||
return Ok(await mediator.Send(command));
|
||||
}
|
||||
|
||||
[HttpDelete("{id:guid}")]
|
||||
[Authorize(Roles = "Admin")]
|
||||
public async Task<ActionResult> DeleteProductVariant(Guid id)
|
||||
{
|
||||
await mediator.Send(new DeleteProductVariantCommand { Id = id });
|
||||
return NoContent();
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
using Imprink.Application.Domains.Products;
|
||||
using Imprink.Application.Products;
|
||||
using Imprink.Application.Products.Dtos;
|
||||
using Imprink.Domain.Models;
|
||||
@@ -20,4 +21,33 @@ public class ProductsController(IMediator mediator) : ControllerBase
|
||||
var result = await mediator.Send(new GetProductsQuery { FilterParameters = filterParameters});
|
||||
return Ok(result);
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[Authorize(Roles = "Admin")]
|
||||
public async Task<ActionResult<PagedResultDto<ProductDto>>> CreateProduct(
|
||||
[FromBody] CreateProductCommand command)
|
||||
{
|
||||
var result = await mediator.Send(command);
|
||||
return Ok(result);
|
||||
}
|
||||
|
||||
[HttpPut("{id:guid}")]
|
||||
[Authorize(Roles = "Admin")]
|
||||
public async Task<ActionResult<ProductDto>> UpdateProduct(
|
||||
Guid id,
|
||||
[FromBody] UpdateProductCommand command)
|
||||
{
|
||||
if (id != command.Id) return BadRequest("ID mismatch");
|
||||
|
||||
var result = await mediator.Send(command);
|
||||
return Ok(result);
|
||||
}
|
||||
|
||||
[HttpDelete("{id:guid}")]
|
||||
[Authorize(Roles = "Admin")]
|
||||
public async Task<ActionResult> DeleteProduct(Guid id)
|
||||
{
|
||||
await mediator.Send(new DeleteProductCommand { Id = id });
|
||||
return NoContent();
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
using System.Security.Claims;
|
||||
using Imprink.Application.Users;
|
||||
using Imprink.Application.Domains.Users;
|
||||
using Imprink.Domain.Models;
|
||||
using MediatR;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
|
||||
Reference in New Issue
Block a user