Пример #1
0
        public ActionResult Edit(DrugstoreViewModel drugstoreViewModel)
        {
            drugstoreViewModel.ListItems = db.Citys.ToList().ConvertAll(
                a =>
            {
                return(new SelectListItem()
                {
                    Text = a.Name,
                    Value = a.Id.ToString(),
                    Selected = false
                });
            });
            var item = db.Entry <Drugstore>(drugstoreViewModel.Drugstore);

            item.State = EntityState.Modified;
            item.Collection(i => i.Products).Load();
            drugstoreViewModel.Drugstore.Products.Clear();

            foreach (var id in drugstoreViewModel._selectedDrugstoreProduct)
            {
                drugstoreViewModel.Drugstore.Products.Add(db.Products.First(i => i.Id == id));
            }

            if (ModelState.IsValid)
            {
                db.Entry(drugstoreViewModel.Drugstore).State = EntityState.Modified;
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(drugstoreViewModel));
        }
Пример #2
0
        public async Task <DrugstoreViewModel> Add(DrugstoreViewModel drugstore)
        {
            if (await _contextDrugstore.CheckIfExists(drugstore.Name))
            {
                throw new Exception("Farmácia já cadastrada!");
            }
            await _contextDrugstore
            .Add(new Drugstore(
                     Guid.NewGuid(),
                     drugstore.Name,
                     drugstore.RoundTheClock,
                     drugstore.FoundationDate,
                     drugstore.NeighborhoodId,
                     drugstore.Neighborhood)
                 );

            return(drugstore);
        }
Пример #3
0
        // GET: Drugstore/Edit/5

        /*public ActionResult Edit(int? id)
         * {
         *  if (id == null)
         *  {
         *      return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
         *  }
         *  Drugstore drugstore = db.Drugstores.Find(id);
         *  if (drugstore == null)
         *  {
         *      return HttpNotFound();
         *  }
         *  ViewBag.CityId = new SelectList(db.Citys, "Id", "Name", drugstore.CityId);
         *  return View(drugstore);
         * }
         */
        public ActionResult Edit(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            var drugstoreViewModel = new DrugstoreViewModel
            {
                Drugstore = db.Drugstores.Include(i => i.Products).First(i => i.Id == id),
            };

            if (drugstoreViewModel.Drugstore == null)
            {
                return(HttpNotFound());
            }
            var drugstoreProduct = db.Products.ToList();

            drugstoreViewModel.DrugstoresProducts = drugstoreProduct.Select(o => new SelectListItem
            {
                Text  = o.Name,
                Value = o.Id.ToString()
            });
            Drugstore drugstore = db.Drugstores.Find(id);

            if (drugstore == null)
            {
                return(HttpNotFound());
            }
            drugstoreViewModel.ListItems = db.Citys.ToList().ConvertAll(
                a =>
            {
                return(new SelectListItem()
                {
                    Text = a.Name,
                    Value = a.Id.ToString(),
                    Selected = false
                });
            });
            return(View(drugstoreViewModel));
        }
Пример #4
0
        public async Task <DrugstoreViewModel> Update(Guid id, DrugstoreViewModel drugstoreViewModel)
        {
            var drugstore = await _contextDrugstore.GetById(id);

            if (drugstore == null)
            {
                throw new Exception("Farmácia não encontrada!");
            }
            if (await _contextDrugstore.CheckIfExists(drugstoreViewModel.Name))
            {
                throw new Exception("Farmácia já existe!");
            }
            await _contextDrugstore.Update(new Drugstore(
                                               id,
                                               drugstoreViewModel.Name,
                                               drugstoreViewModel.RoundTheClock,
                                               drugstoreViewModel.FoundationDate,
                                               drugstoreViewModel.NeighborhoodId,
                                               drugstoreViewModel.Neighborhood));

            return(drugstoreViewModel);
        }