Example #1
0
        public async Task <List <ThreadComment> > GetThreadComments(int threadId, int pageIndex, int pageSize)
        {
            var foundComments = new List <ThreadComment>();
            var conn          = Context.Database.GetDbConnection();

            try
            {
                await conn.OpenAsync();

                const string query =
                    @"SELECT fc.id, fc.date_created, fc.content,  concat(users.first_name , ' ' ,users.last_name  ) as user_fullname, users.slug as userslug  
FROM forum_comments fc inner join users
on(fc.user_id = users.id)
where fc.forum_thread_id = @threadId
order by fc.date_created asc";
                using (var command = conn.CreateCommand())
                {
                    command.CommandText = query;
                    command.Parameters.Add(new MySqlParameter("threadId", threadId));
                    var reader = await command.ExecuteReaderAsync();

                    if (reader.HasRows)
                    {
                        while (await reader.ReadAsync())
                        {
                            var row = new ThreadComment
                            {
                                Id           = reader.GetInt32(0),
                                Created      = reader.GetDateTime(1),
                                Content      = reader.GetString(2),
                                UserFullName = reader.GetString(3),
                                UserSlug     = reader.GetString(4),
                            };
                            foundComments.Add(row);
                        }
                    }

                    reader.Dispose();
                }
            }
            finally
            {
                conn.Close();
            }

            return(foundComments);
        }
Example #2
0
 public IActionResult NewComment(int CatId, int ThreadId, ThreadComment comment)
 {
     if (ModelState.IsValid)
     {
         int?          UserId     = HttpContext.Session.GetInt32("UserId");
         ThreadComment newComment = new ThreadComment
         {
             Comment  = comment.Comment,
             UserId   = (int)UserId,
             ThreadId = ThreadId
         };
         dbContext.Add(newComment);
         dbContext.SaveChanges();
         return(RedirectToAction("ShowThread", new { CatId = CatId, ThreadId = ThreadId }));
     }
     return(View("ShowThread"));
 }