Exemple #1
0
        public HttpResponseMessage PostContact(Contact item)
        {
            var response  = new HttpResponseMessage();
            var dupStatus = repository.CheckDuplicates(item);

            if (dupStatus == 0)
            {
                if (repository.Add(item))
                {
                    response = Request.CreateResponse <Contact>(HttpStatusCode.Created, item);
                    string uri = Url.Link("DefaultApi", new { id = item.Id });
                    response.Headers.Location = new Uri(uri);
                }

                else
                {
                    response = new HttpResponseMessage(HttpStatusCode.InternalServerError)
                    {
                        Content      = new StringContent("Internal server error occurred. Could not save data."),
                        ReasonPhrase = "Error"
                    };
                    throw new HttpResponseException(response);
                }
                return(response);
            }



            else if (dupStatus == 1 || dupStatus == 2)
            {
                string message = "";
                if (dupStatus == 1)
                {
                    message = "Email already exists!";
                }
                else if (dupStatus == 2)
                {
                    message = "Phone no already exists";
                }
                response = new HttpResponseMessage(HttpStatusCode.Conflict)
                {
                    Content      = new StringContent(message),
                    ReasonPhrase = "Error"
                };
                throw new HttpResponseException(response);
            }

            else
            {
                response = new HttpResponseMessage(HttpStatusCode.InternalServerError)
                {
                    Content      = new StringContent("Internal server error occurred. Could not save data."),
                    ReasonPhrase = "Error"
                };
                throw new HttpResponseException(response);
            }
        }