DBLabs/abitur_list_client/controllers/examresult.controller.js

47 lines
2.2 KiB
JavaScript

const db = require('../db')
const ExamResultControllerInterface = require('./interfaces/examresult.controller.interface')
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])
}
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])
}
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])
}
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)
}
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])
}
}
module.exports = new ExamResultController()