public void AddContact()
        {
            var physicalDataSourcePath = HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["virtualPath"]);
            var physicalAvatarPath = HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["avatarPath"]);
            var contact = new ContactDetail();
            ReadFileData(contact, physicalAvatarPath);

            XMLServices.AddContact(contact, physicalDataSourcePath);
        }
        public IHttpActionResult UpdateContact()
        {
            var physicalDataSourcePath = HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["virtualPath"]);
            var physicalAvatarPath = HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["avatarPath"]);
            var contact = new ContactDetail();
            ReadFileData(contact, physicalAvatarPath);

            var result = XMLServices.UpdateContact(contact, physicalDataSourcePath);
            if (result) return Ok();
            return NotFound();
        }
        public void AddContact(ContactDetail contact, string urlDataStoreXml)
        {
            lock (lockObject)
            {
                XDocument doc = LoadXDocument(urlDataStoreXml);
                if (doc == null)
                {
                    doc = new XDocument();
                    doc.Add(new XElement("Contacts"));
                }

                var contacts = doc.Root.Descendants("Contacts");
                var cont = new XElement("Contact");
                var id = Guid.NewGuid();
                cont.SetAttributeValue("contactId",id);
                if (!string.IsNullOrEmpty(contact.Name))
                {
                    var name = new XElement("Name", contact.Name);
                    cont.Add(name);
                }
                if (!string.IsNullOrEmpty(contact.MobileNumber))
                {
                    var mobNumber = new XElement("MobileNumber", contact.MobileNumber);
                    cont.Add(mobNumber);
                }
                if (!string.IsNullOrEmpty(contact.HomeNumber))
                {
                    var homeNumber = new XElement("HomeNumber", contact.HomeNumber);
                    cont.Add(homeNumber);
                }

                if (!string.IsNullOrEmpty(contact.EmailAddress))
                {
                    var email = new XElement("EmailAddress", contact.EmailAddress);
                    cont.Add(email);
                }

                var avatar = new XElement("Avatar", contact.Avatar);
                cont.Add(avatar);

                cont.Add(contact);
                doc.Root.Add(cont);
                SaveDocument(doc, urlDataStoreXml);
            }
        }
 public IHttpActionResult PostAndSyncData(ContactDetail[] data)
 {
     try
     {
         var physicalDataSourcePath =
             HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["virtualPath"]);
         var physicalAvatarPath =
             HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["avatarPath"]);
         foreach (var contact in data)
         {
             ReadFileData(contact, physicalAvatarPath);
             var result = XMLServices.UpdateContact(contact, physicalDataSourcePath);
         }
         return Ok();
     }
     catch (Exception e)
     {
         return NotFound();
     }
 }
        public bool UpdateProfileContact(ContactDetail contact, string urlDataStoreXml)
        {
            lock (lockObject)
            {
                XDocument doc = LoadXDocument(urlDataStoreXml);
                try
                {
                    if (doc == null)
                    {
                        return false;
                    }
                    var existingContact = doc.Root.Descendants("Contact").SingleOrDefault(p => p.Attribute("contactId").Value == contact.ContactId);

                    if (existingContact != null && !string.IsNullOrEmpty(contact.Avatar))
                    {
                        existingContact.Descendants("Avatar").SingleOrDefault().SetValue(contact.Avatar);
                    }
                    SaveDocument(doc, urlDataStoreXml);
                    return true;
                }
                catch (Exception e)
                {
                    return false;
                }
            }
        }
        private void ReadAvatarFileData(ContactDetail contact, string physicalAvatarPath)
        {
            var httpRequest = HttpContext.Current.Request;
            var avatarUrl = string.Empty;
            // Check if files are available
            if (httpRequest.Files.Count > 0)
            {
                var files = new List<string>();

                // interate the files and save on the server
                foreach (string file in httpRequest.Files)
                {
                    var postedFile = httpRequest.Files[file];
                    var filePath = physicalAvatarPath + postedFile.FileName;
                    postedFile.SaveAs(filePath);

                    contact.Avatar = "/Avatars/" + postedFile.FileName;
                    files.Add(filePath);
                }
            }
            foreach (var key in httpRequest.Form.AllKeys)
            {
                foreach (var val in httpRequest.Form.GetValues(key))
                {
                    if (key.Equals("ContactId", StringComparison.OrdinalIgnoreCase))
                    {
                        contact.ContactId = val;
                    }

                }
            }
        }