From 1e17e77ab6bb0aa1d5d367285cb7258304f38406 Mon Sep 17 00:00:00 2001 From: Menci Date: Mon, 26 Jun 2017 20:58:39 +0800 Subject: [PATCH] Add contest is_public --- models/contest.js | 7 +++++-- modules/contest.js | 9 +++++++-- modules/index.js | 5 ++++- views/contest_edit.ejs | 7 +++++++ 4 files changed, 23 insertions(+), 5 deletions(-) diff --git a/models/contest.js b/models/contest.js index 8bcfda3..300b00d 100644 --- a/models/contest.js +++ b/models/contest.js @@ -53,7 +53,9 @@ let model = db.define('contest', { model: 'contest_ranklist', key: 'id' } - } + }, + + is_public: { type: Sequelize.BOOLEAN } }, { timestamps: false, tableName: 'contest', @@ -79,7 +81,8 @@ class Contest extends Model { start_time: 0, end_time: 0, holder: 0, - ranklist_id: 0 + ranklist_id: 0, + is_public: false }, val))); } diff --git a/modules/contest.js b/modules/contest.js index e7240c6..ca1beed 100644 --- a/modules/contest.js +++ b/modules/contest.js @@ -28,8 +28,12 @@ let User = syzoj.model('user'); app.get('/contests', async (req, res) => { try { - let paginate = syzoj.utils.paginate(await Contest.count(), req.query.page, syzoj.config.page.contest); - let contests = await Contest.query(paginate, null, [['start_time', 'desc']]); + let where; + if (res.locals.user && await res.locals.user.is_admin) where = {} + else where = { is_public: true }; + + let paginate = syzoj.utils.paginate(await Contest.count(where), req.query.page, syzoj.config.page.contest); + let contests = await Contest.query(paginate, where, [['start_time', 'desc']]); await contests.forEachAsync(async x => x.subtitle = await syzoj.utils.markdown(x.subtitle)); @@ -97,6 +101,7 @@ app.post('/contest/:id/edit', async (req, res) => { contest.information = req.body.information; contest.start_time = syzoj.utils.parseDate(req.body.start_time); contest.end_time = syzoj.utils.parseDate(req.body.end_time); + contest.is_public = req.body.is_public === 'on'; await contest.save(); diff --git a/modules/index.js b/modules/index.js index a7eb8ab..770b295 100644 --- a/modules/index.js +++ b/modules/index.js @@ -47,7 +47,10 @@ app.get('/', async (req, res) => { fortune = Divine(res.locals.user.username, res.locals.user.sex); } - let contests = await Contest.query([1, 5], null, [['start_time', 'desc']]); + let where; + if (res.locals.user && await res.locals.user.is_admin) where = {} + else where = { is_public: true }; + let contests = await Contest.query([1, 5], where, [['start_time', 'desc']]); let hitokoto; try { diff --git a/views/contest_edit.ejs b/views/contest_edit.ejs index acb91b4..b0118c4 100644 --- a/views/contest_edit.ejs +++ b/views/contest_edit.ejs @@ -51,6 +51,13 @@ +
+ +
+ checked<% } %> name="is_public"> + +
+