public IHttpActionResult PutFoundPet(int id, FoundPet foundPet) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != foundPet.ID) { return(BadRequest()); } db.Entry(foundPet).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!FoundPetExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
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 EditFoundPetVM GetEditPetVM(int id) { FoundPet foundPet = this.Context.FoundPets.FirstOrDefault(p => p.Id == id); EditFoundPetVM vm = Mapper.Map <FoundPet, EditFoundPetVM>(foundPet); return(vm); }
public FoundPetVM GetFoundPetVM(string username, int id) { FoundPet foundPet = this.Context.FoundPets.FirstOrDefault(p => p.Id == id); FoundPetVM vm = Mapper.Map <FoundPet, FoundPetVM>(foundPet); vm.IsLostPet = false; vm.Comments = vm.Comments.OrderByDescending(comment => comment.DatePosted); vm.AssociatedUsername = username; return(vm); }
public IHttpActionResult GetFoundPet(int id) { FoundPet foundPet = db.FoundPets.Find(id); if (foundPet == null) { return(NotFound()); } return(Ok(foundPet)); }
public IHttpActionResult DeleteFoundPet(int id) { FoundPet foundPet = db.FoundPets.Find(id); if (foundPet == null) { return(NotFound()); } db.FoundPets.Remove(foundPet); db.SaveChanges(); return(Ok(foundPet)); }
public void DeleteFoundPet(int id) { FoundPet foundPet = this.Context.FoundPets.Find(id); List <Comment> relatedComments = foundPet.Comments; foundPet.Comments = null; foreach (var comment in this.Context.Comments) { foreach (var relatedComment in relatedComments) { if (comment.Id == relatedComment.Id) { this.Context.Comments.Remove(comment); } } } this.Context.FoundPets.Remove(foundPet); this.Context.SaveChanges(); }
public void EditPet(EditFoundPetBM bind) { FoundPet foundPet = this.Context.FoundPets.Find(bind.Id); foundPet.AnimalType = bind.AnimalType; foundPet.Breed = bind.Breed; foundPet.LastSeenLocation = bind.LastSeenLocation; foundPet.LastSeenTime = bind.LastSeenTime; foundPet.DistinguishingFeatures = bind.DistinguishingFeatures; foundPet.Description = bind.Description; foundPet.ActionTaken = bind.ActionTaken; 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); } this.Context.SaveChanges(); }
public IHttpActionResult PostFoundPet(FoundPet foundPet) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } //Pet pet = db2.Pets.Find(foundPet.PetID); Pet pet = db2.Pets.Single(p => p.ID == foundPet.PetID); if (pet == null) { return(NotFound()); } foundPet.FounderID = User.Identity.GetUserId(); foundPet.FoundDate = DateTime.Now; db.FoundPets.Add(foundPet); db.SaveChanges(); pet.FoundID = foundPet.ID; db2.SaveChanges(); return(CreatedAtRoute("DefaultApi", new { id = foundPet.ID }, foundPet)); }
public void AddComment(AddComentBM bind) { var author = this.Context.Users.FirstOrDefault(u => u.UserName == bind.Username); Comment comment = new Comment { Author = author, Content = bind.Content, DatePosted = DateTime.Now, }; if (bind.IsLostPet) { LostPet lostPet = this.Context.LostPets.FirstOrDefault(p => p.Id == bind.PetId); lostPet.Comments.Add(comment); } else { FoundPet foundPet = this.Context.FoundPets.FirstOrDefault(p => p.Id == bind.PetId); foundPet.Comments.Add(comment); } this.Context.SaveChanges(); }