public static bool Save(Lead lead)
 {
     if (!leads.ContainsKey(lead.Email))
     {
         //email no exists then add
         leads.Add(lead.Email, new List <Lead> {
             lead
         });
         LeadLog.writeTofile(lead);
         return(true);
     }
     else
     {
         if (LeadRepository.Get(lead.Email, lead.CorrelationId) == null)
         {
             //Email with this Id isn't exist then add
             var listLead = leads.Where(x => x.Key == lead.Email).Select(x => x.Value).SingleOrDefault();
             listLead.Add(lead);
             leads[lead.Email] = listLead;
             LeadLog.writeTofile(lead);
             return(true);
         }
         else
         {
             //the email with this correlationId has already existed.
             return(false);
         }
     }
 }
        public IHttpActionResult GetAll()
        {
            if (LeadRepository.DictionaryIsEmpty())
            {
                throw new HttpResponseException(HttpStatusCode.Gone);
            }

            return(Content(HttpStatusCode.OK, LeadRepository.GetAll()));
        }
        public IHttpActionResult Get(string email, string correlationId)
        {
            var lead = LeadRepository.Get(email, correlationId);

            if (lead != null)
            {
                return(Content(HttpStatusCode.OK, lead));
            }
            else
            {
                HttpResponseMessage message = new HttpResponseMessage(HttpStatusCode.NotFound);
                message.Content = new StringContent("the lead is not existed.");
                throw new HttpResponseException(message);
            }
        }
        public IHttpActionResult Post([FromBody] Lead lead)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (LeadRepository.Save(lead))
            {
                return(Content(HttpStatusCode.Created, "the new lead has added successfully."));
            }

            HttpResponseMessage message = new HttpResponseMessage(HttpStatusCode.Ambiguous);

            message.Content = new StringContent("Ooops! This lead is already existed in the dictionary.");
            throw new HttpResponseException(message);
        }
        public IHttpActionResult Delete(string email, string correlationId = "")
        {
            if (LeadRepository.DictionaryIsEmpty())
            {
                throw new HttpResponseException(HttpStatusCode.Gone);
            }

            if (LeadRepository.Delete(email, correlationId))
            {
                return(Ok(HttpStatusCode.OK));
            }

            else
            {
                HttpResponseMessage message = new HttpResponseMessage(HttpStatusCode.NotFound);
                message.Content = new StringContent("Oooops! the lead is not member of the dictinary.");
                throw new HttpResponseException(message);
            }
        }