-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
226 lines (188 loc) · 6.92 KB
/
index.js
File metadata and controls
226 lines (188 loc) · 6.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
const { response } = require('express');
const express = require('express') //подключаем библиотеку express, упрощает запуск сервера, готовые функции
const mysql = require('mysql2') //подключаем библиотеку для работы с базой данных
const cors = require('cors')
const app = express() // мы создаем константу app, вызывает express()
const PORT = 4000 //создаем порт нашего сервера
const timeConstants = require('./consts/time_consts')
// подключаемся к базе данных
const connection = mysql.createConnection({
host: "localhost", //адрес базы данных
user: "root",
database: "databases", //название бд
password: "root"
});
app.use(cors())
let students = []
let lessons = []
app.get('/api/lessons/:id', (req, res) => {//достаю список предметов по id препода из совпадений id в lessons
const id = req.params.id
let sql = `SELECT lessonId FROM groups_attendance WHERE groupId=${id}`;
connection.query(sql, (err, data) => {
data.forEach((item) => {
getLessons(item.lessonId)
})
})
res.json(lessons)
lessons = []
})
function getLessons(id) {
let sql = `SELECT * FROM lessons WHERE Id=${id}`;
connection.query(sql, (err, data) => {
data.forEach((item) => {
lessons.push(item)
})
})
}
app.get('/api/get-lessons/:id', (req, res) => {
const teacherId = req.params.id;
let sql = `SELECT * FROM Lessons WHERE TeacherID=${teacherId}`
connection.query(sql, (err, data) => {
res.json(data)
})
})
app.get('/api/get-groups/:id', (req, res) => {// id предмета достаем id группы и все о ней знаем
const lessonId = req.params.id;
let sql = `SELECT DISTINCT GroupId , GroupName FROM Groups INNER JOIN
timitable ON Groups.Id = timitable.GroupId WHERE LessonId=${lessonId}`
connection.query(sql, (err, data) => {
res.json(data)
})
})
app.get('/api/students/:id', (req, res) => {// id групппы получаем по этому id получаем студентов
const id = req.params.id
console.log(id)
let sql = `SELECT * FROM students WHERE GroupId=${id}`;
connection.query(sql, (err, data) => {
console.log(students)
res.send(data)
})
})
app.use(
express.urlencoded({
extended: true
})
)
app.use(express.json())
app.post('/api/attendance/', (req, res) => {
const code = req.body.code
let sql = `SELECT * FROM normal_attendance WHERE CardCode=${code}`
connection.query(sql, (err, data) => {
console.log(data)
res.send(data)
})
})
app.post('/api/timetable/', (req, res) => {//id пары и предмета но перед этим получаем список всех посещ ст
const id = req.body.id
const lessonId = req.body.lesson
console.log(id)
let sql = `SELECT * FROM timitable WHERE Id=${id} AND LessonId=${lessonId}`
connection.query(sql, (err, data) => {
console.log(data)
res.send(data)
})
})
app.post('/api/insert-attendance', (req, res) => {//обработчик для записи в базу
const id = req.body.id
let allCards = req.body.cards
let cards = allCards.split(',')
let sql = ``
cards.forEach((card) => {
sql = `INSERT INTO normal_attendance(CardCode, TimetableId, Presence) VALUES (${card.trim()}, ${id}, 1)`
console.log(sql)
connection.query(sql, (err, data) => {
console.log('SQL request has been executed')
})
})
})
app.post('/api/insert-attendance', (req, res) => {//обработчик для записи в базу
const id = req.body.id
let allCards = req.body.cards
let cards = allCards.split(',')
let numOfPair = checkCurrentLesson()
let audience
if (numOfPair) {
let audienceSql = ` SELECT audience FROM slots WHERE id=${id}`
connection.execute(audienceSql, (err, data) => {
audience = data;
})
}
if (audience && numOfPair) {
let date = new Date()
date = date.toLocaleDateString()
let timitableId
let timetableSql = ` SELECT id FROM timitabe WHERE audience=${audience} AND Day=${date} AND NumOfPair=${numOfPair}`
connection.execute(audienceSql, (err, data) => {
timitableId = data;
})
}
let sql = ``
cards.forEach((card) => {
sql = `INSERT INTO normal_attendance(CardCode, TimetableId, Presence) VALUES (${card.trim()}, ${timitableId}, 1)`
console.log(sql)
connection.query(sql, (err, data) => {
console.log('SQL request has been executed')
})
})
})
// app.post('api/signin/',(req, res)=>{
// let login=req.body.login
// let password=req.body.password
// console.log(password)
// })
app.post('/api/sign-in', (req, res) => {
const login = req.body.login
const password = req.body.password
let sql = `SELECT id FROM teacher WHERE login='${login}' AND password='${password}'`
connection.query(sql, (err, data) => {
console.log(data)
if (data) {
res.send(data[0])
} else {
res.status(200).send({ error: 'Something failed!' });
}
})
})
checkCurrentLesson()
function checkCurrentLesson() {
let nowDate = new Date();
let time = Date.parse(nowDate)
let currentLesson
if (timeConstants.LESSON_1_START <= time && time <= timeConstants.LESSON_1_END) {
currentLesson = 1
}
else if (timeConstants.LESSON_2_START <= time && time <= timeConstants.LESSON_2_END) {
currentLesson = 2
}
else if (timeConstants.LESSON_3_START <= time && time <= timeConstants.LESSON_3_END) {
currentLesson = 3
}
else if (timeConstants.LESSON_4_START <= time && time <= timeConstants.LESSON_4_END) {
currentLesson = 4
}
else if (timeConstants.LESSON_5_START <= time && time <= timeConstants.LESSON_5_END) {
currentLesson = 5
}
else if (timeConstants.LESSON_6_START <= time && time <= timeConstants.LESSON_6_END) {
currentLesson = 6
}
else if (timeConstants.LESSON_7_START <= time && time <= timeConstants.LESSON_7_END) {
currentLesson = 7
} else {
currentLesson = 0
}
return currentLesson;
}
app.get('/api/teacher-timetable/:id', (req, res) => {// id групппы получаем по этому id получаем студентов
const id = req.params.id
console.log(id)
let sql = `SELECT * FROM teachertimetable
LEFT JOIN lessons ON lessons.Id=teachertimetable.LessonId
WHERE teachertimetable.TeacherId=${id}
ORDER BY teachertimetable.Day`;
connection.query(sql, (err, data) => {
console.log(data)
res.send(data)
})
})
app.listen(PORT)