diff --git a/modules/problem.js b/modules/problem.js index bbdc621..5ef2978 100644 --- a/modules/problem.js +++ b/modules/problem.js @@ -49,6 +49,40 @@ app.get('/problems', async (req, res) => { } }); +app.get('/problems/search', async (req, res) => { + try { + let id = parseInt(req.query.keyword) || 0; + + let where = { + $or: { + title: { like: `%${req.query.keyword}%` }, + id: id + } + }; + + let order = [syzoj.db.literal('`id` = ' + id + ' DESC')]; + + let paginate = syzoj.utils.paginate(await Problem.count(where), req.query.page, syzoj.config.page.problem); + let problems = await Problem.query(paginate, where, order); + + await problems.forEachAsync(async problem => { + problem.allowedEdit = await problem.isAllowedEditBy(res.locals.user); + problem.judge_state = await problem.getJudgeState(res.locals.user, true); + problem.tags = await problem.getTags(); + }); + + res.render('problems', { + problems: problems, + paginate: paginate + }); + } catch (e) { + syzoj.log(e); + res.render('error', { + err: e + }); + } +}); + app.get('/problems/tag/:tagIDs', async (req, res) => { try { let tagIDs = Array.from(new Set(req.params.tagIDs.split(',').map(x => parseInt(x)))); diff --git a/views/index.ejs b/views/index.ejs index da19f0b..d35e157 100644 --- a/views/index.ejs +++ b/views/index.ejs @@ -123,13 +123,15 @@ <% } %>