-
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.
Add CustomerController with Validations
- Loading branch information
Showing
2 changed files
with
118 additions
and
0 deletions.
There are no files selected for viewing
89 changes: 89 additions & 0 deletions
89
RestaurantReservation.Api/Controllers/CustomerController.cs
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,89 @@ | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Mvc; | ||
using RestaurantReservation.App.DTOs; | ||
using RestaurantReservation.App.Interfaces.Services; | ||
|
||
namespace RestaurantReservation.Api.Controllers; | ||
|
||
[Route("api/customers")] | ||
[ApiController] | ||
[Authorize] | ||
public class CustomerController : ControllerBase | ||
{ | ||
private readonly ICustomerService _customerService; | ||
|
||
public CustomerController(ICustomerService customerService) | ||
{ | ||
_customerService = customerService; | ||
} | ||
|
||
[HttpGet("{customerId:int}")] | ||
public async Task<ActionResult<CustomerDto>> GetCustomer(int customerId) | ||
{ | ||
var result = await _customerService.GetCustomerAsync(customerId); | ||
if (!result.IsSuccess) | ||
{ | ||
return NotFound(result.ErrorMessage); | ||
} | ||
|
||
return Ok(result.GetDataOrThrow()); | ||
} | ||
|
||
[HttpPost] | ||
public async Task<ActionResult<CustomerDto>> CreateCustomer(CustomerDto customerDto) | ||
{ | ||
var result = await _customerService.CreateAsync(customerDto); | ||
if (!result.IsSuccess) | ||
{ | ||
return Conflict(result.ErrorMessage); | ||
} | ||
|
||
return CreatedAtAction( | ||
nameof(GetCustomer), | ||
new { customerId = result.GetDataOrThrow().CustomerId }, | ||
result.GetDataOrThrow()); | ||
} | ||
|
||
[HttpPut("{customerId:int}")] | ||
public async Task<IActionResult> UpdateCustomer(int customerId, CustomerDto customerDto) | ||
{ | ||
var updatedDto = customerDto with { CustomerId = customerId }; | ||
var result = await _customerService.UpdateAsync(updatedDto); | ||
if (!result.IsSuccess) | ||
{ | ||
return Conflict(result.ErrorMessage); | ||
} | ||
|
||
return NoContent(); | ||
} | ||
|
||
[HttpDelete("{customerId:int}")] | ||
public async Task<IActionResult> DeleteCustomer(int customerId) | ||
{ | ||
var customerResult = await _customerService.GetCustomerAsync(customerId); | ||
if (!customerResult.IsSuccess) | ||
{ | ||
return NotFound(customerResult.ErrorMessage); | ||
} | ||
|
||
var deleteResult = await _customerService.DeleteAsync(customerResult.GetDataOrThrow()); | ||
if (!deleteResult.IsSuccess) | ||
{ | ||
return Conflict(deleteResult.ErrorMessage); | ||
} | ||
|
||
return NoContent(); | ||
} | ||
|
||
[HttpGet("party-size-greater-than/{partySize:int}")] | ||
public async Task<ActionResult<IEnumerable<CustomerDto>>> GetCustomersWithPartySizeGreaterThan(int partySize) | ||
{ | ||
var result = await _customerService.GetCustomersWithPartySizeGreaterThanAsync(partySize); | ||
if (!result.IsSuccess) | ||
{ | ||
return BadRequest(result.ErrorMessage); | ||
} | ||
|
||
return Ok(result.GetDataOrThrow()); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
RestaurantReservation.App/Validations/CustomerDtoValidator.cs
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,29 @@ | ||
using FluentValidation; | ||
using RestaurantReservation.App.DTOs; | ||
|
||
namespace RestaurantReservation.App.Validations; | ||
|
||
public class CustomerDtoValidator : AbstractValidator<CustomerDto> | ||
{ | ||
public CustomerDtoValidator() | ||
{ | ||
RuleFor(c => c.CustomerId) | ||
.Equal(0) | ||
.WithMessage("CustomerId must not be set"); | ||
RuleFor(c => c.FirstName) | ||
.NotEmpty() | ||
.MaximumLength(100); | ||
RuleFor(c => c.LastName) | ||
.NotEmpty() | ||
.MaximumLength(100); | ||
RuleFor(c => c.Email) | ||
.NotEmpty() | ||
.EmailAddress() | ||
.MaximumLength(255); | ||
RuleFor(c => c.PhoneNumber) | ||
.NotEmpty() | ||
.Matches(@"^\d{3}-\d{3}-\d{4}$") | ||
.WithMessage("Phone number must be in the format xxx-xxx-xxxx, e.g., 123-456-7890") | ||
.MaximumLength(12); | ||
} | ||
} |