2023-05-09 17:44:03 +04:00

209 lines
6.8 KiB
JavaScript

const express = require('express')
const cors = require('cors')
const faculty = require('./models/faculty')
const direction = require('./models/direction')
const subject = require('./models/Subject')
const teacher = require('./models/teacher')
const test = require('./models/test')
const student = require('./models/student')
const score = require('./models/score')
const app = express()
const port = 3001
app.use(cors())
app.use(express.json())
app.use(function (req, res, next) {
res.setHeader('Access-Control-Allow-Origin', 'http://localhost:3000')
res.setHeader('Access-Control-Allow-Methods', 'GET,POST,PUT,DELETE,OPTIONS')
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Access-Control-Allow-Headers')
next();
});
// ФАКУЛЬТЕТ-------------------------------------
app.post('/faculty', (req, res) => {
faculty.create(req.body.name)
.then(response => res.status(200).send(response))
.catch(error => res.status(500).send(error))
})
app.get('/faculty', (req, res) => {
faculty.get()
.then(response => res.status(200).send(response))
.catch(error => res.status(500).send(error))
})
app.delete('/faculty/del/:id', (req, res) => {
faculty.del(req.params.id)
.then(response => res.status(200).send(response))
.catch(error => res.status(500).send(error))
})
app.put('/faculty/upd/:id', (req, res) => {
faculty.update(req.params.id, req.body.name)
.then(response => res.status(200).send(response))
.catch(error => res.status(500).send(error))
})
//----------------------------------------------
// TEST TYPE-------------------------------------
app.post('/test', (req, res) => {
test.create(req.body.name)
.then(response => res.status(200).send(response))
.catch(error => res.status(500).send(error))
})
app.get('/test', (req, res) => {
test.get()
.then(response => res.status(200).send(response))
.catch(error => res.status(500).send(error))
})
app.delete('/test/del/:id', (req, res) => {
test.del(req.params.id)
.then(response => res.status(200).send(response))
.catch(error => res.status(500).send(error))
})
app.put('/test/upd/:id', (req, res) => {
test.update(req.params.id, req.body.name)
.then(response => res.status(200).send(response))
.catch(error => res.status(500).send(error))
})
//----------------------------------------------
// TEACHER-------------------------------------
app.post('/teacher', (req, res) => {
teacher.create(req.body.name, req.body.surname,req.body.post)
.then(response => res.status(200).send(response))
.catch(error => res.status(500).send(error))
})
app.get('/teacher', (req, res) => {
teacher.get()
.then(response => res.status(200).send(response))
.catch(error => res.status(500).send(error))
})
app.delete('/teacher/del/:id', (req, res) => {
teacher.del(req.params.id)
.then(response => res.status(200).send(response))
.catch(error => res.status(500).send(error))
})
app.put('/teacher/upd/:id', (req, res) => {
teacher.update(req.params.id, req.body.name, req.body.surname,req.body.post)
.then(response => res.status(200).send(response))
.catch(error => res.status(500).send(error))
})
//----------------------------------------------
// SUBJECT-------------------------------------
app.post('/subject', (req, res) => {
subject.create(req.body.name, req.body.hours_count)
.then(response => res.status(200).send(response))
.catch(error => res.status(500).send(error))
})
app.get('/subject', (req, res) => {
subject.get()
.then(response => res.status(200).send(response))
.catch(error => res.status(500).send(error))
})
app.delete('/subject/del/:id', (req, res) => {
subject.del(req.params.id)
.then(response => res.status(200).send(response))
.catch(error => res.status(500).send(error))
})
app.put('/subject/upd/:id', (req, res) => {
subject.update(req.params.id, req.body.name, req.body.hours_count)
.then(response => res.status(200).send(response))
.catch(error => res.status(500).send(error))
})
//----------------------------------------------
// DIRECTION-------------------------------------
app.post('/direction', (req, res) => {
direction.create(req.body.name, req.body.faculty_id)
.then(response => res.status(200).send(response))
.catch(error => res.status(500).send(error))
})
app.get('/direction', (req, res) => {
direction.get()
.then(response => res.status(200).send(response))
.catch(error => res.status(500).send(error))
})
app.delete('/direction/del/:id', (req, res) => {
direction.del(req.params.id)
.then(response => res.status(200).send(response))
.catch(error => res.status(500).send(error))
})
app.put('/direction/upd/:id', (req, res) => {
direction.update(req.params.id, req.body.name, req.body.facultyId)
.then(response => res.status(200).send(response))
.catch(error => res.status(500).send(error))
})
//----------------------------------------------
// STUDENT-------------------------------------
app.post('/student', (req, res) => {
student.create(req.body.name, req.body.surname, req.body.record_book, req.body.direction_id)
.then(response => res.status(200).send(response))
.catch(error => res.status(500).send(error))
})
app.get('/student', (req, res) => {
student.get()
.then(response => res.status(200).send(response))
.catch(error => res.status(500).send(error))
})
app.delete('/student/del/:id', (req, res) => {
student.del(req.params.id)
.then(response => res.status(200).send(response))
.catch(error => res.status(500).send(error))
})
app.put('/student/upd/:id', (req, res) => {
student.update(req.params.id, req.body.name, req.body.surname, req.body.record_book, req.body.direction_id)
.then(response => res.status(200).send(response))
.catch(error => res.status(500).send(error))
})
//----------------------------------------------
// SCORE-------------------------------------
app.post('/score', (req, res) => {
score.create(req.body.value, req.body.subject_id, req.body.test_type_id, req.body.teacher_id, req.body.student_id)
.then(response => res.status(200).send(response))
.catch(error => res.status(500).send(error))
})
app.get('/score', (req, res) => {
score.get()
.then(response => res.status(200).send(response))
.catch(error => res.status(500).send(error))
})
app.delete('/score/del/:id', (req, res) => {
score.del(req.params.id)
.then(response => res.status(200).send(response))
.catch(error => res.status(500).send(error))
})
app.put('/score/upd/:id', (req, res) => {
score.update(req.params.id, req.body.value, req.body.subjectId, req.body.testId, req.body.teacherId, req.body.studentId)
.then(response => res.status(200).send(response))
.catch(error => res.status(500).send(error))
})
//----------------------------------------------
app.listen(port, () => {
console.log(`app runnong on port ${port}`)
})