CRUD методы для всех сущностей

This commit is contained in:
Данила Мочалов 2023-03-31 23:19:54 +04:00
parent c8f0d209ab
commit 39f81fd472
22 changed files with 336 additions and 3 deletions

View File

@ -1,6 +1,6 @@
const db = require('../db')
const AbiturControllerInterface = require('../controllers/abitur.controller.interface')
const AbiturControllerInterface = require('./interfaces/abitur.controller.interface')
class AbiturController extends AbiturControllerInterface{
async createAbitur(req, res) {

View File

@ -0,0 +1,32 @@
const db = require('../db')
const EducationFormControllerInterface = require('./interfaces/educationform.controller.interface')
class EducationFormController extends EducationFormControllerInterface {
async createEducationForm(req, res) {
const {title} = req.body
const newEducationForm = await db.query('INSERT INTO education_form (title) VALUES ($1) RETURNING *', [title])
res.json(newEducationForm.rows[0])
}
async getEducationForms(req, res) {
const forms = await db.query('SELECT * FROM education_form')
res.json(forms.rows)
}
async getOneEducationForm(req, res) {
const id = req.params.id
const forms = await db.query('SELECT * FROM education_form WHERE id=$1', [id])
res.json(forms.rows[0])
}
async updateEducationForm(req, res) {
const {id, title} = req.body
const newEducationForm = await db.query('UPDATE education_form SET title=$1 WHERE id=$2 RETURNING *', [title, id])
res.json(newEducationForm.rows[0])
}
async deleteEducationForm(req, res) {
const id = req.params.id
const forms = await db.query('DELETE FROM education_form WHERE id=$1', [id])
res.json(forms.rows[0])
}
}
module.exports = new EducationFormController()

View File

@ -0,0 +1,32 @@
const db = require('../db')
const EnrollmentControllerInterface = require('./interfaces/enrollment.controller.interface')
class EnrollmentController extends EnrollmentControllerInterface {
async createEnrollment(req, res) {
const {request_id} = req.body
const newEnrollment = await db.query('INSERT INTO enrollment (request_id, date) VALUES ($1, $2) RETURNING *', [request_id, new Date()])
res.json(newEnrollment.rows[0])
}
async getEnrollments(req, res) {
const enrollments = await db.query('SELECT * FROM enrollment')
res.json(enrollments.rows)
}
async getOneEnrollment(req, res) {
const id = req.params.id
const enrollments = await db.query('SELECT * FROM enrollment WHERE id=$1', [id])
res.json(enrollments.rows[0])
}
async updateEnrollment(req, res) {
const {id, request_id, date} = req.body
const newEnrollment = await db.query('UPDATE enrollment SET request_id=$1, date=$2 WHERE id=$3 RETURNING *', [request_id, date, id])
res.json(newEnrollment.rows[0])
}
async deleteEnrollment(req, res) {
const id = req.params.id
const enrollments = await db.query('DELETE FROM enrollment WHERE id=$1', [id])
res.json(enrollments.rows[0])
}
}
module.exports = new EnrollmentController()

View File

@ -0,0 +1,32 @@
const db = require('../db')
const ExamResultControllerInterface = require('./interfaces/examresult.controller.interface')
class ExamResultController extends ExamResultControllerInterface {
async createExamResult(req, res) {
const {title, result, abitur_id} = req.body
const newExamResult = await db.query('INSERT INTO exam_result (title, result, abitur_id) VALUES ($1, $2, $3) RETURNING *', [title, result, abitur_id])
res.json(newExamResult.rows[0])
}
async getExamResults(req, res) {
const results = await db.query('SELECT * FROM exam_result')
res.json(results.rows)
}
async getOneExamResult(req, res) {
const id = req.params.id
const results = await db.query('SELECT * FROM exam_result WHERE id=$1', [id])
res.json(results.rows[0])
}
async updateExamResult(req, res) {
const {id, title, result} = req.body
const newExamResult = await db.query('UPDATE exam_result SET title=$1, result=$2 WHERE id=$3 RETURNING *', [title, result, id])
res.json(newExamResult.rows[0])
}
async deleteExamResult(req, res) {
const id = req.params.id
const results = await db.query('DELETE FROM exam_result WHERE id=$1', [id])
res.json(results.rows[0])
}
}
module.exports = new ExamResultController()

View File

@ -8,6 +8,5 @@ module.exports = class AbiturControllerInterface {
async updateAbitur(req, res) {
}
async deleteAbitur(req, res) {
}
}

View File

@ -0,0 +1,12 @@
module.exports = class EducationFormControllerInterface {
async createEducationForm(req, res) {
}
async getEducationForms(req, res) {
}
async getOneEducationForm(req, res) {
}
async updateEducationForm(req, res) {
}
async deleteEducationForm(req, res) {
}
}

View File

@ -0,0 +1,12 @@
module.exports = class EnrollmentControllerInterface {
async createEnrollment(req, res) {
}
async getEnrollments(req, res) {
}
async getOneEnrollment(req, res) {
}
async updateEnrollment(req, res) {
}
async deleteEnrollment(req, res) {
}
}

View File

@ -0,0 +1,12 @@
module.exports = class ExamResultControllerInterface {
async createExamResult(req, res) {
}
async getExamResults(req, res) {
}
async getOneExamResult(req, res) {
}
async updateExamResult(req, res) {
}
async deleteExamResult(req, res) {
}
}

View File

@ -0,0 +1,12 @@
module.exports = class RequestControllerInterface {
async createRequest(req, res) {
}
async getRequests(req, res) {
}
async getOneRequest(req, res) {
}
async updateRequest(req, res) {
}
async deleteRequest(req, res) {
}
}

View File

@ -0,0 +1,8 @@
module.exports = class Request_ExamResultControllerInterface {
async createRecord(req, res) {
}
async getRecords(req, res) {
}
async deleteRecord(req, res) {
}
}

View File

@ -0,0 +1,12 @@
module.exports = class SpecializationControllerInterface {
async createSpecialization(req, res) {
}
async getSpecializations(req, res) {
}
async getOneSpecialization(req, res) {
}
async updateSpecialization(req, res) {
}
async deleteSpecialization(req, res) {
}
}

View File

@ -0,0 +1,32 @@
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 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])
}
}
module.exports = new RequestController()

