Skip to content

Commit

Permalink
Add CustomerController with Validations
Browse files Browse the repository at this point in the history
  • Loading branch information
izzat5233 committed Jul 5, 2024
1 parent 837d328 commit b093346
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 0 deletions.
89 changes: 89 additions & 0 deletions RestaurantReservation.Api/Controllers/CustomerController.cs
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 RestaurantReservation.App/Validations/CustomerDtoValidator.cs
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);
}
}

0 comments on commit b093346

Please sign in to comment.