Ejemplo n.º 1
0
        public async Task <IActionResult> Create()
        {
            var vm   = new PhoneCreateViewModel();
            var list = await companyAsyncRepository.GetAllAsyn();

            vm.Companies = new SelectList(list, "Id", "Name");

            return(View(vm));
        }
Ejemplo n.º 2
0
        private string ProcessUploadedFile(PhoneCreateViewModel model)
        {
            string uniqueFileName = null;

            if (model.Photo != null)
            {
                string uploadsFolder = Path.Combine(hostingEnvironment.WebRootPath, "images");

                uniqueFileName = Guid.NewGuid().ToString() + "_" + model.Photo.FileName;
                string filePath = Path.Combine(uploadsFolder, uniqueFileName);
                using (var fileStream = new FileStream(filePath, FileMode.Create))
                {
                    model.Photo.CopyTo(fileStream);
                }
            }

            return(uniqueFileName);
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> Edit(int?id)
        {
            if (id.HasValue)
            {
                var p = phoneAsyncRepository.FindById(id.Value);
                if (p != null)
                {
                    var vm = new PhoneCreateViewModel {
                        Id = p.Id, Name = p.Name, Price = p.Price, CompanyId = p.CompanyId
                    };
                    var cs = await companyAsyncRepository.GetAllAsyn();

                    vm.Companies = new SelectList(cs, "Id", "Name");
                    var selected = vm.Companies.Where(x => x.Value == vm.CompanyId.ToString()).First();
                    selected.Selected = true;
                    return(View(vm));
                }
            }
            return(NotFound());
        }
Ejemplo n.º 4
0
        public IActionResult Create(PhoneCreateViewModel model)
        {
            if (ModelState.IsValid)
            {
                string uniqueFileName = ProcessUploadedFile(model);

                Phone newphone = new Phone
                {
                    Name      = model.Name,
                    Color     = model.Color,
                    Type      = model.Type,
                    Dimension = model.Dimension,
                    Price     = model.Price,
                    PhotoPath = uniqueFileName
                };
                _phoneRepository.Add(newphone);
                return(RedirectToAction("details", new { id = newphone.Id }));
            }
            return(View());
        }
Ejemplo n.º 5
0
        public async Task <IActionResult> Edit(PhoneCreateViewModel phone)
        {
            if (phone != null)
            {
                if (ModelState.IsValid)
                {
                    var p = new Phone {
                        Id = phone.Id, CompanyId = phone.CompanyId, Name = phone.Name, Price = phone.Price
                    };

                    await phoneAsyncRepository.UpdateAsyn(p, p.Id);

                    logger.LogInformation($"Was updated product with id {p.Id}");
                    return(RedirectToAction("Index"));
                }
                else
                {
                    return(View(phone));
                }
            }
            return(NotFound());
        }