/// <summary> /// Validate the object. /// </summary> /// <exception cref="ValidationException"> /// Thrown if validation fails /// </exception> public virtual void Validate() { if (BuyingPrice == null) { throw new ValidationException(ValidationRules.CannotBeNull, "BuyingPrice"); } if (RegularPrice == null) { throw new ValidationException(ValidationRules.CannotBeNull, "RegularPrice"); } if (FulfillmentChannel == null) { throw new ValidationException(ValidationRules.CannotBeNull, "FulfillmentChannel"); } if (ItemCondition == null) { throw new ValidationException(ValidationRules.CannotBeNull, "ItemCondition"); } if (ItemSubCondition == null) { throw new ValidationException(ValidationRules.CannotBeNull, "ItemSubCondition"); } if (SellerSKU == null) { throw new ValidationException(ValidationRules.CannotBeNull, "SellerSKU"); } if (BuyingPrice != null) { BuyingPrice.Validate(); } }
//[ProducesResponseType(201)] //[ProducesResponseType(400)] public IActionResult CreateAsync([FromBody] Product product) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (_context.Products.Any(p => p.Name.Contains(product.Name))) { return(BadRequest("Product already exists")); } product.Created = DateTime.Now; _context.Products.Add(product); _context.SaveChanges(); //update new price double currentPrice = _context.SellingPrices.Where(x => x.ProductId == product.Id).OrderByDescending(x => x.PriceDate).Select(x => x.Price).FirstOrDefault(); if (currentPrice != product.Price) { SellingPrice sp = new SellingPrice(); sp.Price = product.Price; sp.ProductId = product.Id; sp.PriceDate = DateTime.Now; sp.QuantityFrom = 1; _context.SellingPrices.Add(sp); _context.SaveChanges(); } //update import price double importPrice = _context.BuyingPrices.Where(x => x.ProductId == product.Id).OrderByDescending(x => x.PriceDate).Select(x => x.Price).FirstOrDefault(); if (importPrice != product.BuyingPrice) { BuyingPrice bp = new BuyingPrice(); bp.Price = product.BuyingPrice; bp.PriceDate = DateTime.Now; bp.ProductId = product.Id; if (product.SupplierID > 0) { bp.SupplierId = product.SupplierID; } _context.BuyingPrices.Add(bp); _context.SaveChanges(); } return(CreatedAtAction(nameof(GetById), new { id = product.Id }, product)); }
/// <summary> /// Validate the object. /// </summary> /// <exception cref="ValidationException"> /// Thrown if validation fails /// </exception> public virtual void Validate() { if (BuyingPrice == null) { throw new ValidationException(ValidationRules.CannotBeNull, "BuyingPrice"); } if (RegularPrice == null) { throw new ValidationException(ValidationRules.CannotBeNull, "RegularPrice"); } if (FulfillmentChannel == null) { throw new ValidationException(ValidationRules.CannotBeNull, "FulfillmentChannel"); } if (ItemCondition == null) { throw new ValidationException(ValidationRules.CannotBeNull, "ItemCondition"); } if (ItemSubCondition == null) { throw new ValidationException(ValidationRules.CannotBeNull, "ItemSubCondition"); } if (SellerSKU == null) { throw new ValidationException(ValidationRules.CannotBeNull, "SellerSKU"); } if (BuyingPrice != null) { BuyingPrice.Validate(); } if (QuantityDiscountPrices != null) { foreach (var element in QuantityDiscountPrices) { if (element != null) { element.Validate(); } } } }
public async Task <IActionResult> Put([FromRoute] int id, [FromBody] Product product) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != product.Id) { return(BadRequest()); } _context.Entry(product).State = EntityState.Modified; try { await _context.SaveChangesAsync(); //update new price double currentPrice = _context.SellingPrices.Where(x => x.ProductId == product.Id).OrderByDescending(x => x.PriceDate).Select(x => x.Price).FirstOrDefault(); if (product.Price > 0 && currentPrice != product.Price) { SellingPrice sp = new SellingPrice(); sp.Price = product.Price; sp.ProductId = product.Id; sp.PriceDate = DateTime.Now; sp.QuantityFrom = 1; _context.SellingPrices.Add(sp); _context.SaveChanges(); } //update import price double importPrice = _context.BuyingPrices.Where(x => x.ProductId == product.Id).OrderByDescending(x => x.PriceDate).Select(x => x.Price).FirstOrDefault(); if (product.BuyingPrice > 0 && importPrice != product.BuyingPrice) { BuyingPrice bp = new BuyingPrice(); bp.Price = product.BuyingPrice; bp.PriceDate = DateTime.Now; bp.ProductId = product.Id; if (product.SupplierID > 0) { bp.SupplierId = product.SupplierID; } _context.BuyingPrices.Add(bp); _context.SaveChanges(); } } catch (DbUpdateConcurrencyException) { if (!ProductExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }