public async Task <ThreadResponse> AddAsync(Thread thread) { thread.Id = Guid.NewGuid().ToString(); try { await _threadRepository.AddAsync(thread); await _unitOfWork.CompleteAsync(); return(new ThreadResponse(thread)); } catch (Exception ex) { return(new ThreadResponse($"An error occurred when saving the thread: {ex.Message}")); } }
public async Task <ActionResult> Create(ThreadCreateDto model, string onForum) { // This code is a big mess // Now I understand why is generating your own primary key with GUID better than letting database autogenerate it. if (!ModelState.IsValid) { return(View(model)); } var forum = await forumRepository.GetByNameAsync(onForum); var author = await accountRepository.GetByUsernameAsync(User.Identity.Name); var newThread = new Thread { Name = model.Name, AuthorId = author.Id, ForumId = forum.Id }; await threadRepository.AddAsync(newThread); newThread = await threadRepository.GetByNameAsync(model.Name); var newPost = new Post { Content = model.Post.Content, AuthorId = author.Id, ThreadId = newThread.Id }; await postRepository.AddAsync(newPost); return(RedirectToAction(onForum, "Forum", new { threadName = Server.UrlEncode(model.Name) })); }