コード例 #1
0
        public ActionResult Create(PetFormViewModel viewModel)
        {
            if (!ModelState.IsValid)
            {
                viewModel.animalTypes = _unitOfWork.AnimalType.GetAnimalType();
                return(View("PetForm", viewModel));
            }

            var pet = new Pet
            {
                OwnerName = viewModel.OwnerName,
                Phone     = viewModel.Phone,
                Address   = viewModel.Address,
                DateTime  = DateTime.Now,
                Age       = viewModel.Age,
                // Height = viewModel.Height,
                Weight = viewModel.Weight,
                TypeId = viewModel.Type,
                Sex    = viewModel.Sex,
                Token  = (2018 + _unitOfWork.Pets.GetPet().Count()).ToString().PadLeft(7, '0')
            };

            _unitOfWork.Pets.Add(pet);
            _unitOfWork.Complete();
            return(RedirectToAction("Index", "Pets"));

            // TODO: BUG redirect to detail
            //return RedirectToAction("Details", new { id = viewModel.Id });
        }
コード例 #2
0
        public ActionResult Update(PetFormViewModel viewModel)
        {
            if (!ModelState.IsValid)
            {
                viewModel.animalTypes = _unitOfWork.AnimalType.GetAnimalType();
                return(View("PetForm", viewModel));
            }


            var petInDb = _unitOfWork.Pets.GetPet(viewModel.Id);

            petInDb.Id        = viewModel.Id;
            petInDb.OwnerName = viewModel.OwnerName;
            petInDb.Phone     = viewModel.Phone;
            petInDb.Age       = viewModel.Age;
            petInDb.Address   = viewModel.Address;
            //patientInDb.Height = viewModel.Height;
            petInDb.Weight = viewModel.Weight;
            petInDb.Sex    = viewModel.Sex;
            petInDb.TypeId = viewModel.Type;

            _unitOfWork.Complete();
            return(RedirectToAction("Index", "Pets")
                   );
        }
コード例 #3
0
        public ActionResult Create()
        {
            var viewModel = new PetFormViewModel
            {
                animalTypes = _unitOfWork.AnimalType.GetAnimalType(),
                Heading     = "New Pet"
            };

            return(View("PetForm", viewModel));
        }
コード例 #4
0
        public ActionResult edit(PetFormViewModel model)
        {
            if (model != null)
            {
                var Pet = Mapper.Map <PetFormViewModel, Pet>(model);
                petService.Update(Pet);
                petService.SavePet();
            }

            return(RedirectToAction("Add", "Pet"));
        }
コード例 #5
0
        public ActionResult Create(PetFormViewModel newPet)
        {
            if (ModelState.IsValid)
            {
                var pet = Mapper.Map <PetFormViewModel, Pet>(newPet);
                petService.CreatePet(pet);
                petService.SavePet();
                return(RedirectToAction("Add"));
            }


            return(View("Add"));
        }
コード例 #6
0
        public ActionResult Save(PetFormViewModel viewModel, HttpPostedFileBase file)
        {
            //// shit happens
            //if (!ModelState.IsValid)
            //{
            //    var viewModel = new PetFormViewModel()
            //    {
            //        Pet = pet
            //    };

            //    return View("PetForm", viewModel);
            //}
            //Add
            if (viewModel.Id == 0)
            {
                var identityId = User.Identity.GetUserId();

                if (identityId == null)
                {
                    return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
                }

                var PersonToUpdate = context.Persons.Where(s => s.IdentityUserId == identityId).SingleOrDefault();
                var kindToUpdate   = context.Kinds.Single(s => s.Id == viewModel.KindId);

                context.Pets.Add(new Pet(viewModel.Name, viewModel.KindId, kindToUpdate, viewModel.Age, ExtraMethods.UploadPhoto(file), PersonToUpdate));

                TempData["PetAdd"] = "Added";
            }
            else //Update
            {
                var petInDb  = context.Pets.Single(m => m.Id == viewModel.Id);
                var kindInDb = context.Kinds.Single(s => s.Id == viewModel.KindId);


                petInDb.Modify(viewModel.Name, viewModel.KindId, kindInDb, viewModel.Age);

                if (!(file is null))
                {
                    petInDb.Modify(ExtraMethods.UploadPhoto(file));
                }


                TempData["PetUpdate"] = "Updated";
            }
            context.SaveChanges();

            return(RedirectToAction("ShowPets"));
        }
