public async Task <IEnumerable <LocationItemListDto> > GetLocationItems(int?locationId, int?itemId) { if (!(locationId.HasValue || itemId.HasValue)) { return(null); } var entities = await _repository .GetAsync(i => locationId.HasValue?i.LocationId == locationId : i.ItemId == itemId); return(_mapper.Map <IEnumerable <LocationItemListDto> >(entities)); }
public async Task <IActionResult> CreatePurchaseOrder([FromBody] PurchaseOrderDto entityDto) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var entity = _mapper.Map <PurchaseOrder>(entityDto); _repository.Add(entity); // adds the purchase order to the repository // Get the location items involved in the transaction using the locationId of the purchase order and // the itemsIds of the purchase order items. var itemIds = entityDto.PurchaseOrderItems.Select(p => p.ItemId); var locationItems = await _locationItemRepository .GetAsync(l => l.LocationId == entity.LocationId && itemIds.Contains(l.ItemId)); if (locationItems.Count() != itemIds.Count()) { return(BadRequest("Duplicate item in purchase or an item does not exist in the specified location")); } foreach (var lItem in locationItems) { lItem.Quantity += entity.PurchaseOrderItems.Single(p => p.ItemId == lItem.ItemId).Quantity; if (lItem.Quantity < 0) { return(BadRequest(new { message = "Quantity cannot be less than zero", lItem })); } _locationItemRepository.Update(lItem); } try { await _unitOfWork.SaveAsync(); var createdResult = CreatedAtAction("GetPurchaseById", new { id = entity.Id }, entity.Id); createdResult.StatusCode = 200; return(createdResult); } catch (Exception e) { throw new Exception("An unexpected error occured. Could not be added.", e); } }
public async Task <IActionResult> CreateSupply([FromBody] SupplyDto entityDto) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var entity = _mapper.Map <Supply>(entityDto); _repository.Add(entity); var itemIds = entityDto.SupplyItems.Select(s => s.ItemId); var locationItems = await _locationItemRepository .GetAsync(l => l.LocationId == entity.LocationId && itemIds.Contains(l.ItemId)); if (locationItems.Count() != itemIds.Count()) { return(BadRequest("Duplicate item in supply list or an item does not exist")); } foreach (var item in locationItems) { item.Quantity -= entity.SupplyItems.Single(s => s.ItemId == item.ItemId).Quantity; if (item.Quantity < 0) { return(BadRequest(new { message = "Insufficient quantity available", item })); } _locationItemRepository.Update(item); } try { await _unitOfWork.SaveAsync(); var createdResult = CreatedAtAction("GetSupplyById", new { id = entity.Id }, entity.Id); createdResult.StatusCode = 200; return(createdResult); } catch (Exception e) { throw new Exception("An unexpected error occured. Could not be added.", e); } }
public async Task <IActionResult> CreateTransfer([FromBody] TransferDto entityDto) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var entity = _mapper.Map <Transfer>(entityDto); _repository.Add(entity); var itemIds = entityDto.TransferItems.Select(t => t.ItemId); var sourceLocationItems = await _locationItemRepository .GetAsync(l => l.LocationId == entity.SourceLocationId && itemIds.Contains(l.ItemId)); if (sourceLocationItems.Count() != itemIds.Count()) { return(BadRequest("An item must have been duplicated in the transfer or does not exist in the source location")); } var destinationLocationItems = await _locationItemRepository .GetAsync(l => l.LocationId == entity.DestinationLocationId && itemIds.Contains(l.ItemId)); var newLocationItemIds = itemIds.Where(i => !destinationLocationItems.Select(l => l.ItemId).Contains(i)); foreach (var item in sourceLocationItems) { item.Quantity -= entity.TransferItems.Single(t => t.ItemId == item.ItemId).Quantity; if (item.Quantity < 0) { return(BadRequest(new { message = "Quantity cannot be less than zero", item })); } _locationItemRepository.Update(item); } foreach (var item in destinationLocationItems) { item.Quantity += entity.TransferItems.Single(t => t.ItemId == item.ItemId).Quantity; if (item.Quantity < 0) { return(BadRequest(new { message = "Quantity cannot be less than zero", item })); } _locationItemRepository.Update(item); } var newLocationItems = entity.TransferItems.Where(t => newLocationItemIds.Contains(t.ItemId)) .Select(t => new LocationItem() { ItemId = t.ItemId, Quantity = t.Quantity, LocationId = entity.DestinationLocationId }); _locationItemRepository.AddRange(newLocationItems); try { await _unitOfWork.SaveAsync(); var createdResult = CreatedAtAction("GetTransferById", new { id = entity.Id }, entity.Id); createdResult.StatusCode = 200; return(createdResult); } catch (Exception e) { throw new Exception("An unexpected error occured. Could not be added.", e); } }