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

143 lines
5.6 KiB
JavaScript

const mymongodb = require('../../mongoIndex')
const RequestControllerInterface = require('../../controllers/interfaces/request.controller.interface');
const { ObjectId } = require('mongodb');
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])
const {abitur_id, education_form_id, specialization_id} = req.body
const collection = mymongodb.collection('abiturs')
const education_form = await mymongodb.collection('educationforms').findOne({id: education_form_id})
const specialization = await mymongodb.collection('specializations').findOne({id: specialization_id})
const newreq = {
_id: new ObjectId(),
exam_results: [],
date: Date.now().toString(),
specialization: specialization['title'],
education_form: education_form['title']
}
await collection.updateOne({_id: new ObjectId(abitur_id)},
{
$push: { requests: newreq }
}
)
res.json(newreq)
}
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])
const {id, abitur_id, education_form_id, specialization_id} = req.body
const collection = mymongodb.collection('abiturs')
const education_form = await mymongodb.collection('educationforms').findOne({id: education_form_id})
const specialization = await mymongodb.collection('specializations').findOne({id: specialization_id})
await collection.updateOne({_id: new ObjectId(abitur_id)},
{
$push: { requests: {
_id: new ObjectId(id),
exam_results: [],
date: Date.now().toString(),
specialization: specialization['title'],
education_form: education_form['title']
} }
}
)
}
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])
const id = req.params.id
const collection = mymongodb.collection('abiturs')
const abitur = await collection.findOne({"requests._id": new ObjectId(id)}, {_id: 0, requests: {$elemMatch: {_id: new ObjectId(id)}}})
for(const req of abitur['requests']) {
if (req['_id'] == id){
console.log(req)
res.json(req)
break
}
}
}
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])
const {id, education_form, specialization, exam_results, date, abitur_id} = req.body
const collection = mymongodb.collection('abiturs')
if (exam_results != null) {
var dictionary = {}
for(const newres of exam_results) {
dictionary[newres['title']] += 1
if (dictionary[newres['title']] > 1){
console.log('same result')
return
}
}
}
await collection.updateOne({_id: new ObjectId(abitur_id)},
{$pull: {
requests: {_id: new ObjectId(id)}
}}
)
await collection.updateOne({_id: new ObjectId(abitur_id)},
{
$push: { requests: {
_id: new ObjectId(id),
exam_results: exam_results,
date: date,
specialization: specialization,
education_form: education_form
} }
}
)
}
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])
const {abitur_id, request_id} = req.body
const collection = mymongodb.collection('abiturs')
await collection.updateOne({_id: new ObjectId(abitur_id)},
{$pull: {
requests: {_id: new ObjectId(request_id)}
}}
)
}
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()