public Post(Postkey postKey, string title, string body, Postkey parentPost, string subforum) { this.key = postKey; this.title = title; this.parentPost = parentPost; this.subforum = subforum; this.replies = new Dictionary<Postkey, Post>(); this.body = body; }
public void AddPostServerTests() { DataManager target = new DataManager(); target.CleanForumData(); User user = new User("user", "user"); target.AddUser(user); target.AddSubforum(new Subforum("subforumName")); Postkey pk = new Postkey("user", DateTime.Now); Post p = new Post(pk, "Post", "body", null, "subforumName"); bool actual = target.AddPost(p, "subforumName"); Post p2 = target.GetPost(pk); Assert.IsTrue(actual); Assert.AreEqual(p.Key.Time, p2.Key.Time); Assert.AreEqual(p.Key.Username, p2.Key.Username); Assert.AreEqual(p.Title, p2.Title); Assert.AreEqual(p.Body, p2.Body); target.RemoveSubforum("subforumName"); }
public bool AddReply(Post reply, Postkey originalPost) { Post post; GetPost(originalPost, out post); if (post == null) throw new PostNotFoundException(); try { post.HasReplies = true; subforumsList[post.Subforum].TotalPosts++; post.Replies.Add(reply.Key, reply); return true; //return UpdatePost(oldPost); } catch (Exception ex) { throw ex; } }
public Result RemovePost(Postkey postkey, string username, string password) { return webService.RemovePost(postkey, username, password); }
private IEnumerable<PostKeyEntity> GetPostKeyEntity(Postkey postkey) { IEnumerable<PostKeyEntity> postkeyQuery = from pk in ForumContext.PostKeyEntities where pk.Username == postkey.Username && pk.Time.Hour == postkey.Time.Hour && pk.Time.Minute == postkey.Time.Minute && pk.Time.Second == postkey.Time.Second && pk.Time.Year == postkey.Time.Year && pk.Time.Month == postkey.Time.Month && pk.Time.Day == postkey.Time.Day select pk; return postkeyQuery; }
public Post GetPost(Postkey postkey) { try { IEnumerable<PostEntity> postsQuery = GetPostEntity(postkey); if (postsQuery.Count() != 1) { // Post wasn't found or more than one was found return null; } else { PostEntity pe = postsQuery.First(); return PostEntityToPost(pe); } } catch (Exception) { //TODO throw; } }
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; } }
public Result RemovePost(Postkey originalPostKey, string username, string password) { try { log.Info("got request to remove post " + originalPostKey); Result res = securityManager.IsAuthorizedToEdit(username, originalPostKey, password) | policyManager.IsAuthorizedToEdit(originalPostKey, username); if (res == Result.OK) if (dataManager.RemovePost(originalPostKey)) return Result.OK; else return Result.POST_NOT_FOUND; else return res; } catch (Exception e) { log.Error("failed to remove post " + originalPostKey, e); throw e; } }
public Post GetPost(Postkey postkey) { try { log.Info("got request to get post"); return dataManager.GetPost(postkey); } catch (Exception e) { log.Error("failed to get post", e); throw e; } }
public Result Reply(Postkey current, Post toPost) { try { toPost.Replies = new Dictionary<Postkey, Post>(); return controller.Reply(current, toPost); } catch (Exception e) { throw new FaultException<Exception>(e, e.Message); } }
public Result RemovePost(Postkey postkey, string username, string password) { try { return controller.RemovePost(postkey, username, password); } catch (Exception e) { throw new FaultException<Exception>(e, e.Message); } }
public Post[] GetReplies(Postkey postkey) { try { return controller.GetReplies(postkey); } catch (Exception e) { throw new FaultException<Exception>(e, e.Message); } }
public string SerializePostkey(Postkey toSerialize) { return serializer.Serialize(toSerialize); }
/// <summary> /// Add a reply to a post. /// </summary> /// <param name="originalPost">The post being replied</param> /// <param name="newReply">The new reply post</param> /// <returns>Returns true if reply succeeded, false otherwise</returns> internal Result Reply(Postkey originalPost, Post newReply) { return webService.Reply(originalPost, newReply); }
/// <summary> /// Get a Post using its PostKey /// </summary> /// <param name="postkey">A post key consisting of the user + timestamp</param> /// <returns>The required Post</returns> internal Post[] GetReplies(Postkey postkey) { try { return webService.GetReplies(postkey); } catch (System.Exception e) { throw e; } }
internal Post GetPost(Postkey postkey) { try { return webService.GetPost(postkey); } catch (System.ServiceModel.FaultException e) { throw e; } }
public Result EditPost(Postkey currPost, Post post, string username, string password) { try { log.Info("got request to edit post " + currPost); if (!CheckPost(post)) return Result.ILLEGAL_POST; Result res = securityManager.IsAuthorizedToEdit(username, currPost, password); if (res == Result.OK) if (dataManager.EditPost(post, currPost)) return Result.OK; else return Result.POST_NOT_FOUND; else return res; } catch (Exception e) { log.Error("failed to edit post " + currPost, e); throw e; } }
public Result EditPost(Postkey oldPost, Post newPost, string username, string password) { try { return controller.EditPost(oldPost, newPost, username, password); } catch (Exception e) { throw new FaultException<Exception>(e, e.Message); } }
public Post[] GetReplies(Postkey key) { try { if (key != null) { log.Info("got request for post " + key); return dataManager.GetPost(key).Replies.Values.OrderBy(post => post.Key.Time).ToArray(); } else { log.Info("got a null postkey in get replies"); return null; } } catch (Exception e) { log.Error("failed get post " + key, e); throw e; } }
public bool EditPost(Post postToUpdate, Postkey originalPost) { Post oldPost; GetPost(originalPost, out oldPost); if (oldPost == null) throw new PostNotFoundException(); try { oldPost.Body = postToUpdate.Body; oldPost.Title = postToUpdate.Title; return true; //return UpdatePost(oldPost); } catch (Exception ex) { throw ex; } }
public Result Reply(Postkey currPost, Post post) { try { log.Info("got request to reply to post " + currPost); if (!CheckPost(post)) return Result.ILLEGAL_POST; Result res = securityManager.IsAuthorizedToPost(post.Key.Username, post.Subforum) ; if (res == Result.OK) if (dataManager.AddReply(post, currPost)) { this.posted = post; Notify(); return Result.OK; } else return Result.POST_NOT_FOUND; else return res; } catch (Exception e) { log.Error("failed to reply to post " + currPost, e); throw e; } }
public Post GetPost(Postkey postkey) { Post res; GetPost(postkey, out res); if (res != null) { return res; } else { throw new PostNotFoundException(); } }
public bool EditPost(Post postToUpdate, Postkey oldPostKey) { try { PostEntity post = GetPostEntity(oldPostKey).First(); post.Title = postToUpdate.Title; post.Body = postToUpdate.Body; //TODO - Do username & datetime (Postkey) change on update? ForumContext.SaveChanges(); return true; } catch (Exception) { //TODO throw; } }
public bool RemovePost(Postkey postkey) { foreach (KeyValuePair<string, Subforum> subforumEntry in subforumsList) { try { if (subforumEntry.Value.Posts.ContainsKey(postkey)) { subforumsList[subforumEntry.Key].Posts.Remove(postkey); subforumsList[subforumEntry.Key].TotalPosts--; return true; } else { return RemoveReply(postkey, subforumsList[subforumEntry.Key].Posts); } } catch (Exception ex) { throw ex; } } return false; }
public bool RemovePost(Postkey postkey) { try { // Find Postkey IEnumerable<PostKeyEntity> postkeyQuery = GetPostKeyEntity(postkey); // Find Post PostKeyEntity pke = postkeyQuery.First(); IEnumerable<PostEntity> postQuery = GetPostEntity(postkey); ForumContext.PostKeyEntities.DeleteObject(pke); ForumContext.PostEntities.DeleteObject(postQuery.ElementAt<PostEntity>(0)); ForumContext.SaveChanges(); return true; } catch (Exception) { return false; //TODO throw; } }
private void GetPost(Postkey postkey, out Post returnedPost) { returnedPost = null; foreach (KeyValuePair<string, Subforum> subforumEntry in subforumsList) { if (returnedPost != null) { break; } try { foreach (Post PostToReturn in subforumEntry.Value.Posts.Values) { if (PostToReturn.Key.CompareTo(postkey) == 0) { returnedPost = PostToReturn; break; } } if (returnedPost != null) { break; } else { //if (subforumEntry.Value.Posts.ContainsKey(postkey)) // If oldPost is main oldPost in subforumName //{ // returnedPost = subforumsList[subforumEntry.Key].Posts[postkey]; // break; //} //else // If oldPost is not main oldPost, search oldPost in replies & update //{ GetReply(postkey, subforumsList[subforumEntry.Key].Posts, out returnedPost); if (returnedPost != null) break; } } catch (Exception ex) { throw ex; } } }
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; }
private void GetReply(Postkey postkey, Dictionary<Postkey, Post> postsList, out Post returnedPost) { returnedPost = null; foreach (Post PostToReturn in postsList.Values) { if (PostToReturn.Key.CompareTo(postkey) == 0) { returnedPost = PostToReturn; break; } } if (returnedPost != null) { return; } //if (postsList.ContainsKey(postkey)) //{ // returnedPost = postsList[postkey]; // return; //} //else //{ foreach (Post reply in postsList.Values) { if (returnedPost == null) { GetReply(postkey, reply.Replies, out returnedPost); } } //} }
public Result IsAuthorizedToEdit(string username, Postkey postkey, string password) { User user = dataManager.GetUser(username); if (user == null) return Result.USER_NOT_FOUND; Post post = dataManager.GetPost(postkey); if (post == null) return Result.POST_NOT_FOUND; Subforum sub = dataManager.GetSubforums().Find(subforum => subforum.Name.Equals(post.Subforum)); if (user.Password.Equals(password) && (post.Key.Username.Equals(username))) //|| (user.Level.Equals(AuthorizationLevel.MODERATOR) // && sub != null && sub.ModeratorsList.Contains(username)) //|| (user.Level.Equals(AuthorizationLevel.ADMIN)))) if ((user.CurrentState == UserState.Active) || (user.CurrentState == UserState.ShouldBeBanned)) { return Result.OK; } else //if (user.CurrentState == UserState.NotActive) { //dataManager.SetUserState(username, UserState.ShouldBeBanned); return Result.ILLEGAL_POST; } if (!post.Key.Username.Equals(username)) { user.CurrentState = UserState.ShouldBeBanned; dataManager.UpdateUser(user); } return Result.INSUFFICENT_PERMISSIONS; }
private bool RemoveReply(Postkey postkey, Dictionary<Postkey, Post> repliesList) { foreach (Post reply in repliesList.Values) { if (reply.Replies.ContainsKey(postkey)) { reply.Replies.Remove(postkey); subforumsList[reply.Subforum].TotalPosts--; reply.HasReplies = !(reply.Replies.Values.Count == 0); // If reply.Replies.Values.Count > 0 then HasReplies==true; return true; } else { return RemoveReply(postkey, reply.Replies); } } return false; }