11const router = require ( 'express' ) . Router ( ) ;
22const { submit, listSubmissions, getSubmission } = require ( '../controllers/submissionController' ) ;
33const { authenticate, adminOnly } = require ( '../middleware/auth' ) ;
4- const { queryAll } = require ( '../config/db' ) ;
4+ const { queryAll, queryOne, run } = require ( '../config/db' ) ;
5+ const { judgeCode, judgeChoice, judgeFillBlank } = require ( '../services/judgeService' ) ;
56
67router . post ( '/' , authenticate , submit ) ;
78router . get ( '/' , authenticate , listSubmissions ) ;
@@ -20,6 +21,38 @@ router.get('/admin/all', authenticate, adminOnly, (req, res) => {
2021 ) ;
2122 res . json ( { submissions, total, page : Number ( page ) , totalPages : Math . ceil ( total / Number ( limit ) ) } ) ;
2223} ) ;
24+ router . post ( '/admin/rejudge/:id' , authenticate , adminOnly , ( req , res ) => {
25+ const submission = queryOne ( 'SELECT * FROM submissions WHERE id = ?' , [ req . params . id ] ) ;
26+ if ( ! submission ) return res . status ( 404 ) . json ( { error : '提交记录不存在' } ) ;
27+ const problem = queryOne ( 'SELECT * FROM problems WHERE id = ?' , [ submission . problem_id ] ) ;
28+ if ( ! problem ) return res . status ( 404 ) . json ( { error : '题目不存在' } ) ;
29+
30+ let result ;
31+ try {
32+ let testCases ;
33+ try { testCases = JSON . parse ( problem . test_cases ) ; } catch { testCases = [ ] ; }
34+
35+ if ( submission . type === 'programming' || problem . type === 'programming' ) {
36+ result = judgeCode ( submission . code , submission . language || 'javascript' , testCases ) ;
37+ } else if ( submission . type === 'choice' || problem . type === 'choice' ) {
38+ let options ; try { options = JSON . parse ( problem . options ) ; } catch { options = [ ] ; }
39+ result = judgeChoice ( submission . answer , problem . blanks_answer , options ) ;
40+ } else {
41+ let answers ; try { answers = JSON . parse ( problem . blanks_answer ) ; } catch { answers = [ ] ; }
42+ result = judgeFillBlank ( submission . answer , answers , problem . solution ) ;
43+ }
44+
45+ run ( 'UPDATE submissions SET status = ?, score = ?, details = ? WHERE id = ?' ,
46+ [ result . status , result . score , JSON . stringify ( result . details ) , submission . id ] ) ;
47+ if ( result . status === 'accepted' ) {
48+ run ( 'UPDATE problems SET accepted_count = accepted_count + 1 WHERE id = ?' , [ submission . problem_id ] ) ;
49+ }
50+
51+ res . json ( { message : '重新判题完成' , result } ) ;
52+ } catch ( e ) {
53+ res . status ( 500 ) . json ( { error : e . message } ) ;
54+ }
55+ } ) ;
2356router . get ( '/:id' , authenticate , getSubmission ) ;
2457
2558module . exports = router ;
0 commit comments