diff --git a/models/problem.js b/models/problem.js index ccd881d..1b200d2 100644 --- a/models/problem.js +++ b/models/problem.js @@ -638,6 +638,36 @@ class Problem extends Model { await this.save(); } + async delete() { + let fs = Promise.promisifyAll(require('fs-extra')); + let oldTestdataDir = this.getTestdataPath(), oldTestdataZip = oldTestdataDir + '.zip'; + await fs.removeAsync(oldTestdataDir); + await fs.removeAsync(oldTestdataZip); + + let JudgeState = syzoj.model('judge_state'); + let submissions = await JudgeState.query(null, { problem_id: this.id }), submitCnt = {}, acUsers = new Set(); + for (let sm of submissions) { + if (sm.status === 'Accepted') acUsers.add(sm.user_id); + if (!submitCnt[sm.user_id]) { + submitCnt[sm.user_id] = 1; + } else { + submitCnt[sm.user_id]++; + } + } + + for (let u in submitCnt) { + let user = await User.fromID(u); + user.submit_num -= submitCnt[u]; + if (acUsers.has(parseInt(u))) user.ac_num--; + await user.save(); + } + + await db.query('DELETE FROM `problem` WHERE `id` = ' + this.id); + await db.query('DELETE FROM `judge_state` WHERE `problem_id` = ' + this.id); + await db.query('DELETE FROM `problem_tag_map` WHERE `problem_id` = ' + this.id); + await db.query('DELETE FROM `article` WHERE `problem_id` = ' + this.id); + } + getModel() { return model; } } diff --git a/modules/problem.js b/modules/problem.js index a06c260..e450ce5 100644 --- a/modules/problem.js +++ b/modules/problem.js @@ -652,6 +652,25 @@ app.post('/problem/:id/submit', app.multer.fields([{ name: 'answer', maxCount: 1 } }); +app.post('/problem/:id/delete', async (req, res) => { + try { + let id = parseInt(req.params.id); + let problem = await Problem.fromID(id); + if (!problem) throw new ErrorMessage('无此题目。'); + + if (!problem.isAllowedManageBy(res.locals.user)) throw new ErrorMessage('您没有权限进行此操作。'); + + await problem.delete(); + + res.redirect(syzoj.utils.makeUrl(['problem'])); + } catch (e) { + syzoj.log(e); + res.render('error', { + err: e + }); + } +}); + app.get('/problem/:id/testdata', async (req, res) => { try { let id = parseInt(req.params.id); diff --git a/views/problem.ejs b/views/problem.ejs index 99599d7..ab79774 100644 --- a/views/problem.ejs +++ b/views/problem.ejs @@ -95,7 +95,7 @@ div[class*=ace_br] { 讨论 <% if (discussionCount) { %> -
<%= discussionCount %>
+
<%= discussionCount %>
<% } %>
测试数据 @@ -116,6 +116,28 @@ div[class*=ace_br] { <% } else { %> 公开 <% } %> + +
删除
<% } %> <% } %>