Пример #1
0
        public ActionResult Edit(ProductImageVm product)
        {
            if (ModelState.IsValid)
            {
                var p = db.Products.Where(pr => pr.ProductID == product.ProductID).FirstOrDefault();
                p.ProductName     = product.ProductName;
                p.Unit            = product.Unit;
                p.SubCategoryID   = product.SubCategoryID;
                db.Entry(p).State = EntityState.Modified;
                db.SaveChanges();

                var    img       = db.ImageAlbums.Where(i => i.ProductID == product.ProductID).FirstOrDefault();
                var    oldImg    = img.ImageUrl;
                string filename  = Path.GetFileNameWithoutExtension(product.ImageUpload.FileName);
                string extension = Path.GetExtension(product.ImageUpload.FileName);
                filename     = filename + "_" + DateTime.Now.ToString("yymmssfff") + extension;
                img.ImageUrl = filename;
                System.IO.File.Delete(Path.Combine(Server.MapPath("~/App_File/ProductImage"), oldImg));
                product.ImageUpload.SaveAs(Path.Combine(Server.MapPath("~/App_File/ProductImage"), filename));
                img.ProductID       = product.ProductID;
                db.Entry(img).State = EntityState.Modified;
                db.SaveChanges();

                return(RedirectToAction("Index"));
            }
            return(View(product));
        }
        public async Task <IHttpActionResult> PutProductImage(int id, ProductImageVm model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != model.ImageId)
            {
                return(BadRequest());
            }

            _db.Entry(model).State = EntityState.Modified;

            try
            {
                await _db.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ProductImageExists(id))
                {
                    return(NotFound());
                }
                throw;
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
Пример #3
0
        // GET: Product/Edit/5
        public ActionResult Edit(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            var product = (from p in db.Products
                           join i in db.ImageAlbums
                           on p.ProductID equals i.ProductID
                           where i.ProductID == id
                           select new
            {
                id = p.ProductID,
                name = p.ProductName,
                imgid = i.ImageAlbumID,
                image = i.ImageUrl,
                punit = p.Unit,
                price = p.Price,
                otherPrice = p.OtherPrice,
                discount = p.Discount,
                vat = p.Vat,
                sid = p.SubCategoryID,
                subname = p.SubCategory.SubCategoryName,
                category = p.SubCategory.Category.CategoryID,
                catname = p.SubCategory.Category.CategoryName,
                brandid = p.Brand.BrandID,
                brand = p.Brand.BrandName
            }).FirstOrDefault();

            ProductImageVm pimg = new ProductImageVm();

            pimg.ProductID       = product.id;
            pimg.ImageAlbumID    = product.imgid;
            pimg.ProductName     = product.name;
            pimg.ImageUrl        = product.image;
            pimg.Unit            = product.punit;
            pimg.Price           = product.price;
            pimg.OtherPrice      = product.otherPrice;
            pimg.Discount        = product.discount;
            pimg.Vat             = product.vat;
            pimg.BrandID         = product.brandid;
            pimg.BrandName       = product.brand;
            pimg.SubCategoryID   = product.sid;
            pimg.SubCategoryName = product.subname;
            pimg.CategoryName    = product.catname;
            pimg.CategoryID      = product.category;

            if (product == null)
            {
                return(HttpNotFound());
            }
            return(View(pimg));
        }
        public async Task <IHttpActionResult> PostProductImage(ProductImageVm model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            _db.ProductImage.Add(model);
            await _db.SaveChangesAsync();

            var ret = await _db.ProductImage.Select <ProductImage, ProductImageVm>(ProductImageVm.Select).FirstOrDefaultAsync(x => x.ImageId == model.ImageId);

            return(CreatedAtRoute("ShoppingApi", new { id = model.ImageId }, model));
        }
Пример #5
0
        // GET: Product
        public ActionResult Index()
        {
            List <ProductImageVm> pivm = new List <ProductImageVm>();
            var product = (from p in db.Products
                           join i in db.ImageAlbums
                           on p.ProductID equals i.ProductID
                           select new
            {
                id = p.ProductID,
                name = p.ProductName,
                imgid = i.ImageAlbumID,
                image = i.ImageUrl,
                punit = p.Unit,
                price = p.Price,
                otherPrice = p.OtherPrice,
                discount = p.Discount,
                vat = p.Vat,
                sid = p.SubCategoryID,
                subname = p.SubCategory.SubCategoryName,
                category = p.SubCategory.Category.CategoryID,
                catname = p.SubCategory.Category.CategoryName,
                brandid = p.Brand.BrandID,
                brand = p.Brand.BrandName
            });


            foreach (var item in product)
            {
                ProductImageVm pimg = new ProductImageVm();
                pimg.ProductID       = item.id;
                pimg.ImageAlbumID    = item.imgid;
                pimg.ProductName     = item.name;
                pimg.ImageUrl        = item.image;
                pimg.Unit            = item.punit;
                pimg.Price           = item.price;
                pimg.OtherPrice      = item.otherPrice;
                pimg.Discount        = item.discount;
                pimg.Vat             = item.vat;
                pimg.BrandID         = item.brandid;
                pimg.BrandName       = item.brand;
                pimg.SubCategoryID   = item.sid;
                pimg.SubCategoryName = item.subname;
                pimg.CategoryName    = item.catname;
                pimg.CategoryID      = item.category;
                pivm.Add(pimg);
            }
            return(View(pivm));
        }
Пример #6
0
        public async Task <ProductImageVm> GetImageById(int imageId)
        {
            var image = await _context.ProductImages.FindAsync(imageId);

            if (image == null)
            {
                throw new EShopeException($"Cannot find an image with id:{imageId}");
            }
            var viewModel = new ProductImageVm()
            {
                Caption     = image.Caption,
                DateCreated = image.DateCreated,
                FileSize    = image.FileSize,
                Id          = image.Id,
                ImagePath   = image.ImagePath,
                Isdefault   = image.Isdefault,
                ProductId   = image.ProductId,
                SortOrder   = image.SortOrder
            };

            return(viewModel);
        }
Пример #7
0
        public async Task <ProductImageVm> GetImageById(int imageId)
        {
            var sessions = _httpContextAccessor
                           .HttpContext
                           .Session
                           .GetString(SystemConstants.AppSettings.Token);

            var client = _httpClientFactory.CreateClient();

            client.BaseAddress = new Uri(_configuration[SystemConstants.AppSettings.BaseAddress]);
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", sessions);
            var response = await client.GetAsync($"/api/products/images/{imageId}");

            var body = await response.Content.ReadAsStringAsync();

            if (response.IsSuccessStatusCode)
            {
                ProductImageVm myDeserializedObjList = (ProductImageVm)JsonConvert.DeserializeObject(body, typeof(ProductImageVm));

                return(myDeserializedObjList);
            }
            return(JsonConvert.DeserializeObject <ProductImageVm>(body));
        }