Exemplo n.º 1
0
        public void EditPet(EditLostPetBM bind)
        {
            LostPet lostPet = this.Context.LostPets.Find(bind.Id);

            lostPet.Name                   = bind.Name;
            lostPet.AnimalType             = bind.AnimalType;
            lostPet.Breed                  = bind.Breed;
            lostPet.Age                    = bind.Age;
            lostPet.LastSeenLocation       = bind.LastSeenLocation;
            lostPet.LastSeenTime           = bind.LastSeenTime;
            lostPet.DistinguishingFeatures = bind.DistinguishingFeatures;
            lostPet.Temper                 = bind.Temper;
            lostPet.Description            = bind.Description;



            if (bind.Thumbnail != null && bind.Thumbnail.ContentLength > 0)
            {
                var uploadDir = "~/Uploads/LostPets";
                var imagePath = Path.Combine(System.Web.Hosting.HostingEnvironment.MapPath(uploadDir), bind.Thumbnail.FileName);
                var imageUrl  = uploadDir + "/" + bind.Thumbnail.FileName;
                bind.Thumbnail.SaveAs(imagePath);
                lostPet.ThumbnailUrl = imageUrl.Remove(0, 1);
            }

            this.Context.SaveChanges();
        }
Exemplo n.º 2
0
        public void Edit_ShouldRedirectSuccessfully()
        {
            EditLostPetBM bm = new EditLostPetBM()
            {
                AnimalType             = AnimalType.Other,
                Name                   = "Edited Animal",
                Age                    = 12,
                Breed                  = "Edited Breed",
                Description            = "",
                DistinguishingFeatures = "",
                LastSeenLocation       = "West Park",
                LastSeenTime           = DateTime.Today,
                Temper                 = Temper.Calm,
                Thumbnail              = null,
                Id = 1
            };

            this._controller.WithCallTo(c => c.Edit(bm)).ShouldRedirectToRoute("");
        }
Exemplo n.º 3
0
        public void Edit_ShouldEditSuccessfully()
        {
            EditLostPetBM bm = new EditLostPetBM()
            {
                AnimalType             = AnimalType.Other,
                Name                   = "Edited Animal",
                Age                    = 12,
                Breed                  = "Edited Breed",
                Description            = "",
                DistinguishingFeatures = "",
                LastSeenLocation       = "West Park",
                LastSeenTime           = DateTime.Today,
                Temper                 = Temper.Calm,
                Thumbnail              = null,
                Id = 1
            };

            this._controller.Edit(bm);

            Assert.AreEqual("Edited Animal", this._context.LostPets.Find(1).Name);
        }
Exemplo n.º 4
0
        public ActionResult Edit(EditLostPetBM bind)
        {
            string username = User.Identity.Name;

            var validImageTypes = new string[]
            {
                "image/gif",
                "image/jpeg",
                "image/pjpeg",
                "image/jpg",
                "image/png"
            };

            //if (bind.Thumbnail == null || bind.Thumbnail.ContentLength == 0)
            //{
            //    ModelState.AddModelError("Thumbnail", "This field is required");
            //}
            if (bind.Thumbnail != null && !validImageTypes.Contains(bind.Thumbnail.ContentType))
            {
                ModelState.AddModelError("Thumbnail", "Please choose either a GIF, JPG or PNG image.");
            }


            if (this.ModelState.IsValid && this.service.PetBelongsToUser(username, bind.Id) || User.IsInRole("Admin"))
            {
                try
                {
                    this.service.EditPet(bind);
                    return(RedirectToAction("profile", "users"));
                }
                catch (System.Web.HttpException e)
                {
                    ModelState.AddModelError("Thumbnail", e.Message);
                }
            }


            return(RedirectToAction("index", "home"));
        }