-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib_submission_create.js.html
153 lines (128 loc) · 6.48 KB
/
lib_submission_create.js.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Source: lib/submission_create.js</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<div id="main">
<h1 class="page-title">Source: lib/submission_create.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>/**
* Submission Create
* @module
* @author zccz14 <[email protected]>
* @requires co
* @requires superagent
* @requires models/index
* @requires lib/on_error
*/
const co = require('co');
const request = require('superagent');
const {User, ProblemList, Submission} = require('../models');
const OnError = require('./on_error');
/**
* @name ProblemCreate
* @desc 创建题目的中间件
* @function
* @param {HTTPRequest} req - HTTPRequest
* @param {HTTPRequest} res - HTTPResponse
* @returns {void}
*
* @api {post} /submission 创建提交
* @apiVersion 0.1.0
* @apiName SubmissionCreate
* @apiGroup Submission
* @apiPermission user
*
* @apiParam {ObjectId} problemId 题目ID
* @apiParam {ObjectId} problemListId 题单ID
* @apiParam {ObjectId} judgerId 裁判ID
* @apiParam {String} type 提交类型
* @apiParam {Buffer} body 提交正文
*
* @apiSuccess {Number} code 0
* @apiSuccess {Submission} submission 新建的提交
* @apiSuccess {ProblemList} problemList 更新后的题单
* @apiDescription
* 用户登录后查看题单中的题目并提交。
* 如果题单不存在,返回 11
* 如果题单存在但用户看不到,返回 11
* 如果用户无法提交,返回 7
* 如果题单中没有要交的问题,返回 11
* 一切正常返回 0
*/
function SubmissionCreate(req, res) {
co(function* () {
let user = req.session.user;
// combine
let submitterId = user._id;
let {judgerId, problemId, problemListId, type, body} = req.body;
// find the problem list
let problemList = yield ProblemList.findById(problemListId).exec();
// if the problem is not visible to the submitter
if (problemList.ownerId.toString() !== submitterId && problemList.visibility !== 'public') {
// the problem list does not belongs to the submitter
// and is not public
res.json({ code: 11, msg: 'problem list not available', problemListId });
return;
}
// if the problem list does not allow submitting
if (problemList.allowSubmitting !== true) {
res.json({ code: 7, msg: 'not allow submitting', problemListId });
return;
}
// If the Problem List does not contain the problem
if (problemList.problems.every((v => v.toString() !== problemId))) {
res.json({ code: 11, msg: 'problem not available', problemListId, problemId });
return;
}
let judger = yield User.findById(judgerId).exec();
if (judger === null) {
res.json({ code: 11, msg: 'judger not found', judgerId });
}
let submission = new Submission({
submitterId: user._id,
judgerId,
problemId,
problemListId,
type,
body
});
submission = yield submission.save();
problemList = yield ProblemList.findByIdAndUpdate(problemList._id, {
$addToSet: {
submissions: submission._id
}
}, { new: true });
if (judger.judgeProxy) {
yield request.post(judger.judgeProxy).send(submission);
} else {
yield User.findByIdAndUpdate(judgerId, { $addToSet: { judgeList: submission._id } }).exec();
}
res.json({ code: 0, submission, problemList });
}).catch(OnError(req, res));
}
module.exports = SubmissionCreate;</code></pre>
</article>
</section>
</div>
<nav>
<h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-config.html">config</a></li><li><a href="module-lib_on_error.html">lib/on_error</a></li><li><a href="module-lib_problem_create.html">lib/problem_create</a></li><li><a href="module-lib_problem_delete.html">lib/problem_delete</a></li><li><a href="module-lib_problem_detail.html">lib/problem_detail</a></li><li><a href="module-lib_problem_list_create.html">lib/problem_list_create</a></li><li><a href="module-lib_problem_list_detail.html">lib/problem_list_detail</a></li><li><a href="module-lib_problem_list_problem_create.html">lib/problem_list_problem_create</a></li><li><a href="module-lib_problem_list_retrieve.html">lib/problem_list_retrieve</a></li><li><a href="module-lib_problem_retrieve.html">lib/problem_retrieve</a></li><li><a href="module-lib_problem_update.html">lib/problem_update</a></li><li><a href="module-lib_submission_create.html">lib/submission_create</a></li><li><a href="module-lib_submission_detail.html">lib/submission_detail</a></li><li><a href="module-lib_submission_retrieve.html">lib/submission_retrieve</a></li><li><a href="module-lib_submission_sentence_update.html">lib/submission_sentence_update</a></li><li><a href="module-lib_user_create.html">lib/user_create</a></li><li><a href="module-lib_user_detail.html">lib/user_detail</a></li><li><a href="module-lib_user_login.html">lib/user_login</a></li><li><a href="module-lib_user_logout.html">lib/user_logout</a></li><li><a href="module-lib_user_profile_detail.html">lib/user_profile_detail</a></li><li><a href="module-lib_user_retrieve.html">lib/user_retrieve</a></li><li><a href="module-models_index.html">models/index</a></li><li><a href="module-routes_group.html">routes/group</a></li><li><a href="module-routes_index.html">routes/index</a></li><li><a href="module-routes_problem.html">routes/problem</a></li><li><a href="module-routes_problem_list.html">routes/problem_list</a></li><li><a href="module-routes_submission.html">routes/submission</a></li><li><a href="module-routes_user.html">routes/user</a></li><li><a href="module-server.html">server</a></li></ul><h3>Classes</h3><ul><li><a href="Problem.html">Problem</a></li><li><a href="ProblemList.html">ProblemList</a></li><li><a href="Submission.html">Submission</a></li><li><a href="User.html">User</a></li></ul>
</nav>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.3</a> on Thu Mar 23 2017 05:16:25 GMT+0000 (UTC)
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>