public async Task <ActionResult> AddCoffee([FromBody] AddCoffeeDto coffeeDto) { try { using (_cofeelogger.BeginScope($"API-AddCoffee {DateTime.UtcNow}")) { var result = await _coffeeService.CreateCoffee(coffeeDto).ConfigureAwait(false); _cofeelogger.LogInformation($"API-AddCoffee {DateTime.UtcNow}"); return(StatusCode((int)result.statusCode, result.coffeeId)); } } catch (Exception ex) { _cofeelogger.LogError (ex, $"API-AddCoffee-Exception {DateTime.UtcNow}" ); return(StatusCode((int)HttpStatusCode.BadRequest, _apiSettings.IsSecuredEnvironment ? "An error occured while processing AddCoffee" : ex.ToString())); } }
public async Task <(HttpStatusCode statusCode, string coffeeId)> CreateCoffee(AddCoffeeDto coffeeToAdd) { _logger.LogInformation($"Service-CreateCoffee-Executing CreateCoffee started at {DateTime.UtcNow}"); var coffeeId = string.Empty; var statusCode = HttpStatusCode.Created; CoffeeDto cofeeToStrore = default(CoffeeDto); var coffeeSpec = new CoffeeWithAreasSpecification(coffeeToAdd.CoffeeName, false); var cofee = (await _coffeeRepository.ListAsync(coffeeSpec).ConfigureAwait(false)).FirstOrDefault(); if (cofee != null) { _logger.LogInformation($"cofee with cofee name {coffeeToAdd.CoffeeName} already exists!!!"); statusCode = HttpStatusCode.BadRequest; } else { Coffee cofeeEntity = await _coffeeRepository.GetMaxOfPrimaryKey(); string newCoffeeDisplayId = cofeeEntity.GetNextPrimaryKey(); cofeeToStrore = new CoffeeDto(newCoffeeDisplayId); cofeeToStrore.CoffeeName = coffeeToAdd.CoffeeName; await _coffeeRepository.AddAsync(cofeeToStrore.ToEntity(true)).ConfigureAwait(false); await _coffeeRepository.SaveAllwithAudit().ConfigureAwait(false); statusCode = HttpStatusCode.OK; coffeeId = newCoffeeDisplayId; } _logger.LogInformation($"Service-CreateCoffee-Executing CreateCoffee completed at {DateTime.UtcNow}"); return(statusCode, coffeeId); }