DBLabs/abitur_list_client/controllers/abitur.controller.js

56 lines
2.4 KiB
JavaScript
Raw Normal View History

const db = require('../db')
const crypto = require('crypto');
const AbiturControllerInterface = require('./interfaces/abitur.controller.interface')
class AbiturController extends AbiturControllerInterface{
async createAbitur(req, res) {
const {first_name, last_name, middle_name, email, password} = req.body
var hash = crypto.createHash('md5').update(password).digest('hex');
const newAbitur = await db.query('INSERT INTO abitur (first_name, last_name, middle_name, email, password) VALUES ($1, $2, $3, $4, $5) RETURNING *', [first_name, last_name, middle_name, email, hash])
res.json(newAbitur.rows[0])
}
async createAbiturWithId(req, res) {
const {id, first_name, last_name, middle_name, email, password} = req.body
var hash = crypto.createHash('md5').update(password).digest('hex');
const newAbitur = await db.query('INSERT INTO abitur (id, first_name, last_name, middle_name, email, password) VALUES ($1, $2, $3, $4, $5, $6) RETURNING *', [id, first_name, last_name, middle_name, email, hash])
res.json(newAbitur.rows[0])
}
async getAbiturs(req, res) {
const abiturs = await db.query('SELECT * FROM abitur')
res.json(abiturs.rows)
}
async getOneAbitur(req, res) {
const id = req.params.id
const abiturs = await db.query('SELECT * FROM abitur WHERE id=$1', [id])
res.json(abiturs.rows[0])
}
async authAbitur(req, res) {
const {email, password} = req.body
var hash = crypto.createHash('md5').update(password).digest('hex');
const abitur = await db.query('SELECT * FROM abitur WHERE email = $1 AND password = $2', [email, hash])
res.json(abitur.rows[0])
}
async updateAbitur(req, res) {
const {id, first_name, last_name, middle_name} = req.body
const newAbitur = await db.query('UPDATE abitur SET first_name=$1, last_name=$2, middle_name=$3 WHERE id=$4 RETURNING *', [first_name, last_name, middle_name, id])
res.json(newAbitur.rows[0])
}
async deleteAbitur(req, res) {
const id = req.params.id
const abiturs = await db.query('DELETE FROM abitur WHERE id=$1', [id])
res.json(abiturs.rows[0])
}
async deleteAll(req, res) {
const abiturs = await db.query('DELETE FROM abitur')
res.json(abiturs.rows)
}
}
module.exports = new AbiturController()