//Get all contacts including supplier public ActionResult Get([DataSourceRequest] DataSourceRequest request) { contactsService = new ContactsService(); cmAPI = new ContactManagerAPIController(contactsService); var ResultString = cmAPI.Get().Content.ReadAsStringAsync().Result; var result = JsonConvert.DeserializeObject <IEnumerable <ContactViewModel> >(ResultString); return(View(Json(ResultString.ToDataSourceResult(request), JsonRequestBehavior.AllowGet))); }
//Create contact public ActionResult Post(ContactViewModel contact) { contactsService = new ContactsService(); cmAPI = new ContactManagerAPIController(contactsService); Mapper.CreateMap <ContactViewModel, ContactServiceModel>(); ContactServiceModel csvcVM = Mapper.Map <ContactViewModel, ContactServiceModel>(contact); var addedContact = cmAPI.Post(csvcVM); return(RedirectToAction("Get")); }
//Get contacts by Id public ActionResult GetContactbyId(int id) { contactsService = new ContactsService(); cmAPI = new ContactManagerAPIController(contactsService); var contact = cmAPI.Get(id); var person = new Person(); var contactVNM = ContactViewModel.MapFromEntity(person); return(View(contactVNM)); }
//Update contact public ActionResult Put([FromBody] ContactViewModel updatedContactVM) { contactsService = new ContactsService(); cmAPI = new ContactManagerAPIController(contactsService); // Check if contact exists to update. Mapper.CreateMap <ContactViewModel, ContactServiceModel>(); ContactServiceModel csvcVM = Mapper.Map <ContactViewModel, ContactServiceModel>(updatedContactVM); cmAPI.Put(csvcVM); return(RedirectToAction("Get")); }
//Delete contact public ActionResult Delete(int id) { contactsService = new ContactsService(); cmAPI = new ContactManagerAPIController(contactsService); var contactExist = cmAPI.Get(id); if (contactExist == null) { return(View("The contact doesn't exist in database!")); } else { cmAPI.Delete(id); } return(RedirectToAction("Get")); }