public IHttpActionResult RetrieveContacts(int page = 1, int pageSize = 10) { try { int totalRecords; var contacts = AutoMapper.Mapper. Map <List <Model.Contact>, List <Contact> > (ContactRepositoryInstance.GetContacts(page, pageSize, out totalRecords)); var result = new ResourceCollection <Contact>() { Items = contacts, Page = page, PageSize = pageSize, TotalItems = totalRecords, TotalPages = Convert.ToInt32(Math.Ceiling(Convert.ToDouble(totalRecords) / pageSize)) }; return(Ok(result)); } catch (Exception exception) { var resp = new HttpResponseMessage(HttpStatusCode.InternalServerError) { Content = new StringContent(string.Format("internal error")), ReasonPhrase = exception.Message }; throw new HttpResponseException(resp); } }
public HttpResponseMessage EditContact(string emailId, Contact contact) { if (contact != null) { Validate("emailId", emailId); ValidateEmail(emailId); Validate("emailId", contact.EmailId); Validate("firstName", contact.FirstName); Validate("lastName", contact.LastName); Validate("phoneNumber", contact.PhoneNumber); ValidateEmail(contact.EmailId); try { var result = ContactRepositoryInstance.EditContact( emailId, AutoMapper.Mapper. Map <Contact, Contacts.Model.Contact>(contact) ); return(this.Request.CreateResponse(System.Net.HttpStatusCode.Accepted, result)); } catch (Exception exception) { var resp = new HttpResponseMessage(HttpStatusCode.InternalServerError) { Content = new StringContent(exception.Message), ReasonPhrase = "internal error" }; throw new HttpResponseException(resp); } } else { throw new ArgumentNullException("request is empty"); } }
public void RemoveContact(string emailId) { Validate("emailId", emailId); ValidateEmail(emailId); try { ContactRepositoryInstance.RemoveContact(emailId); } catch (Exception exception) { var resp = new HttpResponseMessage(HttpStatusCode.InternalServerError) { Content = new StringContent(exception.Message), ReasonPhrase = "internal error" }; throw new HttpResponseException(resp); } }
public IHttpActionResult RetrieveContact(string emailId) { Validate("emailId", emailId); ValidateEmail(emailId); try { var contact = ContactRepositoryInstance.GetContact(emailId); if (contact != null) { var result = AutoMapper.Mapper. Map <Model.Contact, Contact> (contact); return(Ok(result)); } else { var resp = new HttpResponseMessage(HttpStatusCode.NotFound) { Content = new StringContent(string.Format("No record found for = {0}", emailId)), ReasonPhrase = "EmailID Not Found" }; throw new HttpResponseException(resp); } } catch (ArgumentException exception) { var resp = new HttpResponseMessage(HttpStatusCode.BadRequest) { Content = new StringContent(string.Format(exception.Message)), ReasonPhrase = exception.Message }; throw new HttpResponseException(resp); } catch (Exception exception) { var resp = new HttpResponseMessage(HttpStatusCode.InternalServerError) { Content = new StringContent(string.Format(exception.Message)), ReasonPhrase = exception.Message }; throw new HttpResponseException(resp); } }