public async Task <IHttpActionResult> PostProduct(ProductIn productIn) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } CurrentIdentity identity = getIdentity(); Shop shopToCheck = await db.Shops.FindAsync(productIn.ShopId); Product productsToCheck = await db.Products.FindAsync(productIn.ShopId); if (shopToCheck.UserId != identity.userId) { if (identity.role != "Admin") { return(ResponseMessage(getHttpResponse(HttpStatusCode.Forbidden))); } } if (ProductTitleExistsInShop(productIn.Title, productIn.ShopId)) { return(ResponseMessage(getHttpResponse(HttpStatusCode.Conflict))); } Product newProduct = new Product(); newProduct.Title = productIn.Title; newProduct.Description = productIn.Description; newProduct.DescriptionFull = productIn.DescriptionFull; newProduct.Views = 0; newProduct.IsActive = 0; DateTime now = DateTime.Now; newProduct.CreatedAt = now; newProduct.UpdatedAt = now; newProduct.ShopId = productIn.ShopId; newProduct.Stock = productIn.Stock; newProduct.Price = productIn.Price; db.Products.Add(newProduct); try { await db.SaveChangesAsync(); } catch (DbUpdateException) { if (ProductExists(newProduct.Id)) { return(ResponseMessage(getHttpResponse(HttpStatusCode.Conflict))); } else { throw; } } if (productIn.Images != null) { foreach (var image in productIn.Images) { Image newImage = new Image(); newImage.ProductId = newProduct.Id; newImage.ImageUrl = image; db.Images.Add(newImage); } try { await db.SaveChangesAsync(); } catch (DbUpdateException) { throw; } } if (productIn.CategoryId != null) { foreach (var category in productIn.CategoryId) { ProductsToCategory newCategory = new ProductsToCategory(); newCategory.ProductId = newProduct.Id; newCategory.CategoryId = category; db.ProductsToCategories.Add(newCategory); } try { await db.SaveChangesAsync(); } catch (DbUpdateException) { throw; } } return(CreatedAtRoute("WSApi", new { id = newProduct.Id }, newProduct)); }
public async Task <IHttpActionResult> PutProduct(long id, ProductUpdate productIn) { CurrentIdentity identity = getIdentity(); if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (productIn.IsActive != 0 && productIn.IsActive != 1) { return(ResponseMessage(getHttpResponse(HttpStatusCode.BadRequest))); } Product productCurrent = await db.Products.FindAsync(id); if (productCurrent == null) { return(ResponseMessage(getHttpResponse(HttpStatusCode.NotFound))); } string userId = (await db.Shops.FindAsync(productCurrent.ShopId)).UserId; if (identity.userId == userId || identity.role == "Admin") { if (productCurrent.Title != productIn.Title) { if (ProductTitleExistsInShop(productIn.Title, (long)productCurrent.ShopId)) { return(ResponseMessage(getHttpResponse(HttpStatusCode.Conflict))); } } productCurrent.Title = (productIn.Title != null) ? productIn.Title : productCurrent.Title; productCurrent.Description = (productIn.Description != null) ? productIn.Description : productCurrent.Description; productCurrent.DescriptionFull = (productIn.DescriptionFull != null) ? productIn.DescriptionFull : productCurrent.DescriptionFull; productCurrent.UpdatedAt = DateTime.Now; productCurrent.IsActive = productIn.IsActive; productCurrent.Stock = productIn.Stock; productCurrent.Price = (productIn.Price != null) ? productIn.Price : productCurrent.Price; db.Entry(productCurrent).State = EntityState.Modified; try { await db.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ProductExists(id)) { return(ResponseMessage(getHttpResponse(HttpStatusCode.NotFound))); } else { throw; } } IList <Image> currentImages = await db.Images.Where(p => p.ProductId == id).ToListAsync(); foreach (var image in currentImages) //TODO: Kinda hackish. Should check if images already excists instead of deleting them right off the bat. { db.Images.Remove(image); } if (productIn.Images != null) { foreach (var image in productIn.Images) { Image newImage = new Image(); newImage.ProductId = id; newImage.ImageUrl = image; db.Images.Add(newImage); } try { await db.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { throw; } } IList <ProductsToCategory> currentCategories = await db.ProductsToCategories.Where(p => p.ProductId == id).ToListAsync(); foreach (var category in currentCategories) //TODO: Kinda hackish. Should check if categories are already applied instead of deleting them right off the bat. { db.ProductsToCategories.Remove(category); } if (productIn.CategoryId != null) { foreach (var categoryIn in productIn.CategoryId) { ProductsToCategory newCategory = new ProductsToCategory(); newCategory.ProductId = id; newCategory.CategoryId = categoryIn; db.ProductsToCategories.Add(newCategory); } } try { await db.SaveChangesAsync(); } catch (DbUpdateException) { throw; } return(StatusCode(HttpStatusCode.NoContent)); } return(ResponseMessage(getHttpResponse(HttpStatusCode.Unauthorized))); }