From 766ae45e7e5bf59d70d231fb5c93bd1244862717 Mon Sep 17 00:00:00 2001 From: Menci Date: Sun, 9 Jul 2017 21:20:04 +0800 Subject: [PATCH] Add discussion for problem --- models/article.js | 2 ++ modules/discussion.js | 56 +++++++++++++++++++++++++++++++++++++++--- modules/problem.js | 6 ++++- views/article.ejs | 11 +++++++++ views/article_edit.ejs | 2 +- views/discussion.ejs | 36 +++++++++++++++++++++------ views/problem.ejs | 6 +++++ 7 files changed, 106 insertions(+), 13 deletions(-) diff --git a/models/article.js b/models/article.js index 03b65f5..fb10f86 100644 --- a/models/article.js +++ b/models/article.js @@ -31,6 +31,7 @@ let model = db.define('article', { content: { type: Sequelize.TEXT }, user_id: { type: Sequelize.INTEGER }, + problem_id: { type: Sequelize.INTEGER }, public_time: { type: Sequelize.INTEGER }, update_time: { type: Sequelize.INTEGER }, @@ -61,6 +62,7 @@ class Article extends Model { content: '', user_id: 0, + problem_id: 0, public_time: 0, update_time: 0, diff --git a/modules/discussion.js b/modules/discussion.js index 47a3279..cfcd419 100644 --- a/modules/discussion.js +++ b/modules/discussion.js @@ -19,20 +19,51 @@ 'use strict'; +let Problem = syzoj.model('problem'); let Article = syzoj.model('article'); let ArticleComment = syzoj.model('article-comment'); let User = syzoj.model('user'); app.get('/discussion', async (req, res) => { try { - let paginate = syzoj.utils.paginate(await Article.count(), req.query.page, syzoj.config.page.discussion); - let articles = await Article.query(paginate, null, [['public_time', 'desc']]); + let where = { problem_id: null }; + let paginate = syzoj.utils.paginate(await Article.count(where), req.query.page, syzoj.config.page.discussion); + let articles = await Article.query(paginate, where, [['public_time', 'desc']]); for (let article of articles) await article.loadRelationships(); res.render('discussion', { articles: articles, - paginate: paginate + paginate: paginate, + problem: null + }); + } catch (e) { + syzoj.log(e); + res.render('error', { + err: e + }); + } +}); + +app.get('/problem/:pid/discussion', async (req, res) => { + try { + let pid = parseInt(req.params.pid); + let problem = await Problem.fromID(pid); + if (!problem) throw new ErrorMessage('无此题目。'); + if (!await problem.isAllowedUseBy(res.locals.user)) { + throw new ErrorMessage('您没有权限进行此操作。'); + } + + let where = { problem_id: pid }; + let paginate = syzoj.utils.paginate(await Article.count(where), req.query.page, syzoj.config.page.discussion); + let articles = await Article.query(paginate, where, [['public_time', 'desc']]); + + for (let article of articles) await article.loadRelationships(); + + res.render('discussion', { + articles: articles, + paginate: paginate, + problem: problem }); } catch (e) { syzoj.log(e); @@ -65,10 +96,19 @@ app.get('/article/:id', async (req, res) => { await comment.loadRelationships(); } + let problem = null; + if (article.problem_id) { + problem = await Problem.fromID(article.problem_id); + if (!await problem.isAllowedUseBy(res.locals.user)) { + throw new ErrorMessage('您没有权限进行此操作。'); + } + } + res.render('article', { article: article, comments: comments, - paginate: paginate + paginate: paginate, + problem: problem }); } catch (e) { syzoj.log(e); @@ -116,6 +156,14 @@ app.post('/article/:id/edit', async (req, res) => { article = await Article.create(); article.user_id = res.locals.user.id; article.public_time = article.sort_time = time; + + if (req.query.problem_id) { + let problem = await Problem.fromID(req.query.problem_id); + if (!problem) throw new ErrorMessage('无此题目。'); + article.problem_id = problem.id; + } else { + article.problem_id = null; + } } else { if (!await article.isAllowedEditBy(res.locals.user)) throw new ErrorMessage('您没有权限进行此操作。'); } diff --git a/modules/problem.js b/modules/problem.js index 1a0e564..a06c260 100644 --- a/modules/problem.js +++ b/modules/problem.js @@ -25,6 +25,7 @@ let WaitingJudge = syzoj.model('waiting_judge'); let Contest = syzoj.model('contest'); let ProblemTag = syzoj.model('problem_tag'); let ProblemTagMap = syzoj.model('problem_tag_map'); +let Article = syzoj.model('article'); app.get('/problems', async (req, res) => { try { @@ -204,11 +205,14 @@ app.get('/problem/:id', async (req, res) => { let testcases = await syzoj.utils.parseTestdata(problem.getTestdataPath(), problem.type === 'submit-answer'); + let discussionCount = await Article.count({ problem_id: id }); + res.render('problem', { problem: problem, state: state, lastLanguage: res.locals.user ? await res.locals.user.getLastSubmitLanguage() : null, - testcases: testcases + testcases: testcases, + discussionCount: discussionCount }); } catch (e) { syzoj.log(e); diff --git a/views/article.ejs b/views/article.ejs index f9070e8..c363d59 100644 --- a/views/article.ejs +++ b/views/article.ejs @@ -6,6 +6,17 @@ }
+

<%= article.title %>

<%= article.user.username %><% if (article.user.nameplate) { %><%- article.user.nameplate %><% } %> 于 <%= syzoj.utils.formatDate(article.public_time) %> 发表,<%= syzoj.utils.formatDate(article.update_time) %> 最后更新 diff --git a/views/article_edit.ejs b/views/article_edit.ejs index 9dfddfd..64cf734 100644 --- a/views/article_edit.ejs +++ b/views/article_edit.ejs @@ -6,7 +6,7 @@ }

-
+