2023-05-14 01:50:44 +04:00
const mymongodb = require ( '../../mongoIndex' )
const ExamResultControllerInterface = require ( '../../controllers/interfaces/examresult.controller.interface' ) ;
const { ObjectId } = require ( 'mongodb' ) ;
class ExamResultController extends ExamResultControllerInterface {
async createExamResult ( req , res ) {
// const {title, result, abitur_id} = req.body
// const newExamResult = await db.query('INSERT INTO exam_result (title, result, abitur_id) VALUES ($1, $2, $3) RETURNING *', [title, result, abitur_id])
// res.json(newExamResult.rows[0])
const { title , result , abitur _id } = req . body
const collection = mymongodb . collection ( 'abiturs' )
await collection . updateOne ( { _id : new ObjectId ( abitur _id ) } ,
{
$push : { exam _results : {
_id : new ObjectId ( ) ,
title : title ,
result : result
} }
}
)
}
async createExamResultWithId ( req , res ) {
// const {id, title, result, abitur_id} = req.body
// const newExamResult = await db.query('INSERT INTO exam_result (id, title, result, abitur_id) VALUES ($1, $2, $3, $4) RETURNING *', [id, title, result, abitur_id])
// res.json(newExamResult.rows[0])
const { id , title , result , abitur _id } = req . body
const collection = mymongodb . collection ( 'abiturs' )
await collection . updateOne ( { _id : new ObjectId ( abitur _id ) } ,
{
$push : { exam _results : {
_id : new ObjectId ( id ) ,
title : title ,
result : result
} }
}
)
}
async getExamResults ( req , res ) {
// const results = await db.query('SELECT * FROM exam_result')
// res.json(results.rows)
}
async getOneExamResult ( req , res ) {
// const id = req.params.id
// const results = await db.query('SELECT * FROM exam_result WHERE id=$1', [id])
// res.json(results.rows[0])
}
async updateExamResult ( req , res ) {
// const {id, title, result} = req.body
// const newExamResult = await db.query('UPDATE exam_result SET title=$1, result=$2 WHERE id=$3 RETURNING *', [title, result, id])
// res.json(newExamResult.rows[0])
}
async deleteExamResult ( req , res ) {
// const id = req.params.id
// const results = await db.query('DELETE FROM exam_result WHERE id=$1', [id])
// res.json(results.rows[0])
const { abitur _id , result _id } = req . body
const collection = mymongodb . collection ( 'abiturs' )
await collection . updateOne ( { _id : new ObjectId ( abitur _id ) } ,
{ $pull : {
exam _results : { _id : new ObjectId ( result _id ) }
} }
)
}
async getAbiturExamResults ( req , res ) {
// const id = req.params.id
// const results = await db.query('SELECT * FROM exam_result WHERE abitur_id=$1', [id])
// res.json(results.rows)
const collection = mymongodb . collection ( 'abiturs' )
const id = req . params . id
const abitur = await collection . findOne ( { _id : new ObjectId ( id ) } )
res . json ( abitur [ 'exam_results' ] )
}
async getAverageResultBySpecialization ( req , res ) {
// const id = req.params.id
// const result = await db.query('select avg(result) from exam_result where id in (select exam_result_id from request_exam_result where request_id in (select id from request where specialization_id = $1))', [id])
// res.json(result.rows[0])
2023-05-14 15:26:22 +04:00
const title = req . params . id
const collection = mymongodb . collection ( 'abiturs' )
const answer = await collection . aggregate ( [
{ $match : { "requests.specialization" : title } } ,
{ $unwind : "$requests" } ,
{ $unwind : "$requests.exam_results" } ,
{
$group : {
_id : {
specialization : "$requests.specialization"
} ,
avg _result : { $avg : "$requests.exam_results.result" }
}
}
] ) . toArray ( )
res . json ( answer [ 0 ] )
2023-05-14 01:50:44 +04:00
}
}
module . exports = new ExamResultController ( )