コード例 #1
0
ファイル: LostPetsService.cs プロジェクト: Martinik/PetHome
        public void CreateNewLostPet(CreateLostPetBM bind, string username)
        {
            LostPet lostPet = Mapper.Map <CreateLostPetBM, LostPet>(bind);

            ApplicationUser associatedUser = this.Context.Users.FirstOrDefault(user => user.UserName == username);

            lostPet.AssociatedUser = associatedUser;

            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);
            }

            if (associatedUser != null)
            {
                associatedUser.LostPets.Add(lostPet);
            }
            else
            {
                this.Context.LostPets.Add(lostPet);
            }

            this.Context.SaveChanges();
        }
コード例 #2
0
        public ActionResult Create(CreateLostPetBM bind)
        {
            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)
            {
                string username = User.Identity.Name;

                this.service.CreateNewLostPet(bind, username);

                return(this.RedirectToAction("Index"));
            }

            CreateLostPetVM vm = new CreateLostPetVM();

            return(View(vm));
        }
コード例 #3
0
        public void Create_ShouldRedirectToIndex()
        {
            CreateLostPetBM bm = new CreateLostPetBM()
            {
                AnimalType             = AnimalType.Other,
                Name                   = "Test Animal",
                Age                    = 12,
                Breed                  = "Test Breed",
                Description            = "",
                DistinguishingFeatures = "",
                LastSeenLocation       = "West Park",
                LastSeenTime           = DateTime.Today,
                Temper                 = Temper.Calm,
                Thumbnail              = null
            };

            this._controller.WithCallTo(c => c.Create(bm)).ShouldRedirectToRoute("");
        }
コード例 #4
0
        public void Create_ShouldSuccessfullyCreate()
        {
            CreateLostPetBM bm = new CreateLostPetBM()
            {
                AnimalType             = AnimalType.Other,
                Name                   = "Test Animal",
                Age                    = 12,
                Breed                  = "Test Breed",
                Description            = "",
                DistinguishingFeatures = "",
                LastSeenLocation       = "West Park",
                LastSeenTime           = DateTime.Today,
                Temper                 = Temper.Calm,
                Thumbnail              = null
            };

            this._controller.Create(bm);

            Assert.AreEqual(3, this._context.LostPets.Count());
        }