public StockMovementActionResultModel Revert(long id) { var stockMovementToRevert = _ctx.Set <StockMovementEntity>().SingleOrDefault(e => e.Id == id); if (stockMovementToRevert == null) { throw new NotFoundException(); } var stockMovement = new StockMovementEntity { Price = stockMovementToRevert.Price, Quantity = stockMovementToRevert.Quantity * -1, CreatedOn = DateTimeOffset.Now, CreatedBy = this.GetUserName(), ProductId = stockMovementToRevert.ProductId }; _ctx.Add(stockMovement); _ctx.SaveChanges(); return(new StockMovementActionResultModel { Id = stockMovement.Id }); }
public StockMovementActionResultModel Create(string productCode, [FromBody] CreateStockmovementModel model) { if (!ModelState.IsValid) { throw new ValidationException(ModelState); } var product = _ctx.Set <ProductEntity>().SingleOrDefault(e => e.Code == productCode); if (product == null) { throw new NotFoundException(); } var stockMovement = new StockMovementEntity { Price = product.Price, Quantity = model.Quantity, CreatedOn = DateTimeOffset.Now, CreatedBy = this.GetUserName(), Product = product }; _ctx.Add(stockMovement); _ctx.SaveChanges(); return(new StockMovementActionResultModel { Id = stockMovement.Id }); }