Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions After/src/Api/Customers/CustomersController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,19 @@ public IActionResult Create([FromBody] CreateCustomerDto item)
Result<CustomerName> customerNameOrError = CustomerName.Create(item.Name);
Result<Email> emailOrError = Email.Create(item.Email);

Result result = Result.Combine(customerNameOrError, emailOrError);
if (result.IsFailure)
return Error(result.Error);
if (customerNameOrError.IsFailure)
ModelState.AddModelError(nameof(item.Name), customerNameOrError.Error);
if (emailOrError.IsFailure)
ModelState.AddModelError(nameof(item.Email), emailOrError.Error);

if (!ModelState.IsValid)
return Error(ModelState);

if (_customerRepository.GetByEmail(emailOrError.Value) != null)
return Error("Email is already in use: " + item.Email);
{
ModelState.AddModelError(nameof(item.Email), "Email is already in use: " + item.Email);
return Error(ModelState);
}

var customer = new Customer(customerNameOrError.Value, emailOrError.Value);
_customerRepository.Add(customer);
Expand Down
6 changes: 6 additions & 0 deletions After/src/Api/Utils/BaseController.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Logic.Utils;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ModelBinding;

namespace Api.Utils
{
Expand All @@ -24,6 +25,11 @@ protected IActionResult Ok<T>(T result)
return base.Ok(Envelope.Ok(result));
}

protected IActionResult Error(ModelStateDictionary modelState)
{
return BadRequest(modelState);
}

protected IActionResult Error(string errorMessage)
{
return BadRequest(Envelope.Error(errorMessage));
Expand Down