Skip to content

Commit

Permalink
Merge pull request #140 from ahmetDev7/INFSCP01-123-feature-delete-cl…
Browse files Browse the repository at this point in the history
…ient

Infscp01 123 feature delete client
  • Loading branch information
Sharif-C authored Dec 19, 2024
2 parents f6ac6c1 + 00ecfbe commit ca5561c
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 85 deletions.
39 changes: 39 additions & 0 deletions api/Controllers/ClientsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,45 @@ public IActionResult ShowAll()
return Ok(clientResponses);
}

[HttpDelete("{id}")]
public IActionResult Delete(Guid id)
{
Client? foundclient = _clientProvider.Delete(id);

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

return Ok(new
{
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
}
});
}

[HttpGet("{clientId}/orders")]
public IActionResult ShowRelatedOrders(Guid clientId) =>
Ok(_clientProvider.GetRelatedOrdersById(clientId)
Expand Down
94 changes: 9 additions & 85 deletions api/REST/Clients.rest
Original file line number Diff line number Diff line change
@@ -1,80 +1,3 @@
# Create Client with Contact Body and Address Body
POST http://localhost:5000/api/clients
Content-Type: application/json

{
"name": "Client A",
"contact": {
"name": "John Doe",
"phone": "+1234567890",
"email": "[email protected]"
},
"address": {
"street": "Main Street",
"house_number": "10",
"zipcode": "12345",
"city": "Metropolis",
"province": "Central",
"country_code": "US"
}
}
###

# Create Client with Contact Body and Address Body
POST http://localhost:5000/api/clients
Content-Type: application/json

{
"name": "Client A",
"contact": {
"name": "John Doe",
"phone": "+1234567890",
"email": "[email protected]"
},
"address": {
"street": "Main Street",
"house_number": "10",
"zipcode": "12345",
"city": "Metropolis",
"province": "Central",
"country_code": "US"
}
}
###

# Create Client with Address ID and Contact Body
POST http://localhost:5000/api/clients
Content-Type: application/json

{
"name": "Client B",
"contact": {
"name": "Jane Doe",
"phone": "+9876543210",
"email": "[email protected]"
},
"address_id": "b20a737d-b577-4dc9-a7ea-b0bf93d45eda"
}
###

# Create Client with Contact ID and Address Body
POST http://localhost:5000/api/clients
Content-Type: application/json

{
"name": "Client C",
"contact_id": "a2a2bb62-0398-4b76-a80e-0f509154906f",
"address": {
"street": "Second Street",
"house_number": "42",
"zipcode": "54321",
"city": "Smallville",
"province": "East",
"country_code": "US"
}
}
###

# Create Client with Contact ID and Address ID
POST http://localhost:5000/api/clients
Content-Type: application/json
Expand All @@ -85,14 +8,6 @@ Content-Type: application/json
"address_id": "21903d3a-3d0f-4949-bf0d-e2a84cd1a450"
}
###

### GetById Clients
GET http://localhost:5000/api/clients/17187ac9-34b3-4a94-b4c9-922c06606117
###

### GetAll Clients
GET http://localhost:5000/api/clients
###
# Update Client with Contact ID and Address ID
PUT http://localhost:5000/api/clients/fa5946ab-2ab8-4274-a2eb-b717855f8dca
Content-Type: application/json
Expand All @@ -103,3 +18,12 @@ Content-Type: application/json
"address_id": "0ba16ba7-0c7a-45de-9fe6-9054fb6df6a8"
}
###
# Delete Clients
DELETE http://localhost:5000/api/clients/4a07f327-2ce1-499a-b2ab-692ce2a471be
###
### GetById Clients
GET http://localhost:5000/api/clients/17187ac9-34b3-4a94-b4c9-922c06606117
###
### GetAll Clients
GET http://localhost:5000/api/clients
###
16 changes: 16 additions & 0 deletions api/providers/ClientsProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,22 @@ public ClientsProvider(AppDbContext db, IValidator<Client> clientValidator, Cont

return newClient;
}
public override Client? Delete(Guid id)
{
Client? foundClient = GetById(id);
if (foundClient == null) return null;

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

return foundClient;
}


public override Client? Update(Guid id, BaseDTO updateValues)
{
Expand Down

0 comments on commit ca5561c

Please sign in to comment.