public PayDetailsRequest.PayDetailsResponse CalculateTax(PayDetailsRequest request) { PayDetails payDetails = new PayDetails() { FirstName = request.FirstName, SurName = request.SurName, PayStartDate = request.PayStartDate, PayEndDate = request.PayEndDate }; bool isValueValid = payDetails.TrySetSuperRate(request.SuperRate) && payDetails.TrySetAnnualSalary(request.AnnualSalary); if (isValueValid) { return(new PayDetailsRequest.PayDetailsResponse( payDetails.PayPeriod, payDetails.FullName, payDetails.GrossIncome, payDetails.IncomeTax, payDetails.NetIncome, payDetails.Super, 200)); } // do we reject ? throw exception ? throw new exception /*TODO: map the methods in the payDetail object*/ // fill out response object from paydetails return(new PayDetailsRequest.PayDetailsResponse(422, "Invalid input values")); }
//[BasicAuthenticationAttribute] public async Task <HttpResponseMessage> SavePayDetail(PayDetailsRequest payDetailsRequest) { httpResponseMessage = new HttpResponseMessage(); if (payDetailsRequest != null && ModelState.IsValid) { var paymentDetailsResponse = await _IAccountService.SavePayDetail(payDetailsRequest); httpResponseMessage = Request.CreateResponse(HttpStatusCode.OK, paymentDetailsResponse); } else { httpResponseMessage = Request.CreateResponse(HttpStatusCode.OK, new { Message = CustomErrorMessages.INVALID_INPUTS, Success = false }); } return(httpResponseMessage); }
/*I use 422 here because it best describes the error type: an invalid data is provided in the request body*/ public IActionResult Post(PayDetailsRequest request) { //CalculateTax try / catch var service = new PayDetailsService(); var response = service.CalculateTax(request); if (response.ResponseCode == 200) { return(Ok(JsonSerializer.Serialize(response))); } return(UnprocessableEntity()); ; /*json.Stringfy*/ /*My finding: C# doesn't have json.Stringfy, it is JsonSerializer.Serialize(myObject) instead*/ /*response code to be configured here in the controller*/ }
public async Task <BaseResponse> SavePayDetail(PayDetailsRequest payDetailsRequest) { return(await _IAccountRepository.SavePayDetail(payDetailsRequest)); }