DBLabs/abitur_list_client/mongo/controllers/abitur.controller.js

128 lines
4.8 KiB
JavaScript

const mymongodb = require('../../mongoIndex')
const crypto = require('crypto');
const AbiturControllerInterface = require('../../controllers/interfaces/abitur.controller.interface');
const { ObjectId } = require('mongodb');
const e = require('express');
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])
const {first_name, last_name, middle_name, email, password} = req.body
var hash = crypto.createHash('md5').update(password).digest('hex')
const collection = mymongodb.collection('abiturs')
const newAbitur = collection.insertOne({
first_name: first_name,
last_name: last_name,
middle_name: middle_name,
email: email,
password: hash
})
res.json({id: 1})
}
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])
const {id, first_name, last_name, middle_name, email, password, exam_results, requests} = req.body
var hash = crypto.createHash('md5').update(password).digest('hex')
const collection = mymongodb.collection('abiturs')
const newAbitur = await collection.insertOne({
_id: new ObjectId(id),
first_name: first_name,
last_name: last_name,
middle_name: middle_name,
email: email,
password: hash,
exam_results: exam_results,
requests: requests
})
res.json({id: 1})
}
async getAbiturs(req, res) {
// const abiturs = await db.query('SELECT * FROM abitur')
// res.json(abiturs.rows)
const collection = mymongodb.collection('abiturs')
const abiturs = await collection.find().toArray()
res.json(abiturs)
}
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])
const id = req.params.id
const collection = mymongodb.collection('abiturs')
const abiturs = await collection.findOne({_id: new ObjectId(id)})
res.json(abiturs)
}
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])
const collection = mymongodb.collection('abiturs')
const {email, password} = req.body
var hash = crypto.createHash('md5').update(password).digest('hex')
const abitur = await collection.findOne({email: email, password: hash})
res.json(abitur)
}
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])
const {id, first_name, last_name, middle_name} = req.body
const collection = mymongodb.collection('abiturs')
await collection.findOneAndUpdate({_id: id}, {$set: {
first_name: first_name,
last_name: last_name,
middle_name: middle_name,
}})
res.json({id: 1})
}
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])
const id = req.params.id
const collection = mymongodb.collection('abiturs')
await collection.findOneAndDelete({_id: id})
res.json({id: 1})
}
async deleteAll(req, res) {
// const abiturs = await db.query('DELETE FROM abitur')
// res.json(abiturs.rows)
const collection = mymongodb.collection('abiturs')
await collection.deleteMany({})
res.json({id: 1})
}
}
module.exports = new AbiturController()