Exemple #1
0
        public IHttpActionResult AddPhoto(int Product_ID)
        {
            string Token = HttpContext.Current.Request.Form["Token"];
            string Photo = HttpContext.Current.Request.Form["Photo"];

            //Find Product
            Product product = db.Products.SingleOrDefault(x => x.ID == Product_ID && x.Provider.Token == Token);

            if (product == null)
            {
                return(Ok(new { key = false, Message = "هذا المنتج غير موجود" }));
            }
            //Check Photo Is Found
            if (string.IsNullOrEmpty(Photo))
            {
                return(Ok(new { key = false, Message = "من فضلك قم باختيار صوورة" }));
            }
            //Add Photo To DB
            Product_Photos photo = new Product_Photos {
                Product_ID = product.ID
            };

            db.Product_Photos.Add(photo);
            db.SaveChanges();
            //Save Photo
            byte[] PhotoBase64 = Convert.FromBase64String(Photo);
            File.WriteAllBytes(HttpContext.Current.Server.MapPath("~/Uploads/Products/" + photo.ID + ".jpg"), PhotoBase64);
            return(Ok(new { key = true, Message = "تم اضافة الصورة بنجاح" }));
        }
Exemple #2
0
        public IHttpActionResult DeletePhoto(int ID)
        {
            string Token = HttpContext.Current.Request.Form["Token"];
            //Find Photo
            Product_Photos photo = db.Product_Photos.SingleOrDefault(x => x.Product.Provider.Token == Token && x.ID == ID);

            if (photo == null)
            {
                return(Ok(new { key = false, Message = "هذه الصورة غير موجودة" }));
            }
            //Delete Photo From DB
            db.Product_Photos.Remove(photo);
            db.SaveChanges();
            FileInfo F = new FileInfo(HttpContext.Current.Server.MapPath("~/Uploads/Products/" + ID + ".jpg"));

            if (F.Exists)
            {
                F.Delete();
            }
            return(Ok(new { key = true, Message = "تم مسح الصورة بنجاح" }));
        }