public bool AddPost(Post post, string subforum) { //Getting last id: IEnumerable<int> postKeysList = (from m in ForumContext.PostKeyEntities select m.PostKeyId); int lastId = 0; if (postKeysList.Count() != 0) { lastId = postKeysList.Max(); } currentPostKeyId = lastId + 1; PostKeyEntity pke = new PostKeyEntity(); pke.PostKeyId = currentPostKeyId; pke.Username = post.Key.Username; pke.Time = post.Key.Time; PostEntity pe = new PostEntity(); pe.PostKeyId = currentPostKeyId; pe.ParentPostKeyId = -1; pe.Title = post.Title; pe.Body = post.Body; pe.SubforumName = subforum; //TODO - Why do we need 'subforum'? try { ForumContext.AddToPostKeyEntities(pke); ForumContext.AddToPostEntities(pe); ForumContext.SaveChanges(); return true; } catch (Exception) { //TODO throw; } }
private Post PostEntityToPost(PostEntity pe) { Postkey parentPostKey = null; // Finding parent`s postkey: if (pe.ParentPostKeyId != -1) { IEnumerable<PostKeyEntity> parentPostkeyQuery = from pk in ForumContext.PostKeyEntities where pk.PostKeyId == pe.ParentPostKeyId select pk; PostKeyEntity pke = parentPostkeyQuery.ElementAt<PostKeyEntity>(0); parentPostKey = new Postkey(pke.Username, pke.Time); } // Finding PostKey: IEnumerable<PostKeyEntity> PostkeyQuery = from pk in ForumContext.PostKeyEntities where pk.PostKeyId == pe.PostKeyId select pk; PostKeyEntity postkeyEntity = PostkeyQuery.First(); Postkey postkey = new Postkey(postkeyEntity.Username, postkeyEntity.Time); Post PostToReturn = new Post(postkey, pe.Title, pe.Body, parentPostKey, pe.SubforumName); // Replies: IEnumerable<PostEntity> repliesList = from r in ForumContext.PostEntities where r.ParentPostKeyId == pe.PostKeyId select r; Dictionary<Postkey, Post> repliesDictionary = new Dictionary<Postkey, Post>(); if (repliesList.Count() != 0) { foreach (PostEntity reply in repliesList) { Post p = PostEntityToPost(reply); repliesDictionary.Add(p.Key, p); } PostToReturn.HasReplies = true; } else { PostToReturn.HasReplies = false; } PostToReturn.Replies = repliesDictionary; return PostToReturn; }
/// <summary> /// Create a new PostEntity object. /// </summary> /// <param name="postKeyId">Initial value of the PostKeyId property.</param> /// <param name="title">Initial value of the Title property.</param> /// <param name="subforumName">Initial value of the SubforumName property.</param> public static PostEntity CreatePostEntity(global::System.Int32 postKeyId, global::System.String title, global::System.String subforumName) { PostEntity postEntity = new PostEntity(); postEntity.PostKeyId = postKeyId; postEntity.Title = title; postEntity.SubforumName = subforumName; return postEntity; }
public bool AddReply(Post reply, Postkey postKey) { try { // Find parent postkey: IEnumerable<PostKeyEntity> postkeyQuery = GetPostKeyEntity(postKey); IEnumerable<PostEntity> postQuery = GetPostEntity(postKey); //Getting last id: IEnumerable<int> postKeysList = (from m in ForumContext.PostKeyEntities select m.PostKeyId); int lastId = 0; if (postKeysList.Count() != 0) { lastId = postKeysList.Max(); } currentPostKeyId = lastId + 1; PostKeyEntity pke = new PostKeyEntity(); pke.PostKeyId = currentPostKeyId; pke.Username = reply.Key.Username; pke.Time = reply.Key.Time; ForumContext.AddToPostKeyEntities(pke); PostEntity pe = new PostEntity(); pe.PostKeyId = currentPostKeyId; //currentPostKeyId++; pe.ParentPostKeyId = postkeyQuery.ElementAt(0).PostKeyId; pe.Title = reply.Title; pe.Body = reply.Body; pe.SubforumName = postQuery.ElementAt(0).SubforumName; ForumContext.AddToPostEntities(pe); ForumContext.SaveChanges(); return true; } catch (Exception) { //TODO throw; } }
/// <summary> /// Deprecated Method for adding a new object to the PostEntities EntitySet. Consider using the .Add method of the associated ObjectSet<T> property instead. /// </summary> public void AddToPostEntities(PostEntity postEntity) { base.AddObject("PostEntities", postEntity); }