public static string ImportComments(InstagraphContext context, string xmlString) { var xml = XElement.Parse(xmlString); var jArray = new JArray(); foreach (var item in xml.Elements()) { var content = item.Element("content")?.Value; var username = item.Element("user")?.Value; var postId = item.Element("post")?.Attribute("id")?.Value; jArray.Add(new JObject( new JProperty("content", content), new JProperty("user", username), new JProperty("postId", postId))); } var commentsJson = JsonConvert.SerializeObject(jArray, Newtonsoft.Json.Formatting.Indented, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); var comments = JsonConvert.DeserializeObject <List <CommentImportDTO> >(commentsJson); var result = new StringBuilder(); comments.ForEach(c => { if (DeserializeHelper.IsUserExist(c.User, context) && DeserializeHelper.IsPostExist(c.PostId, context)) { var user = context.Users.First(u => u.Username == c.User); var post = context.Posts.Find(int.Parse(c.PostId)); var comment = Mapper.Map <Comment>(c, opt => { opt.Items["User"] = user; opt.Items["UserId"] = user.Id; opt.Items["Post"] = post; opt.Items["PostId"] = post.Id; }); context.Comments.Add(comment); result.AppendLine($"Successfully imported Comment {c.Content}."); } else { result.AppendLine(Constants.ErrorInvalidData); } }); context.SaveChanges(); return(result.ToString()); }
public static string ImportPosts(InstagraphContext context, string xmlString) { var xmlPosts = XDocument.Parse(xmlString); var jArray = new JArray(); foreach (var item in xmlPosts.Root.Elements()) { jArray.Add(new JObject( new JProperty("caption", item.Element("caption")?.Value), new JProperty("user", item.Element("user")?.Value), new JProperty("picture", item.Element("picture")?.Value))); } var jsonPosts = JsonConvert.SerializeObject(jArray, Newtonsoft.Json.Formatting.Indented, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); var posts = JsonConvert.DeserializeObject <List <PostImportDTO> >(jsonPosts); var result = new StringBuilder(); posts.ForEach(p => { if (DeserializeHelper.IsUserExist(p.User, context) && DeserializeHelper.IsPictureExist(p.Picture, context)) { var user = context.Users.First(u => u.Username == p.User); var picture = context.Pictures.First(p2 => p2.Path == p.Picture); var post = Mapper.Map <Post>(p, opt => { opt.Items["User"] = user; opt.Items["Picture"] = picture; }); context.Posts.Add(post); result.AppendLine($"Successfully imported Post {p.Caption}."); } else { result.AppendLine(Constants.ErrorInvalidData); } }); context.SaveChanges(); return(result.ToString()); }