예제 #1
0
파일: Post.cs 프로젝트: sagioto/forum
 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;
 }
예제 #2
0
 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");
 }
예제 #3
0
 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;
     }
 }
예제 #4
0
 public Result RemovePost(Postkey postkey, string username, string password)
 {
     return webService.RemovePost(postkey, username, password);
 }
예제 #5
0
 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;
 }
예제 #6
0
        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;
            }
        }
예제 #7
0
        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;
            }
        }
예제 #8
0
        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;
            }
        }
예제 #9
0
        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;
            }
        }
예제 #10
0
        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);
            }
        }
예제 #11
0
 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);
     }
 }
예제 #12
0
 public Post[] GetReplies(Postkey postkey)
 {
     try
     {
         return controller.GetReplies(postkey);
     }
     catch (Exception e)
     {
         throw new FaultException<Exception>(e, e.Message);
     }
 }
예제 #13
0
 public string SerializePostkey(Postkey toSerialize)
 {
     return serializer.Serialize(toSerialize);
 }
예제 #14
0
 /// <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);
 }
예제 #15
0
 /// <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;
     }
 }
예제 #16
0
 internal Post GetPost(Postkey postkey)
 {
     try
     {
         return webService.GetPost(postkey);
     }
     catch (System.ServiceModel.FaultException e)
     {
         throw e;
     }
 }
예제 #17
0
 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;
     }
 }
예제 #18
0
 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);
     }
 }
예제 #19
0
 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;
     }
 }
예제 #20
0
 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;
     }
 }
예제 #21
0
        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;
            }
        }
예제 #22
0
 public Post GetPost(Postkey postkey)
 {
     Post res;
     GetPost(postkey, out res);
     if (res != null)
     {
         return res;
     }
     else
     {
         throw new PostNotFoundException();
     }
 }
예제 #23
0
 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;
     }
 }
예제 #24
0
 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;
 }
예제 #25
0
        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;
            }
        }
예제 #26
0
        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;
                }
            }
        }
예제 #27
0
        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;
        }
예제 #28
0
 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);
             }
         }
     //}
 }
예제 #29
0
 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;
 }
예제 #30
0
 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;
 }