Ejemplo n.º 1
0
        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);
        }