/// <summary> /// This method posts to the Pet endpoint of the custom API using data from petfinder API /// when the user clicks on the pet to see more in details /// </summary> /// <param name="id">the petfinder pet id</param> /// <param name="search">the location parameter to get list of pets from Petfinder</param> /// <returns></returns> public async Task <IActionResult> PetAPIPost(int?id, string search) { PetCollections = await GetPetListJSON(search); Pet SelectedPet = PetCollections.Where(x => x.id.data == id.ToString()).FirstOrDefault(); if (SelectedPet != null) { string[] image = new string[SelectedPet.media.photos.photo.Count()]; string[] breed = new string[SelectedPet.breeds.breed.Count()]; for (int i = 0; i < SelectedPet.media.photos.photo.Count(); i++) { image[i] = SelectedPet.media.photos.photo[i].data; } for (int i = 0; i < SelectedPet.breeds.breed.Count(); i++) { breed[i] = SelectedPet.breeds.breed[i].data; } string images = string.Join(",", image); string breeds = string.Join(",", breed); PetPost AddPet = new PetPost() { PetID = (int)id, Animal = SelectedPet.animal.data, Breed = breeds, Mix = SelectedPet.mix.data, Name = SelectedPet.name.data, Age = SelectedPet.age.data, Sex = SelectedPet.sex.data, Size = SelectedPet.size.data, Description = SelectedPet.description.data, ShelterID = SelectedPet.shelterId.data, ShelterName = "", Photos = images, Address = SelectedPet.contact.address1.data, City = SelectedPet.contact.city.data, Zip = SelectedPet.contact.zip.data, State = SelectedPet.contact.state.data, Phone = SelectedPet.contact.phone.data, Email = SelectedPet.contact.email.data, }; string output = await Task.Run(() => JsonConvert.SerializeObject(AddPet)); var httpContent = new StringContent(output, System.Text.Encoding.UTF8, "application/json"); using (HttpClient httpClient = new HttpClient()) { var httpResponse = await httpClient.PostAsync("https://lovethembackapi2.azurewebsites.net/api/Pets", httpContent); if (httpResponse.Content != null) { var responseContent = await httpResponse.Content.ReadAsStringAsync(); } } } return(NoContent()); }
public async Task <ActionResult <Pet> > Create([FromBody] PetPost model) { if (!ModelState.IsValid) { return(BadRequest(model)); } Pet pet = new Pet(); await db.Pet.AddAsync(AssignsControllers.AssingPet(model, pet)); try { await db.SaveChangesAsync(); } catch (System.Exception err) { return(BadRequest(new { ok = false, err = new { message = err.InnerException.Message } })); } return(Ok(new { ok = true, pet })); }
public ActionResult Create([Bind(Include = "PetPostID,PetID,PetPrice,PetDetails,PetImageUrl,PetLocation,PostCreatedDat,PetColor,PostStatus")] PetPost petPost, HttpPostedFileBase file) { if (ModelState.IsValid) { try { String fileName = ""; if (file != null) { fileName = Guid.NewGuid().ToString() + Path.GetExtension(file.FileName); string physicalPath = Server.MapPath("~/Images/PetImage/" + fileName); file.SaveAs(physicalPath); petPost.PetImageUrl = @"~/" + @"Images/PetImage" + @"/" + fileName; } ViewBag.FileStatus = "File uploaded successfully."; petPost.PostCreatedDate = DateTime.Now; db.PetPost.Add(petPost); db.SaveChanges(); } catch (Exception) { ViewBag.FileStatus = "Error while file uploading."; } return(RedirectToAction("Index")); } return(View(petPost)); }
public void PetPostGetUpdateTest() { PetPost Pet = new PetPost(); Pet.Name = "Fluffy"; Pet.Name = "Daany"; Assert.Equal("Daany", Pet.Name); }
public ActionResult DeleteConfirmed(int id) { ViewBag.Current = "PetPosts"; PetPost petPost = db.PetPost.Find(id); db.PetPost.Remove(petPost); db.SaveChanges(); return(RedirectToAction("Index")); }
public static Pet AssingPet(PetPost model, Pet pet) { pet.name = model.name; pet.species = model.species; pet.age = model.age; pet.race = model.race; pet.sex = model.sex; pet.id_foundation = model.id_foundation; return(pet); }
/// <summary> /// takes you to main detail page, adds pet to api database /// </summary> /// <param name="id"></param> /// <param name="search"></param> /// <returns></returns> public async Task <IActionResult> Details(int?id, string search) { var userJSON = HttpContext.Session.GetString("profile"); if (userJSON == null) { return(RedirectToAction("Index", "Login")); } var userProfile = JsonConvert.DeserializeObject <Profile>(userJSON); if (id == null) { return(NotFound()); } List <PetPost> PetList = await GetPetFromCustomAPI(); PetPost GetPet = PetList.Where(pet => pet.PetID == id).FirstOrDefault(); List <Profile> Users = new List <Profile>(); dynamic Models = new ExpandoObject(); Models.Search = search; Models.User = userProfile; if (GetPet != null) { if (GetPet.Review != null) { foreach (var item in GetPet.Review) { Profile User = await _context.Profiles.FirstOrDefaultAsync(x => x.UserID == item.UserID); Users.Add(User); } } Models.ReviewUser = Users; Models.GetPet = GetPet; return(View(Models)); } else { await PetAPIPost(id, search); PetList = await GetPetFromCustomAPI(); GetPet = PetList.Where(pet => pet.PetID == id).FirstOrDefault(); Models.GetPet = GetPet; return(View(Models)); } }
public ActionResult Details(int?id) { ViewBag.Current = "PetPosts"; if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } PetPost petPost = db.PetPost.Find(id); if (petPost == null) { return(HttpNotFound()); } return(View(petPost)); }
public void PetPostGetSetTest() { PetPost Pet = new PetPost(); Pet.Name = "Fluffy"; Pet.Mix = "Fluffy"; Pet.PetID = 1; Pet.Phone = "Fluffy"; Pet.Photos = "Fluffy"; Pet.ShelterID = "Fluffy"; Assert.Equal("Fluffy", Pet.Mix); Assert.Equal("Fluffy", Pet.Phone); Assert.Equal("Fluffy", Pet.Photos); Assert.Equal("Fluffy", Pet.ShelterID); Assert.Equal("Fluffy", Pet.Name); }
public ActionResult Edit(int?id) { ViewBag.Current = "PetPosts"; if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } PetPost petPost = db.PetPost.Find(id); if (petPost == null) { return(HttpNotFound()); } List <Pet> petList = db.Pet.ToList(); ViewBag.PetList = new SelectList(petList, "PetID", "PetName"); return(View(petPost)); }
public ActionResult Edit([Bind(Include = "PetPostID,PetID,PetPrice,PetDetails,PetImageUrl,PetLocation,PostCreatedDate,PetColor,PostStatus")] PetPost petPost, HttpPostedFileBase file) { if (ModelState.IsValid) { String fileName = ""; if (file != null) { fileName = Guid.NewGuid().ToString() + Path.GetExtension(file.FileName); string physicalPath = Server.MapPath("~/Images/PetImage/" + fileName); file.SaveAs(physicalPath); petPost.PetImageUrl = @"~/" + @"Images/PetImage" + @"/" + fileName; } petPost.PostCreatedDate = DateTime.Now; db.Entry(petPost).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } return(View(petPost)); }
public async Task <ActionResult <Pet> > Update([FromBody] PetPost model, int id) { if (!ModelState.IsValid) { return(BadRequest(model)); } Pet pet = await db.Pet .Where(k => k.id_pet == id) .FirstOrDefaultAsync(); if (pet == null) { return(NotFound(new { ok = false, err = "The id " + id + " does not exist in the records" })); } AssignsControllers.AssingPet(model, pet); try { await db.SaveChangesAsync(); } catch (System.Exception err) { return(BadRequest(new { ok = false, err = new { message = err.InnerException.Message } })); } return(Ok(new { ok = true, pet })); }