コード例 #7
0
        public ActionResult PetForm(int?id)
        {
            Pet pet;
            PetFormViewModel petViewModel = new PetFormViewModel();
            var petBreedTypes             = db.PetBreedTypes.ToList();
            var petSizes   = db.PetSizes.ToList();
            var petTypes   = db.PetTypes.ToList();
            var petGenders = db.PetGenders.ToList();

            if (id == null)
            {
                petViewModel = new PetFormViewModel
                {
                    Id            = 0,
                    OwnerId       = User.Identity.GetUserId(),
                    PetBreedTypes = petBreedTypes,
                    PetSizes      = petSizes,
                    PetTypes      = petTypes,
                    PetGenders    = petGenders
                };
                return(View("Form", petViewModel));
            }

            pet = db.Pets.Find(id);

            if (pet == null)
            {
                return(HttpNotFound());
            }

            var county      = db.Counties.Find(pet.CountyId);
            var state       = db.States.Find(pet.StateId);
            var petPictures = db.PetPictures.Where(pp => pp.PetId == pet.Id).ToList();

            Mapper.Map(pet, petViewModel);
            petViewModel.PetTypes      = petTypes;
            petViewModel.PetSizes      = petSizes;
            petViewModel.PetBreedTypes = petBreedTypes;
            petViewModel.PetGenders    = petGenders;
            petViewModel.CountyView    = county.Name;
            petViewModel.StateView     = state.Abbreviation;
            petViewModel.PetPictures   = petPictures;

            return(View("Form", petViewModel));
        }
コード例 #8
0
        public ActionResult Edit(int id)
        {
            var pet = _unitOfWork.Pets.GetPet(id);

            var viewModel = new PetFormViewModel
            {
                Heading   = "Edit Pet",
                Id        = pet.Id,
                OwnerName = pet.OwnerName,
                Phone     = pet.Phone,
                Date      = pet.DateTime,
                //Date = patient.DateTime.ToString("d MMM yyyy"),
                Age     = pet.Age,
                Address = pet.Address,
                // Height = patient.Height,
                Weight      = pet.Weight,
                Sex         = pet.Sex,
                Type        = pet.TypeId,
                animalTypes = _unitOfWork.AnimalType.GetAnimalType()
            };

            return(View("PetForm", viewModel));
        }
コード例 #9
0
        public ActionResult SalvarPet(PetFormViewModel petViewModel)
        {
            if (!ModelState.IsValid)
            {
                return(View("PetForm", petViewModel));
            }

            Pet pet;

            if (petViewModel.Id == 0)
            {
                pet = new Pet();
            }
            else
            {
                pet = db.Pets.Find(petViewModel.Id);
            }

            if (pet == null)
            {
                return(HttpNotFound());
            }

            Mapper.Map(petViewModel, pet);

            if (petViewModel.LivesWithOwner)
            {
                var owner = db.Users.Find(petViewModel.OwnerId);
                pet.FillInAddress(owner);
            }
            else
            {
                var county = db.Counties.First(c => c.Name.Equals(petViewModel.CountyView));
                var state  = db.States.First(s => s.Name.Equals(petViewModel.StateView));

                pet.StateId  = state.Id;
                pet.CountyId = county.Id;
            }

            pet.OwnerId = petViewModel.OwnerId;

            if (pet.Id == 0)
            {
                pet.Active  = true;
                pet.AddedIn = DateTime.Now;
                db.Pets.Add(pet);
            }

            db.SaveChanges();

            if (petViewModel.Pictures.Any())
            {
                foreach (var picture in petViewModel.Pictures)
                {
                    if (picture != null && picture.ContentLength > 0)
                    {
                        var petPicture = new PetPicture
                        {
                            PetId   = pet.Id,
                            Picture = new byte[picture.ContentLength]
                        };

                        picture.InputStream.Read(petPicture.Picture, 0, picture.ContentLength);

                        var mainPicture = db.PetPictures.SingleOrDefault(pp => pp.PetId == pet.Id && pp.MainPic);

                        if (mainPicture == null)
                        {
                            petPicture.MainPic = true;
                        }

                        db.PetPictures.Add(petPicture);

                        db.SaveChanges();
                    }
                }
            }

            return(RedirectToAction("PetOwnerList"));
        }