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(); }
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(); }
public EditLostPetVM GetEditPetVM(int id) { LostPet lostPet = this.Context.LostPets.FirstOrDefault(p => p.Id == id); EditLostPetVM vm = Mapper.Map <LostPet, EditLostPetVM>(lostPet); return(vm); }
public LostPetVM GetLostPetVM(string username, int id) { LostPet lostPet = this.Context.LostPets.FirstOrDefault(p => p.Id == id); LostPetVM vm = Mapper.Map <LostPet, LostPetVM>(lostPet); vm.IsLostPet = true; vm.Comments = vm.Comments.OrderByDescending(comment => comment.DatePosted); vm.AssociatedUsername = username; return(vm); }
public void Create([FromBody] LostPet request) { var context = GeoPetContext.GetInstance(); var lostPet = context.Pets.FirstOrDefault <Pet>(x => x.Name.Equals(request.Name) && x.Email.Equals(request.Email)); if (lostPet != null) { lostPet.IsLost = false; context.LostPets.Remove(request); } context.SaveChanges(); TwitterHandler.GetInstance().TweetFoundPet(request); }
public void Create([FromBody] LostPet request) { var context = GeoPetContext.GetInstance(); context.LostPets.Add(request); var pet = context.Pets.FirstOrDefault(p => p.Name.Equals(request.Name) && p.Email.Equals(request.Email)); if (pet != null) { pet.IsLost = true; } context.SaveChanges(); // Tweet lost pet TwitterHandler.GetInstance().TweetLostPet(request); }
public void Init() { this.ConfigureMappings(); this._context = new FakePetHomeContext(); var lostPet = new LostPet() { Id = 1, Name = "Simba", AnimalType = AnimalType.Cat, AssociatedUser = null, Breed = "Lion", IsLostPet = true, LastSeenLocation = "South Park", LastSeenTime = DateTime.Today }; this._context.LostPets.Add(lostPet); this._context.Users.Add(new ApplicationUser() { UserName = "******", Name = "Tester", Details = "For testing purposes", IsAdmin = true }); var fakeHttpContext = new Mock <HttpContextBase>(); var fakeIdentity = new GenericIdentity("User"); var principal = new GenericPrincipal(fakeIdentity, null); fakeHttpContext.Setup(t => t.User).Returns(principal); var controllerContext = new Mock <ControllerContext>(); controllerContext.Setup(t => t.HttpContext).Returns(fakeHttpContext.Object); this._service = new CommentsService(this._context); this._controller = new CommentsController(this._service); // this._controller.ControllerContext = controllerContext.Object; }
public void TweetFoundPet(LostPet lostPet) { var context = GeoPetContext.GetInstance(); var pet = context.Pets.Where(x => x.Email.Equals(lostPet.Email) && x.Name.Equals(lostPet.Name)).SingleOrDefault(); byte[] imgBytes; using (WebClient client = new WebClient()) { imgBytes = client.DownloadData(new Uri(pet.ImageUrl)); } var publishParams = new PublishTweetOptionalParameters(); publishParams.MediaBinaries = new List <byte[]> { imgBytes }; Tweet.PublishTweet($"Encontraron a {pet.Name}!!! :)", publishParams); }
public void TweetLostPet(LostPet lostPet) { var context = GeoPetContext.GetInstance(); var pet = context.Pets.Where(x => x.Email.Equals(lostPet.Email) && x.Name.Equals(lostPet.Name)).SingleOrDefault(); byte[] imgBytes; using (WebClient client = new WebClient()) { imgBytes = client.DownloadData(new Uri(pet.ImageUrl)); } //Generate any parameters to be included var publishParams = new PublishTweetOptionalParameters(); publishParams.MediaBinaries = new List <byte[]> { imgBytes }; Tweet.PublishTweet($"Se perdiĆ³ {pet.Name} :(, ayudanos a encontrarl@", publishParams); }
public void DeleteLostPet(int id) { LostPet lostPet = this.Context.LostPets.Find(id); List <Comment> relatedComments = lostPet.Comments; lostPet.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.LostPets.Remove(lostPet); this.Context.SaveChanges(); }
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(); }