コード例 #1
0
        public async Task <IActionResult> New(IFormCollection form, [FromServices] ILoanManagement loanManagement)
        {
            JsonElement loanJson        = JsonDocument.Parse(FormReader.GetLoanJson(form)).RootElement;
            JsonElement collateralsJson = JsonDocument.Parse($"[{FormReader.GetCollateralJson(form, _logger)}]").RootElement;

            _logger.LogInformation(collateralsJson.GetRawText());

            try
            {
                //return Ok(await loanManagement.SaveWithCollaterals(loanJson, collateralsJson));
                if (await loanManagement.SaveWithCollaterals(loanJson, collateralsJson))
                {
                    int newLoanId = FormReader.GetLoan(form).Id;
                    return(RedirectToAction(actionName: nameof(LoanController.ViewLoan), new { id = newLoanId }));
                }
                //return Ok("loan and collaterals saved successfully");
                else
                {
                    return(StatusCode((int)HttpStatusCode.InternalServerError, new { error = "error occurred while saving loan and collaterals" }));
                }
            }
            catch (HttpRequestException) { return(StatusCode((int)HttpStatusCode.ServiceUnavailable, new { error = "cannot connect with LoanManagementApi" })); }
            catch (UnexpectedResponseException e) { return(StatusCode((int)HttpStatusCode.InternalServerError, new { error = e.Message })); }
        }