public int Add(Consultant consultant)
        {
            int newId = _consultants.Max(c => c.ID) + 1;
            consultant.ID = newId;

            _consultants.Add(consultant);
            return newId;
        }
        public void Update(Consultant consultant)
        {
            var oldConsulant = _consultants.Single(c => c.ID == consultant.ID);

            _consultants.Remove(oldConsulant);

            _consultants.Add(consultant);
        }
        public int Add(Consultant consultant)
        {
            int newId = _consultants.Max(c => c.ID) + 1;

            consultant.ID = newId;

            _consultants.Add(consultant);
            return(newId);
        }
        public HttpResponseMessage Post(Consultant consultant)
        {
            if (!ModelState.IsValid)
            {
                throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.BadRequest));
            }

            consultant.Owner = Thread.CurrentPrincipal.Identity.Name;
            var id = _repository.Add(consultant);

            var response = new HttpResponseMessage(HttpStatusCode.Created);
            response.Headers.Location = new Uri(Request.RequestUri.AbsoluteUri + "/" + id.ToString());
            
            return response;
        }
        public HttpResponseMessage Post(Consultant consultant)
        {
            if (!ModelState.IsValid)
            {
                throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.BadRequest));
            }

            consultant.Owner = Thread.CurrentPrincipal.Identity.Name;
            var id = _repository.Add(consultant);

            var response = new HttpResponseMessage(HttpStatusCode.Created);

            response.Headers.Location = new Uri(Request.RequestUri.AbsoluteUri + "/" + id.ToString());

            return(response);
        }
        public void Put(int id, Consultant consultant)
        {
            // check if consultant exists
            var oldConsultant = _repository.GetAll().FirstOrDefault(c => c.ID == id);

            if (oldConsultant == null)
            {
                throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotFound));
            }

            consultant.ID = id;
            consultant.Owner = Thread.CurrentPrincipal.Identity.Name;

            if (oldConsultant.Owner != consultant.Owner)
            {
                throw new SecurityException("Not authorized to change record");
            }
            
            _repository.Update(consultant);
        }
        public void Put(int id, [FromBody] Consultant consultant)
        {
            // check if consultant exists
            var oldConsultant = _repository.GetAll().FirstOrDefault(c => c.ID == id);

            if (oldConsultant == null)
            {
                throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotFound));
            }

            consultant.ID    = id;
            consultant.Owner = Thread.CurrentPrincipal.Identity.Name;

            // check moved to authorization manager
            //if (oldConsultant.Owner != consultant.Owner)
            //{
            //    throw new SecurityException("Not authorized to change record");
            //}

            _repository.Update(consultant);
        }
        private static void UpdateConsultant(HttpClient client, string location)
        {
            "\nUpdate consultant...".ConsoleGreen();

            var newClient = new HttpClient { BaseAddress = new Uri(location) };
            if (client.DefaultRequestHeaders.Authorization != null)
            {
                newClient.DefaultRequestHeaders.Authorization = client.DefaultRequestHeaders.Authorization;
            }

            var updatedConsultant = new Consultant
            {
                Name = "Brock Allen",
                Country = "US",
                EmailAddress = "*****@*****.**"
            };

            var response = newClient.PutAsJsonAsync<Consultant>("", updatedConsultant).Result;
            response.EnsureSuccessStatusCode();

            ListConsultants(client);
        }
        private static string AddConsultant(HttpClient client)
        {
            // add consultant
            var newConsultant = new Consultant
            {
                Name = "Brock Allen",
                Country = "US",
                EmailAddress = "*****@*****.**",
            };

            "\nAdd new consultant...".ConsoleGreen();
            var response = client.PostAsJsonAsync<Consultant>("consultants", newConsultant).Result;

            if (response.StatusCode == HttpStatusCode.Created)
            {
                Console.WriteLine("success.");
                Console.WriteLine("location: {0}", response.Headers.Location);
                return response.Headers.Location.OriginalString;
            }
            else
            {
                Console.WriteLine("failed.");
                Console.WriteLine(response.StatusCode);
                return null;
            }
        }
        public void Update(Consultant consultant)
        {
            var oldConsulant = _consultants.Single(c => c.ID == consultant.ID);
            _consultants.Remove(oldConsulant);

            _consultants.Add(consultant);
        }