public static Guid SubmitNewComment(Guid AuthorKey, Guid postKey, string text) { using (var db = new FHNWPrototypeDB()) { Guid newGuid = Guid.NewGuid(); var post = db.Posts .Include("Author") .Include("Wall.Owner") .FirstOrDefault(x => x.Key == postKey); // var author = db.UserAccounts.Single(x => x.Key == AuthorKey); var author = db.BasicProfiles.FirstOrDefault(x => x.ReferenceKey == AuthorKey); Comment newComment = new Comment(); newComment.Author = author; newComment.Key = newGuid; newComment.PublishDateTime = DateTime.Now; newComment.Text = text; newComment.Post = post; db.Comments.Add(newComment); Suscription thisSuscription = null; Suscription thisSuscription2 = null; Event thisEvent = null; Event thisEvent2 = null; Notification notification = null; //suscribe the author to receive notifications from this post, from now on if (AuthorKey != post.Wall.Owner.ReferenceKey) { thisSuscription = new Suscription { Key = Guid.NewGuid(), Type = SuscriptionType.MyComment , Suscriber = author, ReferencePoint = newGuid }; thisSuscription2 = new Suscription { Key = Guid.NewGuid(), Type = SuscriptionType.CommentOnPostOnMyWall, Suscriber = post.Wall.Owner, ReferencePoint = newGuid }; db.Suscriptions.Add(thisSuscription); db.Suscriptions.Add(thisSuscription2); //register the event of liking the post thisEvent = new Event { Key = Guid.NewGuid(), PostOrComment = newGuid, Type = EventType.NewCommentOnPostOnMyWall , TriggeredBy = author, TriggeredOn = DateTime.Now }; thisEvent2 = new Event { Key = Guid.NewGuid(), PostOrComment = newGuid, Type = EventType.NewCommentOnPostILiked, TriggeredBy = author, TriggeredOn = DateTime.Now }; db.Events.Add(thisEvent); db.Events.Add(thisEvent2); //dont notify if its yourself notification = new Notification { Key = Guid.NewGuid(), Event = thisEvent, NotifiedTo = post.Wall.Owner }; db.Notifications.Add(notification); var suscriptions = db.Suscriptions.Where(x => x.ReferencePoint == post.Key && x.Type == SuscriptionType.PostILiked).ToList(); foreach (Suscription suscription in suscriptions) { db.Notifications.Add(new Notification { Key=Guid.NewGuid(), Event=thisEvent2 , NotifiedTo = suscription.Suscriber }); } } db.SaveChanges(); return newGuid; } }
public static Comment GetComment(Guid key) { Comment thisComment = new Comment(); using (var db = new FHNWPrototypeDB()) { var result = db.Comments .Include("Author") .Include("CommentLikes.Author") .Include("CommentLikes") .Include("Post") .FirstOrDefault(x => x.Key == key); thisComment.Key = result.Key; thisComment.Text = result.Text; thisComment.Post = result.Post; thisComment.PublishDateTime = result.PublishDateTime; thisComment.Author = result.Author; thisComment.CommentLikes = result.CommentLikes; } return thisComment; }