public IActionResult BlockProductCreation(int id) { if (_supermarketRepository.ProductExists(id)) { return(new StatusCodeResult(StatusCodes.Status409Conflict)); } return(NotFound()); }
public IActionResult CreateSupermarketStock([FromBody] SupermarketStockForCreationDTO stock) { // check a new stock was passed in if (stock == null) { return(BadRequest()); } // Validate data if (!ModelState.IsValid || !_supermarketRepository.SupermarketExists(stock.SupermarketId) || !_supermarketRepository.ProductExists(stock.ProductId)) { // return 422 return(new UnprocessableEntityObjectResult(ModelState)); } // make sure productId and supermarketId don't match an existing entry if (_supermarketRepository.SupermarketStockExists(stock.ProductId, stock.SupermarketId)) { // return 409 return(StatusCode(409)); } // map stock var stockEntity = Mapper.Map <SupermarketStock>(stock); // add stock and save _supermarketRepository.AddSupermarketStock(stockEntity); if (!_supermarketRepository.Save()) { throw new Exception("Creating stock failed on Save."); } var stockToReturn = Mapper.Map <SupermarketStockDTO>(stockEntity); var links = CreateLinksForSupermarketStock(stockToReturn.Id, null); var linkedResourceToReturn = stockToReturn.ShapeData(null) as IDictionary <string, object>; linkedResourceToReturn.Add("links", links); return(CreatedAtRoute("GetStock", new { id = linkedResourceToReturn["Id"] }, linkedResourceToReturn)); }
public IActionResult CreateSupermarketStockCollection([FromBody] IEnumerable <SupermarketStockForCreationDTO> supermarketStockCollection) { if (supermarketStockCollection == null) { return(BadRequest()); } // Validate data foreach (SupermarketStockForCreationDTO s in supermarketStockCollection) { if (!ModelState.IsValid || !_supermarketRepository.SupermarketExists(s.SupermarketId) || !_supermarketRepository.ProductExists(s.ProductId)) { // return 422 return(new UnprocessableEntityObjectResult(ModelState)); } // make sure productId and supermarketId don't match an existing entry if (_supermarketRepository.SupermarketStockExists(s.ProductId, s.SupermarketId)) { // return 409 return(StatusCode(409)); } } var supermarketStockEntities = Mapper.Map <IEnumerable <SupermarketStock> >(supermarketStockCollection); foreach (var supermarketStock in supermarketStockEntities) { _supermarketRepository.AddSupermarketStock(supermarketStock); } if (!_supermarketRepository.Save()) { throw new Exception("Creating a supermarket stock collection failed on save."); } var supermarketStockCollectionToReturn = Mapper.Map <IEnumerable <SupermarketStockDTO> >(supermarketStockEntities); var idsAsString = string.Join(",", supermarketStockCollectionToReturn.Select(s => s.Id)); return(CreatedAtRoute("GetSupermarketStockCollection", new { ids = idsAsString }, supermarketStockCollectionToReturn)); }