Esempio n. 1
0
        // GET: Jewelry/Details/5
        public async Task <IActionResult> Details(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var jewelry = await _context.Jewelry
                          .Include(x => x.Materials)
                          .FirstOrDefaultAsync(m => m.ProductId == id);

            var jewelryViewModel = new JewelryViewModel()
            {
                JewelryId     = jewelry.ProductId,
                Title         = jewelry.Title,
                Type          = jewelry.Type,
                Color         = jewelry.Color,
                Size          = jewelry.Size,
                Price         = jewelry.Price,
                ExistingImage = jewelry.JewelryImage,
                Materials     = jewelry.Materials.ToDynamicList(m => new MaterialViewModel()
                {
                    MaterialId = m.MaterialId,
                    Title      = m.Title,
                    Category   = m.Category
                })
            };

            if (jewelry == null)
            {
                return(NotFound());
            }

            return(View(jewelryViewModel));
        }
Esempio n. 2
0
        public async Task <IActionResult> Create(JewelryViewModel jewelry)
        {
            if (ModelState.IsValid)
            {
                string uniqueFileName = UploadedFile(jewelry);

                Jewelry jewel = new Jewelry()
                {
                    Title        = jewelry.Title,
                    Type         = jewelry.Type,
                    Size         = jewelry.Size,
                    Price        = jewelry.Price,
                    JewelryImage = uniqueFileName,
                    Materials    = jewelry.Materials.ToModel(m => new Material
                    {
                        MaterialId = m.MaterialId,
                        Title      = m.Title,
                        Category   = m.Category
                    }).ToList()
                };
                _context.Add(jewel);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Jewelry)));
            }
            return(View());
        }
Esempio n. 3
0
        private string UploadedFile(JewelryViewModel jewelry)
        {
            string uniqueFileName = null;

            if (jewelry.ProductImage != null)
            {
                string uploadsFolder = Path.Combine(webHostEnvironment.WebRootPath, "images");
                uniqueFileName = Guid.NewGuid().ToString() + "_" + jewelry.ProductImage.FileName;
                string filePath = Path.Combine(uploadsFolder, uniqueFileName);
                using (var fileStream = new FileStream(filePath, FileMode.Create))
                {
                    jewelry.ProductImage.CopyTo(fileStream);
                }
            }
            return(uniqueFileName);
        }
Esempio n. 4
0
        public async Task <IActionResult> Edit(int id, JewelryViewModel jewelry)
        {
            if (id != jewelry.JewelryId)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                string uniqueFileName = UploadedFile(jewelry);
                var    jewelryModel   = new Jewelry();

                jewelryModel.ProductId    = jewelry.JewelryId;
                jewelryModel.Title        = jewelry.Title;
                jewelryModel.Type         = jewelry.Type;
                jewelryModel.Color        = jewelry.Color;
                jewelryModel.Size         = jewelry.Size;
                jewelryModel.Price        = jewelry.Price;
                jewelryModel.JewelryImage = uniqueFileName;
                jewelryModel.Materials    = jewelry.Materials.ToModel(m => new Material
                {
                    MaterialId = m.MaterialId,
                    Title      = m.Title,
                    Category   = m.Category
                }).ToList();
                if (jewelry.ProductImage == null)
                {
                    jewelryModel.JewelryImage = uniqueFileName;
                }
                else if (jewelry.ProductImage != null)
                {
                    if (jewelry.ExistingImage != null)
                    {
                        string filePath = Path.Combine(webHostEnvironment.WebRootPath, "images", jewelry.ExistingImage);
                        System.IO.File.Delete(filePath);
                    }

                    jewelryModel.JewelryImage = UploadedFile(jewelry);
                }
                _context.Update(jewelryModel);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Jewelry)));
            }
            return(View(jewelry));
        }
Esempio n. 5
0
 private static Jewelry ViewModelToModel(JewelryViewModel viewModel)
 {
     return(new Jewelry()
     {
         ProductId = viewModel.JewelryId,
         Title = viewModel.Title,
         Type = viewModel.Type,
         Color = viewModel.Color,
         Size = viewModel.Size,
         Price = viewModel.Price,
         Materials = viewModel.Materials.ToModel(m => new Material
         {
             MaterialId = m.MaterialId,
             Title = m.Title,
             Category = m.Category
         }).ToList()
     });
 }
Esempio n. 6
0
        private static JewelryViewModel ModelToViewModel(Jewelry model)
        {
            var jewelryViewModel = new JewelryViewModel()
            {
                JewelryId = model.ProductId,
                Title     = model.Title,
                Type      = model.Type,
                Color     = model.Color,
                Size      = model.Size,
                Price     = model.Price,
                Materials = model.Materials.ToDynamicList(m => new MaterialViewModel()
                {
                    MaterialId = m.MaterialId,
                    Title      = m.Title,
                    Category   = m.Category
                })
            };

            return(jewelryViewModel);
        }