public Movement MoveStock(int stockId, int locationId, string notes = "") { var locationRepo = new LocationRepository(Db); var movementRepo = new MovementRepository(Db); var stockRepo = new StockRepository(Db); var stock = stockRepo.Find(stockId); if (stock == null) { throw new EntityNotFoundException("Cannot find Stock with given ID"); } var location = locationRepo.Find(locationId); if (location == null) { throw new EntityNotFoundException("Cannot find Location with given ID"); } stock.LocationId = location.Id; stockRepo.Update(stock); var movement = new Movement { DateCreated = DateTime.UtcNow, StockId = stock.Id, ToLocationId = location.Id, Notes = notes }; movementRepo.Add(movement); Db.SaveChanges(); return(movement); }
public Movement MoveStock(Movement movement) { var locationRepo = new LocationRepository(Db); var movementRepo = new MovementRepository(Db); var stockRepo = new StockRepository(Db); var stock = stockRepo.Find(movement.StockId); if (stock == null) { throw new EntityNotFoundException("Cannot find Stock with given ID"); } var location = locationRepo.Find(movement.ToLocationId); if (location == null) { throw new EntityNotFoundException("Cannot find Location with given ID"); } stock.LocationId = location.Id; stockRepo.Update(stock); // TODO: Optimize! var lastKnownMovt = movementRepo.All.Where(x => x.StockId == movement.StockId).ToList().LastOrDefault(); if (lastKnownMovt != null) { movement.FromLocationId = lastKnownMovt.ToLocationId; } movement.DateCreated = DateTime.UtcNow; movementRepo.Add(movement); Db.SaveChanges(); return(movement); }