Browse Source

Add problem discussion list.

master
t123yh 7 years ago
parent
commit
9917b293ea
  1. 1
      models/article.js
  2. 31
      modules/discussion.js
  3. 6
      views/article.ejs
  4. 26
      views/discussion.ejs
  5. 2
      views/header.ejs
  6. 2
      views/problem.ejs

1
models/article.js

@ -23,6 +23,7 @@ let Sequelize = require('sequelize');
let db = syzoj.db; let db = syzoj.db;
let User = syzoj.model('user'); let User = syzoj.model('user');
const Problem = syzoj.model('problem');
let model = db.define('article', { let model = db.define('article', {
id: { type: Sequelize.INTEGER, primaryKey: true, autoIncrement: true }, id: { type: Sequelize.INTEGER, primaryKey: true, autoIncrement: true },

31
modules/discussion.js

@ -24,18 +24,34 @@ let Article = syzoj.model('article');
let ArticleComment = syzoj.model('article-comment'); let ArticleComment = syzoj.model('article-comment');
let User = syzoj.model('user'); let User = syzoj.model('user');
app.get('/discussion', async (req, res) => { app.get('/discussion/:type?', async (req, res) => {
try { try {
let where = { problem_id: null }; if (!['global', 'problems'].includes(req.params.type)) {
res.redirect(syzoj.utils.makeUrl(['discussion', 'global']));
}
const in_problems = req.params.type === 'problems';
let where;
if (in_problems) {
where = { problem_id: { $not: null } };
} else {
where = { problem_id: { $eq: null } };
}
let paginate = syzoj.utils.paginate(await Article.count(where), req.query.page, syzoj.config.page.discussion); 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']]); let articles = await Article.query(paginate, where, [['public_time', 'desc']]);
for (let article of articles) await article.loadRelationships(); for (let article of articles) {
await article.loadRelationships();
if (in_problems) {
article.problem = await Problem.fromID(article.problem_id);
}
}
res.render('discussion', { res.render('discussion', {
articles: articles, articles: articles,
paginate: paginate, paginate: paginate,
problem: null problem: null,
in_problems: in_problems
}); });
} catch (e) { } catch (e) {
syzoj.log(e); syzoj.log(e);
@ -45,7 +61,7 @@ app.get('/discussion', async (req, res) => {
} }
}); });
app.get('/problem/:pid/discussion', async (req, res) => { app.get('/discussion/problem/:pid', async (req, res) => {
try { try {
let pid = parseInt(req.params.pid); let pid = parseInt(req.params.pid);
let problem = await Problem.fromID(pid); let problem = await Problem.fromID(pid);
@ -63,7 +79,8 @@ app.get('/problem/:pid/discussion', async (req, res) => {
res.render('discussion', { res.render('discussion', {
articles: articles, articles: articles,
paginate: paginate, paginate: paginate,
problem: problem problem: problem,
in_problems: false
}); });
} catch (e) { } catch (e) {
syzoj.log(e); syzoj.log(e);
@ -201,7 +218,7 @@ app.post('/article/:id/delete', async (req, res) => {
await article.destroy(); await article.destroy();
res.redirect(syzoj.utils.makeUrl(['discussion'])); res.redirect(syzoj.utils.makeUrl(['discussion', 'global']));
} catch (e) { } catch (e) {
syzoj.log(e); syzoj.log(e);
res.render('error', { res.render('error', {

6
views/article.ejs

@ -10,11 +10,11 @@
<div class="section">讨论</div> <div class="section">讨论</div>
<i class="right angle icon divider"></i> <i class="right angle icon divider"></i>
<% if (problem) { %> <% if (problem) { %>
<div class="section">题目</div> <div class="section"><a href="<%= syzoj.utils.makeUrl(['discussion', 'problems']) %>">题目</a></div>
<i class="right angle icon divider"></i> <i class="right angle icon divider"></i>
<a href="<%= syzoj.utils.makeUrl(['problem', problem.id, 'discussion']) %>" class="active section"><%= problem.title %></a> <a href="<%= syzoj.utils.makeUrl(['discussion', 'problem', problem.id]) %>" class="active section"><%= problem.title %></a>
<% } else { %> <% } else { %>
<a href="<%= syzoj.utils.makeUrl(['discussion']) %>" class="section">全局板块</a> <a href="<%= syzoj.utils.makeUrl(['discussion', 'global']) %>" class="section">全局板块</a>
<% } %> <% } %>
</div> </div>
<h1><%= article.title %></h1> <h1><%= article.title %></h1>

26
views/discussion.ejs

@ -8,33 +8,50 @@
<div class="section">讨论</div> <div class="section">讨论</div>
<i class="right angle icon divider"></i> <i class="right angle icon divider"></i>
<% if (problem) { %> <% if (problem) { %>
<div class="section">题目</div> <div class="section"><a href="<%= syzoj.utils.makeUrl(['discussion', 'problems']) %>">题目</a></div>
<i class="right angle icon divider"></i> <i class="right angle icon divider"></i>
<div class="active section"><%= problem.title %></div> <div class="active section"><%= problem.title %></div>
<% } else if (in_problems) { %>
<div class="section">题目</div>
<% } else { %> <% } else { %>
<div class="section">全局板块</div> <div class="section">全局板块</div>
<% } %> <% } %>
</div> </div>
</div> </div>
<div class="six wide right aligned column"> <div class="six wide right aligned column">
<% if(in_problems) { %>
<a style="margin-left: 10px; " href="<%= syzoj.utils.makeUrl(['discussion', 'global']) %>" class="ui labeled icon mini blue button">
<i class="world icon"></i>
全局板块
</a>
<% } else { %>
<% if (problem) { %> <% if (problem) { %>
<a style="margin-left: 10px; " href="<%= syzoj.utils.makeUrl(['problem', problem.id]) %>" class="ui labeled icon mini blue button"> <a style="margin-left: 10px; " href="<%= syzoj.utils.makeUrl(['problem', problem.id]) %>" class="ui labeled icon mini blue button">
<i class="arrow left icon"></i> <i class="arrow left icon"></i>
返回题目 返回题目
</a> </a>
<% } else { %>
<a style="margin-left: 10px; " href="<%= syzoj.utils.makeUrl(['discussion', 'problems']) %>" class="ui labeled icon mini blue button">
<i class="file outline icon"></i>
题目板块
</a>
<% } %> <% } %>
<a style="margin-left: 10px; " href="<%= syzoj.utils.makeUrl(['article', 0, 'edit'], problem ? { problem_id: problem.id } : null) %>" class="ui labeled icon mini button"> <a style="margin-left: 10px; " href="<%= syzoj.utils.makeUrl(['article', 0, 'edit'], problem ? { problem_id: problem.id } : null) %>" class="ui labeled icon mini button">
<i class="write icon"></i> <i class="write icon"></i>
发帖 发帖
</a> </a>
<% } %>
</div> </div>
</div> </div>
</div> </div>
<table class="ui very basic center aligned table"> <table class="ui very basic center aligned table">
<thead> <thead>
<tr> <tr>
<th class="left aligned" style="width: 65%; ">标题</th> <th class="left aligned" style="width: 50%; ">标题</th>
<th style="width: 15%; ">作者</th> <% if (in_problems) { %>
<th style="width: 20%; ">所属题目</th>
<% } %>
<th style="width: 10%; ">作者</th>
<th style="width: 20%; ">发表时间</th> <th style="width: 20%; ">发表时间</th>
</tr> </tr>
</thead> </thead>
@ -42,6 +59,9 @@
<% for (let article of articles) { %> <% for (let article of articles) { %>
<tr> <tr>
<td class="left aligned"><a href="<%= syzoj.utils.makeUrl(['article', article.id]) %>"><%= article.title %></a></td> <td class="left aligned"><a href="<%= syzoj.utils.makeUrl(['article', article.id]) %>"><%= article.title %></a></td>
<% if (in_problems) { %>
<td><a href="<%= syzoj.utils.makeUrl(['discussion', 'problem', article.problem_id]) %>"><%= article.problem.title %></a></td>
<% } %>
<td><a href="<%= syzoj.utils.makeUrl(['user', article.user_id]) %>"><%= article.user.username %></a><% if (article.user.nameplate) { %><%- article.user.nameplate %><% } %></td> <td><a href="<%= syzoj.utils.makeUrl(['user', article.user_id]) %>"><%= article.user.username %></a><% if (article.user.nameplate) { %><%- article.user.nameplate %><% } %></td>
<td><%= syzoj.utils.formatDate(article.public_time) %></td> <td><%= syzoj.utils.formatDate(article.public_time) %></td>
</tr> </tr>

2
views/header.ejs

@ -22,7 +22,7 @@
<a class="item<% if (active.startsWith('contest')) { %> active<% } %>" href="/contests"><i class="calendar icon"></i> 比赛</a> <a class="item<% if (active.startsWith('contest')) { %> active<% } %>" href="/contests"><i class="calendar icon"></i> 比赛</a>
<a class="item<% if (active.startsWith('submission')) { %> active<% } %>" href="/submissions"><i class="tasks icon"></i> 评测</a> <a class="item<% if (active.startsWith('submission')) { %> active<% } %>" href="/submissions"><i class="tasks icon"></i> 评测</a>
<a class="item<% if (active.startsWith('ranklist')) { %> active<% } %>" href="/ranklist"><i class="signal icon"></i> 排名</a> <a class="item<% if (active.startsWith('ranklist')) { %> active<% } %>" href="/ranklist"><i class="signal icon"></i> 排名</a>
<a class="item<% if (active.startsWith('discussion') || active.startsWith('article')) { %> active<% } %>" href="/discussion"><i class="comments icon"></i> 讨论</a> <a class="item<% if (active.startsWith('discussion') || active.startsWith('article')) { %> active<% } %>" href="/discussion/global"><i class="comments icon"></i> 讨论</a>
<a class="item<% if (active.startsWith('help')) { %> active<% } %>" href="/help"><i class="help circle icon"></i> 帮助</a> <a class="item<% if (active.startsWith('help')) { %> active<% } %>" href="/help"><i class="help circle icon"></i> 帮助</a>
<% if (typeof contest !== 'undefined' && contest && contest.id) { %> <% if (typeof contest !== 'undefined' && contest && contest.id) { %>
<a id="back_to_contest" class="item" href="<%= syzoj.utils.makeUrl(['contest', contest.id]) %>"><i class="arrow left icon"></i> 返回比赛</a> <a id="back_to_contest" class="item" href="<%= syzoj.utils.makeUrl(['contest', contest.id]) %>"><i class="arrow left icon"></i> 返回比赛</a>

2
views/problem.ejs

@ -96,7 +96,7 @@ div[class*=ace_br] {
<% } %> <% } %>
<a class="small ui positive button" href="<%= syzoj.utils.makeUrl(['submissions'], { problem_id: problem.id }) %>">提交记录</a> <a class="small ui positive button" href="<%= syzoj.utils.makeUrl(['submissions'], { problem_id: problem.id }) %>">提交记录</a>
<a class="small ui orange button" href="<%= syzoj.utils.makeUrl(['problem', problem.id, 'statistics', problem.type === 'submit-answer' ? 'min' : 'fastest']) %>">统计</a> <a class="small ui orange button" href="<%= syzoj.utils.makeUrl(['problem', problem.id, 'statistics', problem.type === 'submit-answer' ? 'min' : 'fastest']) %>">统计</a>
<a class="small ui brown button" href="<%= syzoj.utils.makeUrl(['problem', problem.id, 'discussion']) %>" style="position: relative; "> <a class="small ui brown button" href="<%= syzoj.utils.makeUrl(['discussion', 'problem', problem.id]) %>" style="position: relative; ">
讨论 讨论
<% if (discussionCount) { %> <% if (discussionCount) { %>
<div class="floating ui red tiny circular label"><%= discussionCount %></div> <div class="floating ui red tiny circular label"><%= discussionCount %></div>

Loading…
Cancel
Save