public IEnumerable <ProductDetailsController> GetProductImage() { Product_Images product_image = new Product_Images(); var productImage = db.Database.SqlQuery <ProductDetailsController>("SELECT Product.ProductID,Product.ProductName,Product.ProductDescription,Product.ProductCategory,Product.ProductPrice, Images.Picture FROM Product INNER JOIN Images ON Product.ProductID = Images.ProductID ORDER BY Product.ProductName ASC"); return(productImage); }
public IHttpActionResult Post() { try { if (!Request.Content.IsMimeMultipartContent()) { return(BadRequest("Not Mime Multipart Content")); } var request = HttpContext.Current.Request; var product_image = new Product_Images { }; if (request.Files.Count > 0) { for (int i = 0; i < request.Files.Count; i++) { HttpPostedFile file = request.Files[i]; using (MemoryStream ms = new MemoryStream()) { file.InputStream.CopyTo(ms); byte[] array = ms.GetBuffer(); product_image = new Product_Images { Product_ID = Convert.ToInt32(HttpContext.Current.Request.Params["productId"]), Product_Image = ms.ToArray() }; db.Product_Images.Add(product_image); db.SaveChanges(); } } } return(Ok(product_image)); } catch (Exception ex) { return(InternalServerError(ex)); } }
public ActionResult Detail_Image_Add(Product_Images model, IEnumerable <HttpPostedFileBase> FileUp) { var curent_item = new Product_Images(); if (model.Id > 0) { curent_item = Db.Where <Product_Images>(m => m.Id == model.Id).FirstOrDefault(); if (curent_item == null) { return(JsonError("Please dont try to hack us")); } } else { curent_item.CreatedBy = AuthenticatedUserID; curent_item.CreatedOn = DateTime.Now; } curent_item.ProductId = model.ProductId; curent_item.Name = model.Name; curent_item.Status = model.Status; if (FileUp != null && FileUp.FirstOrDefault() != null) { curent_item.Filename = UploadFile(AuthenticatedUserID, model.ProductId.ToString(), "ProductImage", FileUp); } if (model.Id > 0) { Db.Update <Product_Images>(curent_item); } else { Db.Insert <Product_Images>(curent_item); } return(RedirectToAction("Detail", new { id = model.ProductId })); }
public async Task <ActionResult> ChangeProduct(ProductChangeSubmitModel data) { var productIds = db.Products.Select(p => p.Id).ToList(); List <Product_Sizes> sizes = JsonConvert.DeserializeObject <List <Product_Sizes> >(data.Sizes); if (!productIds.Contains(data.Id))//create new product if product doesn't exist { var product = new Product() { Name = data.Name, Description = data.Description, Status = true }; try { db.Products.Add(product); await db.SaveChangesAsync(); foreach (var s in sizes) { s.ProductId = product.Id; db.Product_Sizes.Add(s); } foreach (var i in data.Files) { var image = new Product_Images() { ProductId = product.Id, Image = ConvertToByte(i) }; db.Product_Images.Add(image); } await db.SaveChangesAsync(); return(new HttpStatusCodeResult(200, "Successfully added Product")); } catch (Exception ex) { return(new HttpStatusCodeResult(500, ex.Message)); } } else//modified product { var product = db.Products.Where(p => p.Id == data.Id).SingleOrDefault(); product.Name = data.Name; product.Description = data.Description; try { await db.SaveChangesAsync(); foreach (var s in sizes) { s.ProductId = product.Id; db.Product_Sizes.Add(s); } foreach (var i in data.Files) { var image = new Product_Images() { ProductId = product.Id, Image = ConvertToByte(i) }; db.Product_Images.Add(image); } await db.SaveChangesAsync(); return(new HttpStatusCodeResult(200, "Successfully modified Product")); } catch (Exception ex) { return(new HttpStatusCodeResult(500, ex.Message)); } } }