Overview
I am designing one api that will be integration with another system, however I stumbled upon one code which I know it can be improved but I don't know how to do it properly.
Code
[HttpPost]
[Route("Reserve")]
public IHttpActionResult Reserve([FromUri]ReserveData reserveData, [FromBody]BetsData bet)
{
var result = new ReserveResponse();
// Question 1.
if (reserveData == null || !IsDataForReserveValid(reserveData))
{
// Question 2.
return Ok(_responseStringBuilder.BuildWrongRequestResponse().Create());
}
// Question 3.
if (!_customerService.CheckIfCustomerExists(reserveData.cust_id))
{
return Ok(_responseStringBuilder.BuildCustomerNotFoundResponse().Create());
}
if (_customerService.IsCustomerRestricted(reserveData.cust_id))
{
return Ok(_responseStringBuilder.BuildRestrictedCustomerResponse().Create());
}
if (!_reserveService.ReserveAmount(reserveData.cust_id, reserveData.amount))
{
return Ok(_responseStringBuilder.BuildInsufficientFundsResponse().Create());
}
if (bet != null)
{
}
_reserveService.InsertReserve(reserveData);
return Ok(_responseStringBuilder.BuildNoErrorsResponse().Create());
}
Requirements
This is simple action that should return Ok with string in its response content.
Questions
- Is there any better what of checking for valid input? I've created hierarchy of methods validating input of each action in this controller(because most of the input for different actions is actually repeating) as private methods in the controller.
- I've created string builder pattern and I build each time my request with needed properties. Is this the best way?
-
There is some business logic which I've put in the controller, but I am not sure if it should be in the service layer or not Should I leave it like that or invoke single method from a service which does all the logic by itself there and simply returns result to me if there are errors or not?
Example response would be: ...(some data) error_code=NoErrors\r\n error_message=There were no errors\r\n
Aucun commentaire:
Enregistrer un commentaire