View File

@ -0,0 +1,23 @@
const db = require('../db')
const Request_ExamResultControllerInterface = require('./interfaces/request_examresult.controller.interface')
class Request_ExamResultController extends Request_ExamResultControllerInterface {
async createRecord(req, res) {
const {request_id, exam_result_id} = req.body
const newRecord = await db.query('INSERT INTO request_exam_result (request_id, exam_result_id) VALUES ($1, $2) RETURNING *', [request_id, exam_result_id])
res.json(newRecord.rows[0])
}
async getRecords(req, res) {
const id = req.params.id
const records = await db.query('SELECT * FROM request_exam_result WHERE request_id=$1', [id])
res.json(records.rows)
}
async deleteRecord(req, res) {
const {request_id, exam_result_id} = req.body
const records = await db.query('DELETE FROM request_exam_result WHERE request_id=$1 AND exam_result_id=$2', [request_id, exam_result_id])
res.json(records.rows[0])
}
}
module.exports = new Request_ExamResultController()

View File

@ -0,0 +1,32 @@
const db = require('../db')
const SpecializationControllerInterface = require('./interfaces/specialization.controller.interface')
class SpecializationController extends SpecializationControllerInterface {
async createSpecialization(req, res) {
const {title} = req.body
const newSpecialization = await db.query('INSERT INTO specialization (title) VALUES ($1) RETURNING *', [title])
res.json(newSpecialization.rows[0])
}
async getSpecializations(req, res) {
const specializations = await db.query('SELECT * FROM specialization')
res.json(specializations.rows)
}
async getOneSpecialization(req, res) {
const id = req.params.id
const specializations = await db.query('SELECT * FROM specialization WHERE id=$1', [id])
res.json(specializations.rows[0])
}
async updateSpecialization(req, res) {
const {id, title} = req.body
const newSpecialization = await db.query('UPDATE specialization SET title=$1 WHERE id=$2 RETURNING *', [title, id])
res.json(newSpecialization.rows[0])
}
async deleteSpecialization(req, res) {
const id = req.params.id
const specializations = await db.query('DELETE FROM specialization WHERE id=$1', [id])
res.json(specializations.rows[0])
}
}
module.exports = new SpecializationController()

