From 4ed96886fc49de2af4f1212a2e5946bbb080b965 Mon Sep 17 00:00:00 2001 From: t123yh Date: Mon, 28 Aug 2017 19:05:06 +0800 Subject: [PATCH] Add sorting for problems. --- modules/problem.js | 53 +++++++++++++++++++++++++++++----------------- views/problems.ejs | 10 ++++----- 2 files changed, 38 insertions(+), 25 deletions(-) diff --git a/modules/problem.js b/modules/problem.js index 8c73f9f..e98e0ab 100644 --- a/modules/problem.js +++ b/modules/problem.js @@ -27,11 +27,22 @@ let Contest = syzoj.model('contest'); let ProblemTag = syzoj.model('problem_tag'); let ProblemTagMap = syzoj.model('problem_tag_map'); let Article = syzoj.model('article'); +const Sequelize = require('sequelize'); let Judger = syzoj.lib('judger'); app.get('/problems', async (req, res) => { try { + const sort = req.query.sort || syzoj.config.sorting.problem.field; + const order = req.query.order || syzoj.config.sorting.problem.order; + if (!['id', 'title', 'rating', 'ac_num', 'submit_num', 'ac_rate'].includes(sort) || !['asc', 'desc'].includes(order)) { + throw new ErrorMessage('错误的排序参数。'); + } + + let sortVal = sort; + if (sort === 'ac_rate') { + sortVal = { raw: 'ac_num / submit_num' }; + } let where = {}; if (!res.locals.user || !await res.locals.user.hasPrivilege('manage_problem')) { if (res.locals.user) { @@ -49,7 +60,7 @@ app.get('/problems', async (req, res) => { } let paginate = syzoj.utils.paginate(await Problem.count(where), req.query.page, syzoj.config.page.problem); - let problems = await Problem.query(paginate, where); + let problems = await Problem.query(paginate, where, [[sortVal, order]]); await problems.forEachAsync(async problem => { problem.allowedEdit = await problem.isAllowedEditBy(res.locals.user); @@ -60,7 +71,9 @@ app.get('/problems', async (req, res) => { res.render('problems', { allowedManageTag: res.locals.user && await res.locals.user.hasPrivilege('manage_problem_tag'), problems: problems, - paginate: paginate + paginate: paginate, + curSort: sort, + curOrder: order === 'asc' }); } catch (e) { syzoj.log(e); @@ -533,30 +546,30 @@ app.post('/problem/:id/manage', app.multer.fields([{ name: 'testdata', maxCount: // Set problem public async function setPublic(req, res, is_public) { - try { - let id = parseInt(req.params.id); - let problem = await Problem.fromID(id); - if (!problem) throw new ErrorMessage('无此题目。'); + try { + let id = parseInt(req.params.id); + let problem = await Problem.fromID(id); + if (!problem) throw new ErrorMessage('无此题目。'); - let allowedManage = await problem.isAllowedManageBy(res.locals.user); - if (!allowedManage) throw new ErrorMessage('您没有权限进行此操作。'); + let allowedManage = await problem.isAllowedManageBy(res.locals.user); + if (!allowedManage) throw new ErrorMessage('您没有权限进行此操作。'); - problem.is_public = is_public; - problem.publicizer_id = res.locals.user.id; - await problem.save(); + problem.is_public = is_public; + problem.publicizer_id = res.locals.user.id; + await problem.save(); - res.redirect(syzoj.utils.makeUrl(['problem', id])); - } catch (e) { - syzoj.log(e); - res.render('error', { - err: e - }); - } + res.redirect(syzoj.utils.makeUrl(['problem', id])); + } catch (e) { + syzoj.log(e); + res.render('error', { + err: e + }); } +} app.post('/problem/:id/public', async (req, res) => { - await setPublic(req, res, true); - }); + await setPublic(req, res, true); +}); app.post('/problem/:id/dis_public', async (req, res) => { await setPublic(req, res, false); diff --git a/views/problems.ejs b/views/problems.ejs index 51b5fc3..45c3292 100644 --- a/views/problems.ejs +++ b/views/problems.ejs @@ -91,11 +91,11 @@ if (typeof tags !== 'undefined') tagIDs = tags.map(x => x.id); <% if (user) { %> 提交状态 <% } %> - 编号 - 题目名称 - 通过 - 提交 - 通过率 + <%- createSortableTitle('id', '编号', true) %> + <%- createSortableTitle('title', '题目名称', true) %> + <%- createSortableTitle('ac_num', '通过', false) %> + <%- createSortableTitle('submit_num', '提交', false) %> + <%- createSortableTitle('ac_rate', '通过率', false) %>