DBLabs/abitur_list_client/controllers/request.controller.js

47 lines
2.2 KiB
JavaScript
Raw Permalink Normal View History

const db = require('../db')
const RequestControllerInterface = require('./interfaces/request.controller.interface')
class RequestController extends RequestControllerInterface {
async createRequest(req, res) {
const {abitur_id, education_form_id, specialization_id} = req.body
const newRequest = await db.query('INSERT INTO request (abitur_id, education_form_id, specialization_id, date) VALUES ($1, $2, $3, $4) RETURNING *', [abitur_id, education_form_id, specialization_id, new Date()])
res.json(newRequest.rows[0])
}
async createRequestWithId(req, res) {
const {id, abitur_id, education_form_id, specialization_id} = req.body
const newRequest = await db.query('INSERT INTO request (id, abitur_id, education_form_id, specialization_id, date) VALUES ($1, $2, $3, $4, $5) RETURNING *', [id, abitur_id, education_form_id, specialization_id, new Date()])
res.json(newRequest.rows[0])
}
async getRequests(req, res) {
const requests = await db.query('SELECT * FROM request')
res.json(requests.rows)
}
async getOneRequest(req, res) {
const id = req.params.id
const requests = await db.query('SELECT * FROM request WHERE id=$1', [id])
res.json(requests.rows[0])
}
async updateRequest(req, res) {
const {id, education_form_id, specialization_id, date} = req.body
const newRequest = await db.query('UPDATE request SET education_form_id=$1, specialization_id=$2, date=$3 WHERE id=$4 RETURNING *', [education_form_id, specialization_id, date, id])
res.json(newRequest.rows[0])
}
async deleteRequest(req, res) {
const id = req.params.id
const requests = await db.query('DELETE FROM request WHERE id=$1', [id])
res.json(requests.rows[0])
}
async getAbiturRequests(req, res) {
const id = req.params.id
const requests = await db.query('SELECT * FROM request WHERE abitur_id=$1', [id])
res.json(requests.rows)
}
async getCountBySpec(req, res) {
const id = req.params.id
const requests = await db.query('SELECT COUNT(*) FROM request WHERE specialization_id=$1', [id])
res.json(requests.rows[0])
}
}
module.exports = new RequestController()