示例#1
0
        public void Post([FromBody] DTO.Product product)
        {
            Console.WriteLine($"DEBUG: Entering {nameof(Post)}");
            Console.WriteLine($"DEBUG: Request body contained: {product}");
            // TODO: updatedBy
            var updatedBy = "SYSTEM";

            using (var db = new TransactionServiceDbContext())
            {
                var dbProduct = db.Products.FindAsync(product.ProductId).Result;
                if (dbProduct == null)
                {
                    // PK is serial, so should be automatically generated
                    dbProduct = new Domain.Entities.Product
                    {
                        CreatedBy = updatedBy,
                        CreatedOn = DateTime.UtcNow
                    };
                    db.Add(dbProduct);
                }
                dbProduct.Name        = product.Name;
                dbProduct.Description = product.Description;
                dbProduct.UpdatedBy   = updatedBy;
                dbProduct.UpdatedOn   = DateTime.UtcNow;
                db.SaveChangesAsync();
            }
        }
示例#2
0
        public ActionResult <DTO.Product> Get(int id)
        {
            DTO.Product product;

            using (var db = new TransactionServiceDbContext())
            {
                product = db.Products
                          .Where(p => !p.Deleted &&
                                 p.ProductId == id)
                          .Select(p => p.toDto())
                          .FirstOrDefault();

                if (product != null)
                {
                    // get sale price
                    product.AvgSalePrice = Decimal.Round(db.LineItems
                                                         .Where(l => !l.Deleted &&
                                                                l.ProductId == product.ProductId &&
                                                                l.Quantity > 0 &&
                                                                l.Transaction.TransactionType == TransactionTypeEnum.Sale.ToString())
                                                         .Sum(l => l.Price / l.Quantity), 2);
                    // get purchase price
                    product.AvgPurchasePrice = Decimal.Round(db.LineItems
                                                             .Where(l => !l.Deleted &&
                                                                    l.ProductId == product.ProductId &&
                                                                    l.Quantity > 0 &&
                                                                    l.Transaction.TransactionType == TransactionTypeEnum.Purchase.ToString())
                                                             .Sum(l => l.Price / l.Quantity), 2);
                }
            }

            // Set default for new products
            if (product == null)
            {
                product = new DTO.Product
                {
                    ProductId   = -1,
                    Name        = "<name>",
                    Description = "<description>"
                };
            }

            return(product);
        }