I need some guidance on how to send error responses to client from WebAPI controller for an update operation. I need to check if data is changed and if it has duplicate data. I have service class that does database operations.
Below is psuedo code
Approach 1
CustomerController.cs
public Task<IActionResult> UpdateCustomer(int id, Customer request)
{
var customer = service.GetCustomer(id);
if(customer is null)
return NotFound()
var isChanged = service.IsChanged(request, customer);
if(!isChanged)
return BadRequest();
var isDuplicate = service.IsDuplicate(request);
if(!isDuplicate)
return BadRequest(); //500 error
await service.Update(request)
}
Approach 2
CustomerController.cs
public Task<IActionResult> UpdateCustomer(int id, Customer request)
{
var customer = service.GetCustomer(id);
if(customer is null)
return NotFound()
await service.Update(request, customer);
}
CustomerService.cs
public void Update(Customer request, Customer customer)
{
if(!HasChanged(request, customer))
throw NoChangesException();
if(IsDuplicate(request))
throw DuplicateException();
db.Update(request);
}
Catch exceptions in Global exception Middleware and send the BadRequest or Internal error based on exception type.
Edit
I need to add that duplicates and no changes will be rejected by data layer. Data layer is another common OData service. I feel its expensive to call the data service for known exceptions.
1 Answer 1
Approach 0
Controller
public Task UpdateCustomer(Customer request)
{
await service.Update(request)
}
Service
public void Update(Customer request)
{
db.Update(request);
}
If there has been no change, update anyway
If the customer already exists, update it
If the customer doesn't exist, add it.
If there is a db error, throw it and convert automatically to a 500 in a global error handler
-
I need to clarify that duplicates and no changes will be rejected by data layer. Its the functionality across common database service. So we know that exceptions occur if we submit the request.Sunny– Sunny01/27/2024 17:51:51Commented Jan 27, 2024 at 17:51
-
Also, Data layer is a OData web service and I feel it can get expensive to submit the update for known exceptions.Sunny– Sunny01/27/2024 18:13:13Commented Jan 27, 2024 at 18:13
-
11. im unclear on what you mean by duplicate here, you are updatign right? so the record must exist. Also I think odata v4 supports Upserts? so it should be fine with dupes if its the create thats causing issues.. But you use of OData behind another rest api seems questionable in of itself to me.Ewan– Ewan01/28/2024 10:41:57Commented Jan 28, 2024 at 10:41
-
12. re: performance. You would have to measure the performance of requesting the existing record first for every update request vs sending some update that will fail. Obviously is depends on the volumes of the different types of request and whether you can cache known dupes or something clever, but, I think you will find the "just try it and throw" approach faster. You can see without the request less data is going over the wireEwan– Ewan01/28/2024 10:43:40Commented Jan 28, 2024 at 10:43
-
1you should be able to do a patch request without retrieving the row first and you should be able to use a patch request to do an upsert : docs.oasis-open.org/odata/odata/v4.01/…Ewan– Ewan01/28/2024 12:52:17Commented Jan 28, 2024 at 12:52
Explore related questions
See similar questions with these tags.