コード例 #1
0
        public static bool TrySavePost(PostViewModel postView)
        {
            var emptyCategory = string.IsNullOrWhiteSpace(postView.Category);
            var emptyTitle    = string.IsNullOrWhiteSpace(postView.Title);
            var emptyContent  = !postView.Content.Any();

            if (emptyCategory || emptyTitle || emptyContent)
            {
                return(false);
            }

            var category = EnsureCategory(postView, ForumData.GetInstance());

            var posts  = ForumData.GetInstance().Posts;
            var postId = posts.Any() ? posts.Last().Id + 1 : 1;

            var author = UserService.GetUser(postView.Author);

            var content = string.Join("", postView.Content);

            var post = new Post(postId, postView.Title, content, category.Id, author.Id);

            posts.Add(post);
            author.Posts.Add(post.Id);
            category.Posts.Add(post.Id);

            ForumData.GetInstance().SaveChanges();

            postView.PostId = postId;

            return(true);
        }
コード例 #2
0
        public static PostViewModel GetPostViewModel(int postId)
        {
            var post          = ForumData.GetInstance().Posts.Find(p => p.Id == postId);
            var postViewModel = new PostViewModel(post);

            return(postViewModel);
        }
コード例 #3
0
        public static bool TryAddReply(PostViewModel post, ReplyViewModel reply)
        {
            if (reply.Content is null)
            {
                return(false);
            }

            var replyContent = string.Join("", reply.Content);

            if (string.IsNullOrWhiteSpace(replyContent))
            {
                return(false);
            }

            var replies = ForumData.GetInstance().Replies;

            var author  = ForumData.GetInstance().Users.Find(u => u.Username == reply.Author);
            var replyId = replies.Any() ? replies.Last().Id + 1 : 1;

            var replyModel = new Reply(replyId, replyContent, author.Id, post.PostId);

            replies.Add(replyModel);

            ForumData.GetInstance().SaveChanges();
            return(true);
        }
コード例 #4
0
        public static SignUpStatus TrySignUpUser(string username, string password)
        {
            var validUsername = !string.IsNullOrWhiteSpace(username) && username.Length > 3;
            var validPassword = !string.IsNullOrWhiteSpace(password) && password.Length > 3;

            if (!validUsername || !validPassword)
            {
                return(SignUpStatus.DetailsError);
            }

            var forumData = ForumData.GetInstance();

            bool userAlreadyExists = forumData.Users.Any(u => u.Username == username && u.Password == password);

            if (!userAlreadyExists)
            {
                //Look for the last entry and if there is no such return null. if the entry is null than make the first id be equal to 1
                var userId = forumData.Users.LastOrDefault()?.Id + 1 ?? 1;
                var user   = new User(userId, username, password);
                forumData.Users.Add(user);
                forumData.SaveChanges();

                return(SignUpStatus.Success);
            }

            return(SignUpStatus.UsernameTakenError);
        }
コード例 #5
0
        internal static string[] GetAllGategoryNames()
        {
            var allCategories = ForumData.GetInstance()
                                .Categories
                                .Select(c => c.Name)
                                .ToArray();

            return(allCategories);
        }
コード例 #6
0
        internal static IEnumerable <Post> GetPostsByCategory(int categoryId)
        {
            var postIds = ForumData.GetInstance().Categories
                          .First(c => c.Id == categoryId).Posts.ToHashSet();

            var posts = ForumData.GetInstance()
                        .Posts.Where(p => postIds.Contains(p.Id));

            return(posts);
        }
コード例 #7
0
        internal static IList <ReplyViewModel> GetPostReplies(int postId)
        {
            var post = ForumData.GetInstance().Posts.Find(p => p.Id == postId);

            var replies = ForumData.GetInstance().Replies
                          .Where(r => r.PostId == post.Id)
                          .Select(r => new ReplyViewModel(r))
                          .ToList();

            return(replies);
        }
コード例 #8
0
        /// <summary>
        /// Validate the username and password and check if it exists in the database
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public static bool TryLogInUser(string username, string password)
        {
            if (string.IsNullOrWhiteSpace(username) || string.IsNullOrWhiteSpace(password))
            {
                return(false);
            }

            var forumData = ForumData.GetInstance();

            bool userExists = forumData.Users.Any(u => u.Username == username && u.Password == password);

            return(userExists);
        }
コード例 #9
0
        internal static Category GetCategory(int categoryId)
        {
            var category = ForumData.GetInstance().Categories.Find(c => c.Id == categoryId);

            return(category);
        }
コード例 #10
0
        internal static User GetUser(string username)
        {
            var user = ForumData.GetInstance().Users.Find(u => u.Username == username);

            return(user);
        }
コード例 #11
0
        internal static User GetUser(int userId)
        {
            var user = ForumData.GetInstance().Users.Find(u => u.Id == userId);

            return(user);
        }