Ejemplo n.º 1
0
        public async Task <IActionResult> Create(HamperCreateViewModel vm, IFormFile file)
        {
            if (ModelState.IsValid)
            {
                var user = await _UserManager.FindByNameAsync(User.Identity.Name);

                Hamper h = new Hamper
                {
                    Active            = true,
                    CategoryId        = vm.CategoryId,
                    HamperDescription = vm.HamperDescription,
                    HamperName        = vm.HamperName,
                    HamperPrice       = vm.HamperPrice,
                };

                if (file != null)
                {
                    var    uploadPath = Path.Combine(environment.WebRootPath, "uploads/hampers/");
                    string extension  = Path.GetExtension(file.FileName);
                    string fileName   = StringFormatHelper.ToTitleCase(h.HamperName) + "-1" + extension;
                    using (var fileStream = new FileStream(Path.Combine(uploadPath, fileName), FileMode.Create))
                    {
                        await file.CopyToAsync(fileStream);
                    }
                    h.Picture = fileName;
                }
                _hamperManager.Add(h);
                return(RedirectToAction("Index"));
            }
            IEnumerable <Category> categories = _categoryManager.GetAll();

            vm.Categories = categories;
            return(View(vm));
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> Update(HamperUpdateViewModel vm)
        {
            Hamper h = _hamperManager.Read(vm.HamperId);

            if (h == null)
            {
                return(NotFound());
            }
            if (vm.File != null)
            {
                var    uploadPath = Path.Combine(environment.WebRootPath, "uploads/hampers/");
                string extension  = Path.GetExtension(vm.File.FileName);
                string fileName   = StringFormatHelper.ToTitleCase(h.HamperName) + "-1" + extension;
                using (var fileStream = new FileStream(Path.Combine(uploadPath, fileName), FileMode.Create))
                {
                    await vm.File.CopyToAsync(fileStream);
                }
                h.Picture = fileName;
            }
            h.HamperDescription = vm.HamperDescription;
            h.HamperName        = vm.HamperName;
            h.HamperPrice       = vm.HamperPrice;
            h.CategoryId        = vm.CategoryId;
            h.Active            = !vm.Retired;

            _hamperManager.Update(h);

            return(Redirect(vm.ReturnUrl));
        }