View File

@ -1,6 +1,12 @@
const express = require('express')
const abiturRouter = require('./routes/abitur.routes')
const examResultRouter = require('./routes/examresult.routes')
const specializationRouter = require('./routes/specialization.routes')
const educationFormRouter = require('./routes/educationform.routes')
const requestRouter = require('./routes/request.routes')
const request_examresultRouter = require('./routes/request_examresult.routes')
const enrollmentRouter = require('./routes/enrollment.routes')
const PORT = process.env.PORT || 8080
@ -8,5 +14,12 @@ const app = express()
app.use(express.json())
app.use('/api', abiturRouter)
app.use('/api', examResultRouter)
app.use('/api', specializationRouter)
app.use('/api', educationFormRouter)
app.use('/api', requestRouter)
app.use('/api', request_examresultRouter)
app.use('/api', enrollmentRouter)
app.listen(PORT, () => console.log('server started on port ' + PORT))

View File

@ -1,7 +1,7 @@
const Router = require('express')
const router = new Router()
const abiturController = require('../controllers/abitur.controllers')
const abiturController = require('../controllers/abitur.controller')
router.post('/abitur', abiturController.createAbitur)
router.get('/abitur', abiturController.getAbiturs)

View File

@ -0,0 +1,12 @@
const Router = require('express')
const router = new Router()
const educationformController = require('../controllers/educationform.controller')
router.post('/educationform', educationformController.createEducationForm)
router.get('/educationform', educationformController.getEducationForms)
router.get('/educationform/:id', educationformController.getOneEducationForm)
router.put('/educationform', educationformController.updateEducationForm)
router.delete('/educationform/:id', educationformController.deleteEducationForm)
module.exports = router

View File

@ -0,0 +1,12 @@
const Router = require('express')
const router = new Router()
const enrollmentController = require('../controllers/enrollment.controller')
router.post('/enrollment', enrollmentController.createEnrollment)
router.get('/enrollment', enrollmentController.getEnrollments)
router.get('/enrollment/:id', enrollmentController.getOneEnrollment)
router.put('/enrollment', enrollmentController.updateEnrollment)
router.delete('/enrollment/:id', enrollmentController.deleteEnrollment)
module.exports = router

View File

@ -0,0 +1,12 @@
const Router = require('express')
const router = new Router()
const examResultController = require('../controllers/examresult.controller')
router.post('/examresult', examResultController.createExamResult)
router.get('/examresult', examResultController.getExamResults)
router.get('/examresult/:id', examResultController.getOneExamResult)
router.put('/examresult', examResultController.updateExamResult)
router.delete('/examresult/:id', examResultController.deleteExamResult)
module.exports = router

View File

@ -0,0 +1,12 @@
const Router = require('express')
const router = new Router()
const requestController = require('../controllers/request.controller')
router.post('/request', requestController.createRequest)
router.get('/request', requestController.getRequests)
router.get('/request/:id', requestController.getRequests)
router.put('/request', requestController.updateRequest)
router.delete('/request/:id', requestController.deleteRequest)
module.exports = router

View File

@ -0,0 +1,10 @@
const Router = require('express')
const router = new Router()
const request_examresultController = require('../controllers/request_examresult.controller')
router.post('/request_examresult', request_examresultController.createRecord)
router.get('/request_examresult/:id', request_examresultController.getRecords)
router.delete('/request_examresult', request_examresultController.deleteRecord)
module.exports = router

View File

@ -0,0 +1,12 @@
const Router = require('express')
const router = new Router()
const specializationController = require('../controllers/specialization.controller')
router.post('/specialization', specializationController.createSpecialization)
router.get('/specialization', specializationController.getSpecializations)
router.get('/specialization/:id', specializationController.getOneSpecialization)
router.put('/specialization', specializationController.updateSpecialization)
router.delete('/specialization/:id', specializationController.deleteSpecialization)
module.exports = router