void CreateDefaultThreads() { var welcomeThread = new ForumThread { Title = "Welcome!", Author = "admin", CreationDate = DateTime.UtcNow }; GetDatabase().GetCollection <ForumThread>(ForumThread).InsertOne(welcomeThread); var welcomePost = new ForumThreadPost { ThreadId = welcomeThread.Id, Author = "admin", CreationDate = DateTime.UtcNow, Text = "Welcome all to our forum!" }; GetDatabase().GetCollection <ForumThreadPost>(ForumThreadPost).InsertOne(welcomePost); var presentationThread = new ForumThread { Title = "Present yourself to our community", Author = "admin", CreationDate = DateTime.UtcNow.AddMinutes(10) }; GetDatabase().GetCollection <ForumThread>(ForumThread).InsertOne(presentationThread); var presentationPost = new ForumThreadPost { ThreadId = presentationThread.Id, Author = "admin", CreationDate = DateTime.UtcNow.AddMinutes(9), Text = "Hi, if you are new to the forum, please introduce yourself here." }; GetDatabase().GetCollection <ForumThreadPost>(ForumThreadPost).InsertOne(presentationPost); }
public IActionResult NewThread([FromForm] ForumThreadPost thread) { //get user info from access token JwtSecurityToken accessToken; var handler = new JwtSecurityTokenHandler(); string id; try { var trimmedToken = Request.Headers["Authorization"].ToString(); trimmedToken = trimmedToken.Substring(7); accessToken = handler.ReadJwtToken(trimmedToken); id = accessToken.Claims.FirstOrDefault(x => x.Type == "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier").Value; } catch { return(StatusCode(400, JsonFormatter.FailResponse("Bad Token"))); } if (!Guid.TryParse(thread.SubForumID, out _)) { return(StatusCode(400, JsonFormatter.FailResponse("Wrong Format"))); } var newThread = (ForumThread)thread; newThread.User = _forumDbContext.Users.FirstOrDefault(x => x.Id == id); newThread.ParentForum = _forumDbContext.SubForums.FirstOrDefault(x => x.SubForumID == newThread.ParentID); if (newThread.User == null) { return(Unauthorized(JsonFormatter.ErrorResponse("User Not Found"))); } if (newThread.ParentForum == null) { return(NotFound(JsonFormatter.ErrorResponse("Forum Not Found"))); } newThread.LastPostTime = DateTime.Now; newThread.PostTime = DateTime.Now; _forumDbContext.Threads.Add(newThread); _forumDbContext.SaveChanges(); newThread.Comments = new List <Post>(); _databaseCache.AddThread(newThread); return(JsonFormatter.SuccessResponse((ForumThreadGet)newThread)); }