public int AddPost(int userId, string postTitle, string postCategory, string postContent) { bool emptyCategory = string.IsNullOrWhiteSpace(postCategory); bool emptyTitle = string.IsNullOrWhiteSpace(postTitle); bool emptyContent = string.IsNullOrWhiteSpace(postContent); if (emptyCategory || emptyContent || emptyTitle) { throw new ArgumentException("All fields must be filled!"); } Category category = this.EnsureCategory(postCategory); int postId = forumData.Posts.Any() ? forumData.Posts.Last().Id + 1 : 1; User author = this.userService.GetUserById(userId); Post post = new Post(postId, postTitle, postContent, category.Id, userId, new List <int>()); forumData.Posts.Add(post); author.Posts.Add(post.Id); category.Posts.Add(post.Id); forumData.SaveChanges(); return(post.Id); }
public bool TrySignUpUser(string username, string password) { bool validUsername = !string.IsNullOrWhiteSpace(username) && password.Length > 3; bool validPassword = !string.IsNullOrWhiteSpace(password) && password.Length > 3; if (!validUsername || !validPassword) { throw new ArgumentException(INVALID_USERNAME_OR_PASSWORD); } bool userAlreadyExists = forumData.Users.Any(u => u.Username == username); if (userAlreadyExists) { throw new InvalidOperationException(USERNAME_TAKEN); } int userId = forumData.Users.LastOrDefault()?.Id + 1 ?? 1; User user = new User(userId, username, password); forumData.Users.Add(user); forumData.SaveChanges(); this.TryLogInUser(username, password); return(true); }
public static SignUpStatus TrySignUpUser(string username, string password) { bool isUsernameValid = !String.IsNullOrWhiteSpace(username) && username.Length > 3; bool isPasswordValid = !String.IsNullOrWhiteSpace(password) && password.Length > 3; if (!isUsernameValid || !isPasswordValid) { return(SignUpStatus.DetailsError); } ForumData forumData = new ForumData(); bool userExists = forumData.Users.Any(u => u.Username == username); if (!userExists) { int userId = forumData.Users.LastOrDefault()?.Id + 1 ?? 1; User user = new User(userId, username, password); forumData.Users.Add(user); forumData.SaveChanges(); return(SignUpStatus.Success); } else { return(SignUpStatus.UsernameTakenError); } }
public static bool TrySavePost(PostViewModel postViewModel) { bool isCategoryEmpty = string.IsNullOrWhiteSpace(postViewModel.Category); bool isTitleEmpty = string.IsNullOrWhiteSpace(postViewModel.Title); bool isContentEmpty = !postViewModel.Content.Any(); if (isCategoryEmpty || isTitleEmpty || isContentEmpty) { return(false); } ForumData forumData = new ForumData(); Category category = EnsureCategory(postViewModel, forumData); //int postId = forumData.Posts.Any() ? forumData.Posts.Last().Id + 1 : 1; int postId = forumData.Posts.Count + 1; User author = UserService.GetUser(postViewModel.Author, forumData); int authorId = author.Id; string content = string.Join(string.Empty, postViewModel.Content); Post post = new Post(postId, postViewModel.Title, content, category.Id, authorId); forumData.Posts.Add(post); author.PostIds.Add(postId); category.PostIds.Add(postId); forumData.SaveChanges(); postViewModel.PostId = postId; return(true); }
public static bool TrySavePost(PostViewModel postViewModel) { bool isTitleValid = !string.IsNullOrWhiteSpace(postViewModel.Title); bool isContentValid = postViewModel.Content.Any(); bool isCategoryValid = !string.IsNullOrWhiteSpace(postViewModel.Category); if (!isTitleValid || !isContentValid || isCategoryValid) { return(false); } ForumData forumData = new ForumData(); Category category = EnsureCategory(postViewModel, forumData); int postId = forumData.Posts.Last()?.Id + 1 ?? 1; User author = forumData.Users.Single(u => u.Username == postViewModel.Author); string content = string.Join("", postViewModel.Content); Post post = new Post(postId, postViewModel.Title, content, category.Id, author.Id, new List <int>()); forumData.Posts.Add(post); category.PostIds.Add(postId); author.PostIds.Add(postId); forumData.SaveChanges(); postViewModel.PostId = postId; return(true); }
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); } ForumData 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; string content = string.Join("", postView.Content); Post post = new Post(postId, postView.Title, content, category.Id, authorId, new List <int>()); forumData.Posts.Add(post); author.PostIds.Add(postId); category.PostIds.Add(postId); forumData.SaveChanges(); postView.PostId = postId; return(true); }
public static bool TrySaveReply(ReplyViewModel replyViewModel, int postId) { if (!replyViewModel.Content.Any()) { return(false); } var forumData = new ForumData(); var author = UserService.GetUser(replyViewModel.Author, forumData); var post = forumData.Posts.Single(p => p.Id == postId); var replyId = forumData.Replies.LastOrDefault()?.Id + 1 ?? 1; var content = string.Join("", replyViewModel.Content); var reply = new Reply(replyId, content, author.Id, postId); forumData.Replies.Add(reply); post.ReplyIds.Add(replyId); forumData.SaveChanges(); return(true); }
public static AddPostStatus TrySavePost(PostViewModel postViewModel) { if (String.IsNullOrWhiteSpace(postViewModel.Title) || postViewModel.Title.Length < 4 || postViewModel.Title.Length > 64) { return(AddPostStatus.TitleError); } if (String.IsNullOrWhiteSpace(postViewModel.Category) || postViewModel.Category.Length < 4 || postViewModel.Category.Length > 32) { return(AddPostStatus.CategoryError); } if (!postViewModel.Content.Any()) { return(AddPostStatus.ContentError); } ForumData forumData = new ForumData(); Category category = EnsureCategory(postViewModel, forumData); int postId = forumData.Posts.LastOrDefault()?.Id + 1 ?? 1; User author = forumData.Users.Find(u => u.Username == postViewModel.Author); string content = String.Join("", postViewModel.Content); Post post = new Post(postId, postViewModel.Title, content, category.Id, author.Id); forumData.Posts.Add(post); author.PostIds.Add(postId); category.PostIds.Add(postId); forumData.SaveChanges(); postViewModel.PostId = postId; return(AddPostStatus.Success); }
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); } ForumData forumData = new ForumData(); bool userAlreadyExists = forumData.Users.Any(u => u.Username == username); if (!userAlreadyExists) { int userId = forumData.Users.LastOrDefault()?.Id + 1 ?? 1; User user = new User(userId, username, password, new List <int>()); forumData.Users.Add(user); forumData.SaveChanges(); return(SignUpStatus.Success); } return(SignUpStatus.UsernameTakenError); }
public static SignUpStatus TrySignUpUser(string username, string password) { bool isUsernameValid = IsValid(username, UsernameMinLength); bool isPasswordValid = IsValid(password, PasswordMinLength); if (!isUsernameValid || !isPasswordValid) { return(SignUpStatus.DetailsError); } ForumData forumData = new ForumData(); bool userAlreadyExists = forumData.Users.Any(user => user.Username == username); if (!userAlreadyExists) { //int userId = forumData.Users.LastOrDefault()?.Id + 1 ?? 1; int userId = forumData.Users.Count + 1; User user = new User(userId, username, password); forumData.Users.Add(user); forumData.SaveChanges(); return(SignUpStatus.Success); } return(SignUpStatus.UsernameTakenError); }
public static SignUpController.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(SignUpController.SignUpStatus.DetailsError); } var forumData = new ForumData(); var userAllreadyExists = forumData.Users.Any(u => u.Username == username); if (userAllreadyExists) { return(SignUpController.SignUpStatus.UsernameTakenError); } var userId = forumData.Users.LastOrDefault()?.Id + 1 ?? 1; var user = new User(userId, username, password); forumData.Users.Add(user); forumData.SaveChanges(); return(SignUpController.SignUpStatus.Success); }
internal static bool TryAddReply(int postId, ReplyViewModel replyViewModel) { ForumData forumData = new ForumData(); bool emptyReply = !replyViewModel.Content.Any(); if (emptyReply) { return(false); } else { int replyId = forumData.Replies.Any() ? forumData.Replies.Last().Id + 1 : 1; string content = string.Join(string.Empty, replyViewModel.Content); int authorId = UserService.GetUserByName(replyViewModel.Author).Id; Reply reply = new Reply(replyId, content, authorId, postId); Post post = forumData.Posts.FirstOrDefault(p => p.Id == postId); forumData.Replies.Add(reply); post.ReplyIds.Add(replyId); forumData.SaveChanges(); return(true); } }
internal static bool TryAddReply(int postId, ReplyViewModel replyViewModel) { var forumData = new ForumData(); var emptyReply = !replyViewModel.Content.Any(); if (emptyReply) { return(false); } var replyId = forumData.Replies.Any() ? forumData.Replies.Last().Id + 1 : 1; var content = string.Join("", replyViewModel.Content); var authorId = UserService.GetUser(replyViewModel.Author).Id; var reply = new Reply(replyId, content, authorId, postId); var post = forumData.Posts.FirstOrDefault(p => p.Id == postId); forumData.Replies.Add(reply); post.ReplyIds.Add(replyId); forumData.SaveChanges(); return(true); }
public static bool TrySaveReply(int postId, ReplyViewModel replyView) { ForumData forumData = new ForumData(); bool emptyContent = !replyView.Content.Any(); if (emptyContent) { return(false); } int replyId = forumData.Replies.Any() ? forumData.Replies.Last().Id + 1 : 1; string content = string.Join("", replyView.Content); var authorId = UserService.GetUser(replyView.Author).Id; Reply reply = new Reply(replyId, content, authorId, postId); Post post = forumData.Posts.FirstOrDefault(p => p.Id == postId); forumData.Replies.Add(reply); post.ReplyIds.Add(replyId); forumData.SaveChanges(); return(true); }
public static bool TrySaveReply(ReplyViewModel replyView) { bool emptyAuthor = string.IsNullOrWhiteSpace(replyView.Author); bool emptyContent = !replyView.Content.Any(); if (emptyAuthor || emptyContent) { return(false); } ForumData forumData = new ForumData(); User author = UserService.GetUser(replyView.Author); int postId = forumData.Posts.Any() ? forumData.Posts.Last().Id + 1 : 1; int replyId = forumData.Replies.Any() ? forumData.Replies.Last().Id + 1 : 1; int authorId = author.Id; string content = string.Join("", replyView.Content); Reply reply = new Reply(replyId, content, authorId, postId); //forumData.Posts.Add(post); //author.PostIds.Add(post.Id); //category.PostIds.Add(post.Id); //forumData.SaveChanges(); //postView.PostId = postId; forumData.Replies.Add(reply); author.PostIds.Add(replyId); forumData.SaveChanges(); return(true); }
internal static bool TrySavePost(PostViewModel postView) { var emptyCategory = string.IsNullOrWhiteSpace(postView.Category); var emptyTitle = string.IsNullOrWhiteSpace(postView.Title); var emptyContent = !postView.Content.Any(); if (emptyCategory || emptyContent || emptyTitle) { return(false); } var forumData = new ForumData(); var category = EnsureCategory(postView, forumData); var postId = forumData.Posts.Any() ? forumData.Posts.Last().Id + 1 : 1; var author = UserService.GetUser(postView.Author); var authorId = author.Id; var content = string.Join(string.Empty, postView.Content); var post = new Post(postId, postView.Title, content, category.Id, authorId, new List <int>()); forumData.Posts.Add(post); author.PostIds.Add(post.Id); category.Posts.Add(post.Id); forumData.SaveChanges(); postView.PostId = postId; return(true); }
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 || emptyContent || emptyTitle) { return(false); } var forumData = new ForumData(); var category = EnsureCategory(postView, forumData); var postId = forumData.Posts.Last()?.Id + 1 ?? 1; var author = forumData.Users.Single(u => u.Username == postView.Author); string cotent = string.Join("", postView.Content); Post post = new Post(postId, postView.Title, cotent, category.Id, author.Id, new List <int>()); forumData.Posts.Add(post); category.PostsIds.Add(postId); author.PostIds.Add(postId); author.PostIds.Add(postId); forumData.SaveChanges(); return(true); }
public static bool TrySavePost(PostViewModel postViewModel) { var isTitleValid = !string.IsNullOrWhiteSpace(postViewModel.Title); var isContentValid = postViewModel.Content.Any(); var isCategoryValid = !string.IsNullOrWhiteSpace(postViewModel.Category); if (!isTitleValid || !isContentValid || !isCategoryValid) { return(false); } var forumData = new ForumData(); var category = EnsureCategory(postViewModel, forumData); var postId = forumData.Posts.LastOrDefault()?.Id + 1 ?? 1; var author = UserService.GetUser(postViewModel.Author, forumData); var content = string.Join("", postViewModel.Content); var post = new Post(postId, postViewModel.Title, content, category.Id, author.Id, new List <int>()); forumData.Posts.Add(post); category.PostsIds.Add(postId); author.PostsIds.Add(postId); forumData.SaveChanges(); postViewModel.PostId = postId; return(true); }
public static bool TrySavePost(PostViewModel pvm) { bool emptyCategory = string.IsNullOrWhiteSpace(pvm.Category); bool emptyTitle = string.IsNullOrWhiteSpace(pvm.Title); bool emptyContent = !pvm.Content.Any(); if (emptyCategory || emptyTitle || emptyContent) { return(false); } ForumData forumData = new ForumData(); Category category = EnsureCategory(pvm, forumData); int postId = forumData.Posts.Any() ? forumData.Posts.LastOrDefault().Id + 1 : 1; User author = forumData.Users.Single(u => u.Username == pvm.Author); int authorId = author.Id; string content = string.Join("", pvm.Content); Post post = new Post(postId, pvm.Title, content, category.Id, authorId, new List <int>()); forumData.Posts.Add(post); author.PostIds.Add(post.Id); category.PostIds.Add(post.Id); forumData.SaveChanges(); pvm.PostId = postId; return(true); }
public bool TrySignUpUser(string username, string password) { var validUsername = !string.IsNullOrWhiteSpace(username) && username.Length > 3; var validPassword = !string.IsNullOrWhiteSpace(password) && password.Length > 3; if (!validUsername || !validPassword) { throw new ArgumentException("Username and Password must be longer than 3 symbols!"); } var userAlreadyExists = forumData.Users.Any(u => u.Username == username); if (userAlreadyExists) { throw new InvalidOperationException("Username taken!"); } var userId = forumData.Users.LastOrDefault()?.Id + 1 ?? 1; var user = new User(userId, username, password); forumData.Users.Add(user); forumData.SaveChanges(); this.TryLogInUser(username, password); return(true); }
public bool TrySignUpUser(string username, string password) { bool validUsername = !string.IsNullOrEmpty(username) && username.Length > 3; bool validPassword = !string.IsNullOrEmpty(password) && password.Length > 3; if (!validUsername || !validPassword) { throw new ArgumentException("Username and Password have to be more than 3 symbols long!"); } bool userAlreadyExist = this.forumData.Users.Any(u => u.Username.Equals(username)); if (userAlreadyExist) { throw new ArgumentException("Username taken!"); } int userId = this.forumData.Users.LastOrDefault()?.Id + 1 ?? 1; User user = new User(userId, username, password); this.forumData.Users.Add(user); forumData.SaveChanges(); this.TryLogInUser(username, password); return(true); }
internal static bool TryAddReply(int postId, ReplyViewModel replyView) { bool emptyAuthor = string.IsNullOrWhiteSpace(replyView.Author); bool emptyContent = !replyView.Content.Any(x => x.Trim().Length > 0); if (emptyContent || emptyAuthor) { return(false); } var forumData = new ForumData(); var replies = forumData.Replies; int replyId = replies.Any() ? replies.Last().Id + 1 : 1; User author = UserService.GetUser(replyView.Author); Post post = forumData.Posts.Find(p => p.Id == postId); int authorId = author.Id; string content = string.Join("", replyView.Content); var reply = new Reply(replyId, content, authorId, postId); forumData.Replies.Add(reply); post.ReplyIds.Add(replyId); forumData.SaveChanges(); return(true); }
private static Category EnsureCategory(PostViewModel postView, ForumData forumData) { var categoryName = postView.Category; Category category = forumData.Categories.FirstOrDefault(c => c.Name == categoryName); if (category == null) { int categoryId = forumData.Categories.LastOrDefault()?.Id + 1 ?? 1; category = new Category(categoryId, categoryName); forumData.Categories.Add(category); forumData.SaveChanges(); } return(category); }
public static Category EnsureCategory(PostViewModel postViewModel, ForumData forumData) { var category = forumData.Categories.SingleOrDefault(c => c.Name == postViewModel.Category); if (category == null) { var id = forumData.Categories.LastOrDefault()?.Id + 1 ?? 1; category = new Category(id, postViewModel.Category, new List <int>()); forumData.Categories.Add(category); forumData.SaveChanges(); } return(category); }
private static Category EnsureCategory(PostViewModel postViewModel, ForumData forumData) { var categoryName = postViewModel.Category; Category category = forumData.Categories.FirstOrDefault(x => x.Name == categoryName); if (category == null) { var categories = forumData.Categories; int categoryId = categories.Any() ? categories.Last().Id + 1 : 1; category = new Category(categoryId, categoryName, new List <int>()); forumData.Categories.Add(category); forumData.SaveChanges(); /// } return(category); }
public bool TrySignUpUser(string username, string password) { bool userAlreadyExists = this.forumData.Users.Any(u => u.Username == username); if (userAlreadyExists) { return(false); } int userId = this.forumData.Users.LastOrDefault()?.Id + 1 ?? 1; User user = new User(userId, username, password); this.forumData.Users.Add(user); forumData.SaveChanges(); this.TryLogInUser(username, password); return(true); }
public static AddReplyStatus TrySaveReply(ReplyViewModel replyView) { if (!replyView.Content.Any()) { return(AddReplyStatus.ContentError); } ForumData forumData = new ForumData(); int replyId = forumData.Replies.LastOrDefault()?.Id + 1 ?? 1; string content = String.Join("", replyView.Content); User author = forumData.Users.Find(u => u.Username == replyView.Author); string postTitle = replyView.PostTitle; Post post = forumData.Posts.Find(p => p.Title == postTitle); Reply reply = new Reply(replyId, content, author.Id, post.Id); forumData.Replies.Add(reply); post.ReplyIds.Add(replyId); forumData.SaveChanges(); replyView.ReplyId = replyId; return(AddReplyStatus.Success); }
public static bool TrySaveReply(ReplyViewModel rvm, int postId) { if (!rvm.Content.Any()) { return(false); } ForumData forumData = new ForumData(); User author = UserService.GetUser(rvm.Author); int authorId = author.Id; Post post = forumData.Posts.Single(p => p.Id == postId); int replyId = forumData.Replies.LastOrDefault()?.Id + 1 ?? 1; string content = string.Join("", rvm.Content); Reply reply = new Reply(replyId, content, authorId, postId); forumData.Replies.Add(reply); post.Replies.Add(replyId); forumData.SaveChanges(); return(true); }
public static bool TrySaveReply(ReplyViewModel replyView, int postId) { if (!replyView.Content.Any()) { return(false); } ForumData forumData = new ForumData(); var replies = forumData.Replies; int replyId = replies.Any() ? replies.Last().Id + 1 : 1; Post post = forumData.Posts.First(p => p.Id == postId); User author = UserService.GetUser(replyView.Author); int authorId = author.Id; string content = String.Join("", replyView.Content); Reply reply = new Reply(replyId, content, authorId, postId); forumData.Replies.Add(reply); post.ReplyIds.Add(replyId); forumData.SaveChanges(); return(true); }
public int AddPost(int authorId, string postTitle, string postCategory, string postContent) { User author = this.forumdata.Users.Find(u => u.Id == authorId); if (author == null) { throw new ArgumentException($"User with id {authorId} not found!"); } int postId = this.forumdata.Posts.LastOrDefault()?.Id + 1 ?? 1; Category category = this.EnsureCategory(postCategory); Post post = new Post(postId, postTitle, postContent, category.Id, authorId, new List <int>()); this.forumdata.Posts.Add(post); author.Posts.Add(postId); category.Posts.Add(postId); forumdata.SaveChanges(); return(postId); }