Advertisement
Guest User

CSharp-DatabaseAdvanced - ExamPrep I

a guest
Dec 18th, 2017
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.43 KB | None | 0 0
  1.  public static string ImportPosts(InstagraphContext context, string xmlString)
  2.         {
  3.             StringBuilder sb = new StringBuilder();
  4.            
  5.             // защо не се десериализират с вградения метод за десерилизация:
  6.             //var serializer = new XmlSerializer(typeof(PostDto[]), new XmlRootAttribute("posts"));
  7.             //var deserializedPosts = (PostDto[])serializer.Deserialize(new MemoryStream(Encoding.UTF8.GetBytes(xmlString)));
  8.            
  9.  
  10.             var xml = XDocument.Parse(xmlString);
  11.             var deserializedPosts = xml.Root.Elements();
  12.            
  13.             var validPosts = new List<Post>();
  14.  
  15.             foreach (var post in deserializedPosts)
  16.             {
  17.                 string caption = post.Element("caption")?.Value;
  18.                 string user = post.Element("user")?.Value;
  19.                 string picture = post.Element("picture")?.Value;
  20.                
  21.                 bool isValid = !String.IsNullOrWhiteSpace(caption) &&
  22.                                 !String.IsNullOrWhiteSpace(user) &&
  23.                                 !String.IsNullOrWhiteSpace(picture);
  24.  
  25.                 if (!isValid)
  26.                 {
  27.                     sb.AppendLine(errorMsg);
  28.                     continue;
  29.                 }
  30.  
  31.                 bool isUserExists = context.Users.Any(u => u.Username == user);
  32.                 bool isPicureExists = context.Pictures.Any(u => u.Path == picture);
  33.  
  34.                 Post currPost = null;
  35.                 if (isPicureExists && isUserExists)
  36.                 {                    
  37.                     var currUser= context.Users.FirstOrDefault(u => u.Username == user);
  38.                     var currPicure = context.Pictures.FirstOrDefault(u => u.Path == picture);
  39.                     currPost = new Post()
  40.                     {
  41.                         Caption = caption,
  42.                         User = currUser,
  43.                         Picture = currPicure
  44.                     };
  45.                     validPosts.Add(currPost);
  46.                     sb.AppendLine(String.Format(successMsg, $"Post {currPost.Caption}"));
  47.                 }
  48.                 else
  49.                 {
  50.                     sb.AppendLine(errorMsg);
  51.                     continue;
  52.                 }
  53.  
  54.             }
  55.             context.Posts.AddRange(validPosts);
  56.             context.SaveChanges();
  57.  
  58.             return sb.ToString().TrimEnd();        
  59.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement