2023-03-31 18:56:39 +04:00
const db = require ( '../db' )
2023-04-29 14:20:44 +04:00
const crypto = require ( 'crypto' ) ;
2023-03-31 18:56:39 +04:00
2023-03-31 23:19:54 +04:00
const AbiturControllerInterface = require ( './interfaces/abitur.controller.interface' )
2023-03-31 18:56:39 +04:00
class AbiturController extends AbiturControllerInterface {
2023-04-29 14:20:44 +04:00
2023-03-31 18:56:39 +04:00
async createAbitur ( req , res ) {
2023-04-29 14:20:44 +04:00
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 ] )
2023-03-31 18:56:39 +04:00
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 ] )
}
2023-04-29 14:20:44 +04:00
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 ] )
}
2023-03-31 18:56:39 +04:00
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 ] )
}
2023-04-29 14:20:44 +04:00
async deleteAll ( req , res ) {
const abiturs = await db . query ( 'DELETE FROM abitur' )
res . json ( abiturs . rows )
}
2023-03-31 18:56:39 +04:00
}
module . exports = new AbiturController ( )