コード例 #1
0
        // these are quite repetitive. How can I improve? Uncle Bob would say separate your error handling from method's logic
        public ServiceResponse <ServiceProducts.Product> CreateProduct(ServiceProducts.Product productToAdd)
        {
            var now      = DateTime.Now;
            var response = new ServiceResponse <ServiceProducts.Product>()
            {
                Time = now
            };

            try
            {
                var entityProduct = ProductMapper.SerialiseProduct(productToAdd);
                entityProduct.CreatedOn = now;
                entityProduct.UpdatedOn = now;
                _productsWriter.AddProductToDb(entityProduct);
                CreateProductInventoryRecord(productToAdd);

                response.IsSuccessful = true;
                response.Message      = ($"Successfully added {productToAdd.Name}");
                response.Data         = ProductMapper.SerialiseProduct(entityProduct);
            }
            catch (Exception e)
            {
                response.IsSuccessful = false;
                response.Message      = ($"Could not add {productToAdd.Name}. Stack trace: {e.StackTrace}");
                response.Data         = productToAdd;
            }

            return(response);
        }
コード例 #2
0
        public ActionResult CreateProduct([FromBody] ServiceProducts.Product product)
        {
            _logger.LogInformation($"Creating product {product.Name}...");
            var productResponse = _productsService.CreateProduct(product);

            return(Ok(productResponse));
        }
コード例 #3
0
        // in goes an entity model and out comes a service model
        public static ServiceProducts.Product SerialiseProduct(EntityProducts.Product entityProduct)
        {
            var serviceProduct = new ServiceProducts.Product()
            {
                Id          = entityProduct.Id,
                Name        = entityProduct.Name,
                Description = entityProduct.Description,
                Category    = entityProduct.Category,
                Price       = entityProduct.Price,
                IsOnSale    = entityProduct.IsOnSale,
                IsArchived  = entityProduct.IsArchived,
                IsTaxable   = entityProduct.IsTaxable,
                CreatedOn   = entityProduct.CreatedOn,
                UpdatedOn   = entityProduct.UpdatedOn
            };

            return(serviceProduct);
        }
コード例 #4
0
        public void CreateProductInventoryRecord(ServiceProducts.Product product)
        {
            // there's no valid id we can use to retrieve by id, because it gets generated automatcially by DB when that product is added
            var entityProduct = _productsReader.GetMostRecentlyAddedProductFromDb();
            var now           = DateTime.Now;

            try
            {
                var productInventoryRecord = new EntityInventories.ProductInventory()
                {
                    Product           = entityProduct.Name == product.Name ? entityProduct : null,
                    QuantityAvailable = 0,
                    IdealQuantity     = 15,
                    CreatedOn         = now,
                    UpdatedOn         = now
                };
                _inventoriesWriter.AddProductInventoryRecordToDb(productInventoryRecord);
            }
            catch (Exception)
            {
                throw new InvalidOperationException($"Failed to add inventory record for product {product.Name}. Most recently added entity product retreived was {entityProduct.Name}");
            }
        }