예제 #1
0
        public async Task <IActionResult> Post([FromBody] OrderRequestModel request)
        {
            /*Check if a car variant exists*/
            var carVariant = await _variants.FindById(request.VariantId);

            if (carVariant == null || !carVariant.IsAvailable)
            {
                _logger.LogWarning($"VariantId: {request.VariantId} - NotFound.");
                return(NotFound());
            }

            /*Apply for bank loan*/
            BankLoanApiRequest bankLoanApiRequest = _mapper.Map <BankLoanApiRequest>(request);

            var bankLoanResponce = await _bankLoan.ApplyBankLoanAsync(bankLoanApiRequest);

            if (bankLoanResponce == null)
            {
                return(BadRequest());
            }
            string responseString = await bankLoanResponce.Content.ReadAsStringAsync();

            HttpContext.Response.StatusCode = (int)bankLoanResponce.StatusCode;
            if (!bankLoanResponce.IsSuccessStatusCode)
            {
                return(new JsonResult(JsonConvert.DeserializeObject <JObject>(responseString)));
            }
            BankLoanApiResponce bankLoan = JsonConvert.DeserializeObject <BankLoanApiResponce>(responseString);

            /*Add Customer*/
            Customer newCustomer = _mapper.Map <Customer>(request);
            await _customer.Create(newCustomer).ConfigureAwait(false);

            /*Add CarOrder*/
            CarOrder order = _mapper.Map <CarOrder>(carVariant);

            Customer customer = await _customer.GetByEmail(request.CustomerEmail);

            order.Created                = DateTime.Now;
            order.CustomerId             = customer.Id;
            order.BankLoanDuration       = request.BankLoanDuration;
            order.BankLoanDownPayment    = request.BankLoanDownPayment;
            order.BankLoanMonthlyPayment = (decimal)bankLoan.MonthlyPayment;

            await _order.Create(order).ConfigureAwait(false);

            /*building response*/
            OrderApiResponce response = _mapper.Map <OrderApiResponce>(carVariant);

            response.BankLoanDuration       = request.BankLoanDuration;
            response.BankLoanDownPament     = request.BankLoanDownPayment;
            response.BankLoanMonthlyPayment = (decimal)bankLoan.MonthlyPayment;

            return(new JsonResult(response));
        }
        public async Task <HttpResponseMessage> ApplyBankLoanAsync(BankLoanApiRequest request)
        {
            string apiKeyName            = "x-api-key";
            HttpResponseMessage response = null;
            var carVariant = await _appDBContent.CarVariant.FirstOrDefaultAsync(c => c.Id == request.VariantId);

            if (carVariant == null || !carVariant.IsAvailable)
            {
                return(response);
            }

            using (var client = new HttpClient()) {
                client.BaseAddress = new Uri(_configuration.GetValue <string>("bankLoanUrl"));
                client.DefaultRequestHeaders.Add("Accept", "aplication/json");
                client.DefaultRequestHeaders.Add(apiKeyName, _configuration.GetValue <string>(apiKeyName));

                response = await client.GetAsync($"/api/Loan?CarPrice={carVariant.Price}&UpFrontPayment={request.DownPayment}&BankLoanTerm={request.Duration}&CarBuildYear={carVariant.Year}");
            }
            return(response);
        }
예제 #3
0
        public async Task <IActionResult> Get([FromQuery] BankLoanApiRequest request)
        {
            _logger.LogInformation($"Query with params: {JsonConvert.SerializeObject(request)}");
            var response = await _bankLoan.ApplyBankLoanAsync(request);

            if (response == null)
            {
                _logger.LogWarning($"VariantId: {request.VariantId} - NotFound.");
                return(NotFound());
            }
            string responseString = await response.Content.ReadAsStringAsync();

            if (!response.IsSuccessStatusCode)
            {
                _logger.LogWarning(responseString);
                HttpContext.Response.StatusCode = (int)response.StatusCode;
                return(new JsonResult(JsonConvert.DeserializeObject <JObject>(responseString)));
            }

            return(new JsonResult(JsonConvert.DeserializeObject <JObject>(responseString)));
        }