public async Task <ActionResult> UpdateProduct(int id, [FromBody] NewProductDTO newProduct) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } Category category = _context.Categories .SingleOrDefault(c => c.Name == newProduct.Subcategory || c.ParentCategory.Name == newProduct.Category); if (category == null) { return(new BadRequestObjectResult("The provided category and sub category doesnt exist")); } City city = _context.Cities.SingleOrDefault(c => c.Name == newProduct.City); if (city == null) { return(new BadRequestObjectResult("The provided city doesnt exist")); } var product = new Product() { ProductId = id, Category = category, Title = newProduct.Title, Description = newProduct.Description, City = city, PublishDate = DateTime.UtcNow }; _context.Products.Update(product); await _context.SaveChangesAsync(); return(Ok()); }
public async Task <ActionResult <ProductDTO> > AddNewProduct([FromBody] NewProductDTO newProductDTO) { if (!ModelState.IsValid) { return(new BadRequestResult());//(ModelState); } Category category = _context.Categories .Include(c => c.ParentCategory) .SingleOrDefault(c => c.Name == newProductDTO.SubCategory || c.ParentCategory.Name == newProductDTO.Category); if (category == null) { return(new BadRequestObjectResult("The provided category and sub category doesnt exist")); } City city = _context.Cities.SingleOrDefault(c => c.Name == newProductDTO.City); if (city == null) { return(new BadRequestObjectResult("The provided category and sub category doesnt exist")); } User owner = await _context.Users.FindAsync(User.Identity.Name); var product = new Product() { Owner = owner, Title = newProductDTO.Title, Description = newProductDTO.Description, Category = category, City = city, Media = null, PublishDate = DateTime.Now }; _context.Products.Add(product); await _context.SaveChangesAsync(); return(CreatedAtAction( nameof(GetProduct), new { productId = product.ProductId }, _productsMapper.Map <ProductDTO>(product) )); }
public void AddNewProduct([FromBody] NewProductDTO newProduct) { }