-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
46 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |