public string AddPosts(string data) { if (string.IsNullOrEmpty(data)) { return("Error: Empty data!"); } try { JObject json = JObject.Parse(data); foreach (var jToken in json["list_posts"]) { string user_fullname = jToken["user_fullname"].ToString(); string user_picture = jToken["user_picture"].ToString(); string user_url = jToken["user_url"].ToString(); string user_id = jToken["user_id"].ToString(); string message = jToken["message"].ToString(); string post_id = jToken["post_id"].ToString(); string full_picture = jToken["full_picture"].ToString(); if (string.IsNullOrEmpty(user_url) || string.IsNullOrEmpty(message) || string.IsNullOrEmpty(post_id) || message.Length < 20 ) { continue; } string check = message.Length >= 50 ? message.Substring(0, 50) : message; if (_shipperHndBcontext.Posts.FirstOrDefault(x => x.PostId.Equals(post_id)) == null && _shipperHndBcontext.Posts.FirstOrDefault(x => x.Message.Contains(check)) == null) { User user; if (!string.IsNullOrEmpty(user_id) && _shipperHndBcontext.Users.FirstOrDefault(x => x.UserId.Equals(user_id)) == null || string.IsNullOrEmpty(user_id) && _shipperHndBcontext.Users.FirstOrDefault(x => x.UserProfileUrl.Equals(user_url)) == null) { user = _shipperHndBcontext.Users.Add(new User { UserId = user_id, UserProfilePicture = user_picture, Name = user_fullname, UserProfileUrl = user_url }); try { _shipperHndBcontext.SaveChanges(); } catch (Exception ex) { _logControl.AddLog(1, "AddPosts", ex.Message); // ignored } } else { if (string.IsNullOrEmpty(user_id)) { user = _shipperHndBcontext.Users .Include(t => t.PhoneNumbers) .FirstOrDefault(t => t.UserProfileUrl.Equals(user_url)); } else { user = _shipperHndBcontext.Users .Include(t => t.PhoneNumbers) .FirstOrDefault(t => t.UserId.Equals(user_id)); } } List <Location> locations = _postBusiness.DectectLocation(message); List <Match> matches = _phoneNumberBusiness.DetetectPhoneNumber(message); if (matches != null && matches.Count > 0) { foreach (Match match in matches) { _phoneNumberBusiness.GetPhone(match, user); } } List <Location> addLocations = new List <Location>(); foreach (var location in locations) { addLocations.Add(_shipperHndBcontext.Locations.Add(location)); } try { _shipperHndBcontext.SaveChanges(); } catch (Exception exception) { _logControl.AddLog(1, "AddPosts", exception.Message); continue; } Post post = new Post { PostId = post_id, Message = message, User = user, Locations = addLocations, CreatedTime = DateTime.Now, FullPicture = full_picture }; _shipperHndBcontext.Posts.Add(post); try { _shipperHndBcontext.SaveChanges(); } catch (Exception e) { _logControl.AddLog(1, "AddPosts", e.Message); // ignored } } } } catch (Exception e) { _logControl.AddLog(1, "AddPosts", e.Message); return(e.Message); } return("Success"); }