Пример #1
0
        public async Task <AddProductResponse> AddProductAsync(AddProductRequest addProductRequest)
        {
            var config = new ProductConfiguration()
            {
                ExpiryInDays = 30
            };

            Core.Product product  = addProductRequest.ToEntity();
            Core.Product response = await product.SaveAsync(_productStore, config);

            return(response.ToModel());
        }
Пример #2
0
        public async Task <IActionResult> AddProductAsync([FromBody] AddProductRequest request)
        {
            Logger?.LogDebug("'{0}' has been invoked", nameof(AddProductAsync));

            // Validate request model
            if (!ModelState.IsValid)
            {
                return(BadRequest(request));
            }

            var response = new SingleResponse <AddProductRequest>();

            try
            {
                var entity = request.ToEntity();

                // Check if entity exists
                var existingProduct = await Service.DbContext.GetProductByProductNameAsync(entity);

                if (existingProduct != null)
                {
                    ModelState.AddModelError("ProductName", "Product name already exists");

                    return(BadRequest(ModelState));
                }

                entity.CreationUser = User.GetUserName();

                await Service.CreateProductAsync(entity);

                response.Model = entity.ToAddProductRequest();

                Logger?.LogInformation("The entity was created successfully.");
            }
            catch (Exception ex)
            {
                response.SetError(Logger, nameof(AddProductAsync), ex);
            }

            return(response.ToHttpCreatedResponse());
        }