Skip to content

Commit

Permalink
Create Sale/Add action
Browse files Browse the repository at this point in the history
  • Loading branch information
RieBi committed Jul 25, 2024
1 parent f672839 commit 9f2cb49
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions Api/Controllers/SaleController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using Application.Commands.SaleCommands;
using Application.Exceptions;
using MediatR;
using Microsoft.AspNetCore.Mvc;

namespace Api.Controllers;
[Route("api/[controller]")]
[ApiController]
public class SaleController(IMediator mediator) : ControllerBase
{
private readonly IMediator _mediator = mediator;

[HttpPost]
[Route("Add")]
[ProducesResponseType(200)]
[ProducesResponseType<ErrorDto>(404)]
public async Task<ActionResult> Add(string wholesalerId, string beerId, int quantity)
{
try
{
await _mediator.Send(new CreateSaleCommand(wholesalerId, beerId, quantity));

return Ok();
}
catch (WholesalerNotFoundException exception)
{
return CreateWholesalerNotFoundResult(exception);
}
catch (BeerNotFoundException exception)
{
return CreateBeerNotFoundResult(exception);
}
}

private NotFoundObjectResult CreateWholesalerNotFoundResult(WholesalerNotFoundException exception)
{
var message = $"Wholesaler with id '{exception.ResourceId}' was not found.";
return NotFound(new ErrorDto(exception, message));
}

private NotFoundObjectResult CreateBeerNotFoundResult(BeerNotFoundException exception)
{
var message = $"Beer with id '{exception.ResourceId}' was not found.";
return NotFound(new ErrorDto(exception, message));
}
}

0 comments on commit 9f2cb49

Please sign in to comment.