Skip to content

Commit

Permalink
fix(client): check if the client already exists orders and cleaned th…
Browse files Browse the repository at this point in the history
…e response
  • Loading branch information
simosbe3 committed Dec 18, 2024
1 parent 3344081 commit 43ea0b7
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 24 deletions.
50 changes: 27 additions & 23 deletions api/Controllers/ClientsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,30 +171,34 @@ public IActionResult Delete(Guid id)

if (foundclient == null) return NotFound(new { message = "Client not found." });

return Ok(new ClientResponse
return Ok(new
{
Id = foundclient.Id,
Name = foundclient.Name,
Contact = new ContactResponse
{
Id = foundclient.ContactId,
Name = foundclient.Contact?.Name,
Phone = foundclient.Contact?.Phone,
Email = foundclient.Contact?.Email
},
Address = new AddressResponse
{
Id = foundclient.AddressId,
Street = foundclient.Address?.Street,
HouseNumber = foundclient.Address?.HouseNumber,
HouseNumberExtension = foundclient.Address?.HouseNumberExtension,
ZipCode = foundclient.Address?.ZipCode,
City = foundclient.Address?.City,
Province = foundclient.Address?.Province,
CountryCode = foundclient.Address?.CountryCode
},
CreatedAt = foundclient.CreatedAt,
UpdatedAt = foundclient.UpdatedAt
Message = "Client deleted",
new_client = new ClientResponse{

Id = foundclient.Id,
Name = foundclient.Name,
Contact = new ContactResponse
{
Id = foundclient.ContactId,
Name = foundclient.Contact?.Name,
Phone = foundclient.Contact?.Phone,
Email = foundclient.Contact?.Email
},
Address = new AddressResponse
{
Id = foundclient.AddressId,
Street = foundclient.Address?.Street,
HouseNumber = foundclient.Address?.HouseNumber,
HouseNumberExtension = foundclient.Address?.HouseNumberExtension,
ZipCode = foundclient.Address?.ZipCode,
City = foundclient.Address?.City,
Province = foundclient.Address?.Province,
CountryCode = foundclient.Address?.CountryCode
},
CreatedAt = foundclient.CreatedAt,
UpdatedAt = foundclient.UpdatedAt
}
});
}

Expand Down
5 changes: 4 additions & 1 deletion api/providers/ClientsProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,12 @@ public ClientsProvider(AppDbContext db, IValidator<Client> clientValidator, Cont
public override Client? Delete(Guid id)
{
Client? foundClient = GetById(id);

if (foundClient == null) return null;

if (_db.Orders.Any(o => o.BillToClientId == id) )
{
throw new ApiFlowException($"{id} The provided client_id is in use and cannot be modified.");
}
_db.Clients.Remove(foundClient);
SaveToDBOrFail();

Expand Down

0 comments on commit 43ea0b7

Please sign in to comment.