public static bool TrySavePost(PostViewModel postView) { bool emptyCategory = string.IsNullOrWhiteSpace(postView.Category); bool emptyTitle = string.IsNullOrWhiteSpace(postView.Title); bool emptyContent = !postView.Content.Any(); if (emptyCategory || emptyTitle || emptyContent) { return(false); } var forumData = new ForumData(); Category category = EnsureCategory(postView, forumData); int postId = forumData.Posts.Any() ? forumData.Posts.Last().Id + 1 : 1; User author = UserService.GetUser(postView.Author); int authorId = author.Id; var content = string.Join("", postView.Content); Post post = new Post(postId, postView.Title, content, category.Id, authorId, new List <int>()); forumData.Posts.Add(post); forumData.Users.Find(u => u.Id == authorId).PostIds.Add(post.Id); //author.PostIds.Add(post.Id); category.Posts.Add(post.Id); forumData.SaveChages(); postView.PostId = postId; return(true); }
public static bool TrySaveReply(ReplyViewModel replyView, int postId) { bool emptyContent = !replyView.Content.Any(); if (emptyContent) { return(false); } var forumData = new ForumData(); int replyId = forumData.Replies.Any() ? forumData.Replies.Last().Id + 1 : 1; User author = UserService.GetUser(replyView.Author); int authorId = author.Id; var content = string.Join("", replyView.Content); Reply reply = new Reply(replyId, content, authorId, postId); forumData.Replies.Add(reply); forumData.Posts.First(p => p.Id == postId).ReplyIds.Add(replyId); forumData.SaveChages(); //replyView.PostId = postId; return(true); }
public static SignUpStatus TrySignUpUser(string username, string password) { bool validUsername = !string.IsNullOrWhiteSpace(username) && username.Length > 3; bool validPassword = !string.IsNullOrWhiteSpace(password) && password.Length > 3; if (!validUsername || !validPassword) { return(SignUpStatus.DetailsError); } var forumData = new ForumData(); bool userAlreadyExists = forumData.Users.Any(u => u.Username == username); if (!userAlreadyExists) { int userId = forumData.Users.LastOrDefault()?.Id + 1 ?? 1; var user = new User(userId, username, password, new List <int>()); forumData.Users.Add(user); forumData.SaveChages(); return(SignUpStatus.Success); } return(SignUpStatus.UsernameTakenError); }