Exemplo n.º 1
0
        public async Task <Guid> AddAsync(DtoCreateProduct model)
        {
            try
            {
                ValidationResult validationResult = createValidator.Validate(model);
                if (!validationResult.IsSuccessful)
                {
                    throw new MissingDataException(validationResult.ErrorMessage);
                }


                Product product = mapper.Map <DtoCreateProduct, Product>(model);

                var result = await dbContext.Products.AddAsync(product);

                await dbContext.SaveChangesAsync();

                return(result.Entity.Id);
            }
            catch (Exception ex)
            {
                logger.LogError(ex, $"Error on {nameof(AddAsync)} in {nameof(ProductsService)} Entity: {model}");

                throw;
            }
        }
Exemplo n.º 2
0
        public ValidationResult Validate(DtoCreateProduct model)
        {
            if (model.Price < MinimalProductPrice)
            {
                return(new ValidationResult {
                    ErrorMessage = $"Product Price is below minimal, is: {model.Price}, minimal is: {MinimalProductPrice}"
                });
            }
            if (model.Price > MaximalProductPrice)
            {
                return(new ValidationResult {
                    ErrorMessage = $"Product Price is above maximal, is: {model.Price}, minimal is: {MaximalProductPrice}"
                });
            }
            if (model.Name.Length < MinimalProductNameLength)
            {
                return(new ValidationResult {
                    ErrorMessage = $"Product Name is below minimal length, is: {model.Name.Length}, minimal is: {MaximalProductNameLength}"
                });
            }
            if (model.Name.Length > MaximalProductNameLength)
            {
                return(new ValidationResult {
                    ErrorMessage = $"Product Name is below minimal length, is: {model.Name.Length}, maximal is: {MaximalProductNameLength}"
                });
            }

            return(new ValidationResult {
                IsSuccessful = true
            });
        }
        public async Task <IActionResult> PostAsync([FromBody] DtoCreateProduct model)
        {
            try
            {
                Guid productId = await service.AddAsync(model);

                return(Ok(productId));
            }
            catch
            {
                return(BadRequest());
            }
        }