public ActionResult Create(CreateFoundPetBM 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.CreateNewFoundPet(bind, username); return(this.RedirectToAction("Index")); } CreateFoundPetVM vm = new CreateFoundPetVM(); return(View(vm)); }
public void CreateNewFoundPet(CreateFoundPetBM bind, string username) { FoundPet foundPet = Mapper.Map <CreateFoundPetBM, FoundPet>(bind); ApplicationUser associatedUser = this.Context.Users.FirstOrDefault(user => user.UserName == username); foundPet.AssociatedUser = associatedUser; if (bind.Thumbnail != null && bind.Thumbnail.ContentLength > 0) { var uploadDir = "~/Uploads/FoundPets"; var imagePath = Path.Combine(System.Web.Hosting.HostingEnvironment.MapPath(uploadDir), bind.Thumbnail.FileName); var imageUrl = uploadDir + "/" + bind.Thumbnail.FileName; bind.Thumbnail.SaveAs(imagePath); foundPet.ThumbnailUrl = imageUrl.Remove(0, 1); } if (associatedUser != null) { associatedUser.FoundPets.Add(foundPet); } else { this.Context.FoundPets.Add(foundPet); } this.Context.SaveChanges(); }
public void Create_ShouldRedirectToIndex() { CreateFoundPetBM bm = new CreateFoundPetBM() { AnimalType = AnimalType.Other, Breed = "Test Breed", Description = "", DistinguishingFeatures = "", LastSeenLocation = "West Park", LastSeenTime = DateTime.Today, ActionTaken = "nothing", Thumbnail = null }; this._controller.WithCallTo(c => c.Create(bm)).ShouldRedirectToRoute(""); }
public void Create_ShouldSuccessfullyCreate() { CreateFoundPetBM bm = new CreateFoundPetBM() { AnimalType = AnimalType.Other, Breed = "Test Breed", Description = "", DistinguishingFeatures = "", LastSeenLocation = "West Park", LastSeenTime = DateTime.Today, ActionTaken = "nothing", Thumbnail = null }; this._controller.Create(bm); Assert.AreEqual(3, this._context.FoundPets.Count()); }