コード例 #1
0
        public int?CreateApplication(ApplicationExtendedModel application)
        {
            string json     = JsonSerializer.Serialize <ApplicationExtendedModel>(application);
            var    response = worker.SendMessage(createMessange + json);

            worker.Close();

            int id;

            return(!string.IsNullOrEmpty(response) && int.TryParse(response, out id) ? id : (int?)null);
        }
コード例 #2
0
        public async Task <IActionResult> PostApplication([FromBody] ApplicationModel application)
        {
            if (application.ClientId <= 0)
            {
                ModelState.AddModelError("ClientId", "Client Id should have a positive value");
                logger.LogError("Client Id doesn't have a positive value");
            }
            if (application.Amount <= 0)
            {
                ModelState.AddModelError("Amount", "Amount should have a positive value");
                logger.LogError("Amount doesn't have a positive value");
            }
            if (string.IsNullOrEmpty(application.Currency))
            {
                ModelState.AddModelError("Currency", "Currency is required");
                logger.LogError("Currency is not defined");
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }


            int?applicationId = null;
            var ip            = accessor.ActionContext.HttpContext.Connection.RemoteIpAddress;

            var applicationExtended = new ApplicationExtendedModel
            {
                Id                = application.Id,
                ClientId          = application.ClientId,
                DepartmentAddress = application.DepartmentAddress,
                Amount            = application.Amount,
                Currency          = application.Currency,
                Ip                = ip.ToString()
            };

            logger.LogInformation("Got request to create Application from {ip}", ip);

            try
            {
                logger.LogInformation("Sent request to service to create Application {application}", application);

                applicationId = await Task.Run(() => service.CreateApplication(applicationExtended));

                if (applicationId != null)
                {
                    logger.LogInformation("Received success response from service to create Application");
                }
                else
                {
                    logger.LogInformation("Cannot create Application {application}", applicationExtended);
                    return(NotFound());
                }
            }
            catch (System.Exception exception)
            {
                logger.LogError(exception.Message);
                return(NotFound());
            }

            return(CreatedAtAction(nameof(PostApplication), applicationId));
        }