public IActionResult AddProduct([FromBody] AddProductModel model) { var images = new List <Image>(); images.Add(new Image { ImageUrl = model.Image }); var product = new DAL.Entities.Product { Name = model.Name, Price = decimal.Parse(model.Price), Description = model.Description, SexId = int.Parse(model.Sex), TypeId = int.Parse(model.Type), Images = images }; _context.Products.Add(product); _context.SaveChanges(); return(Ok()); }
public async Task <IActionResult> DeleteProduct(int id) { try { DAL.Entities.Product productToDelete = await _repo.GetProductAsync(id); if (productToDelete == null) { return(NotFound()); } _repo.DeleteProduct(productToDelete); if (!await _repo.SaveChangesAsync()) { return(StatusCode(500, "A problem occurred while handling your request.")); } return(NoContent()); } catch (Exception ex) { _logger.LogCritical("error message here", ex); return(StatusCode(500, "error message here")); } }
public async Task <IActionResult> UpdateProductPartial(int id, [FromBody] JsonPatchDocument <DTO.Update.Product> patchDocument) { try { if (patchDocument == null) { return(BadRequest()); } DAL.Entities.Product productEntity = await _repo.GetProductAsync(id); if (productEntity == null) { return(NotFound()); } DTO.Update.Product productToUpdate = Mapper.Map <DTO.Update.Product>(productEntity); patchDocument.ApplyTo(productToUpdate, ModelState); if (!ModelState.IsValid) { return(BadRequest(ModelState)); } //TODO: consider fluent validations: github: JeremySkinner/FluentValidation if (productToUpdate.DescriptionShort == productToUpdate.DescriptionLong) { ModelState.AddModelError("DescriptionShort", "The provided descriptions should be different."); } //TODO: check that category is valid TryValidateModel(productToUpdate); if (!ModelState.IsValid) { return(BadRequest(ModelState)); } Mapper.Map(productToUpdate, productEntity); if (!await _repo.SaveChangesAsync()) { return(StatusCode(500, "A problem happened while handling your request.")); } return(NoContent()); } catch (Exception ex) { _logger.LogCritical("error message here", ex); return(StatusCode(500, "error message here")); } }
public async Task <IActionResult> GetProduct(int id) { try { DAL.Entities.Product productEntity = await _repo.GetProductAsync(id); if (productEntity == null) { return(NotFound()); } var productImages = await _repo.GetProductImagesAsync(id); return(Ok((productEntity, productImages))); } catch (Exception ex) { _logger.LogCritical("error message here", ex); return(StatusCode(500, "error message here")); } }
public async Task <IActionResult> CreateProduct([FromBody] DTO.Create.Product product) { try { if (product == null) { return(BadRequest()); } //TODO: consider fluent validations: github: JeremySkinner/FluentValidation if (product.DescriptionShort == product.DescriptionLong) { ModelState.AddModelError("DescriptionShort", "The provided descriptions should be different."); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } //TODO: check that category is valid DAL.Entities.Product productEntity = _mapper.Map <DAL.Entities.Product>(product); _repo.AddProduct(productEntity); await _repo.SaveChangesAsync(); await _repo.GetProductAsync(productEntity.Id); //refetch so that category is populated return(CreatedAtRoute("GetProduct", new { id = productEntity.Id }, productEntity)); } catch (Exception ex) { _logger.LogCritical("error message here", ex); return(StatusCode(500, "error message here")); } }