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]) 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]) } } module.exports = new ExamResultController()