Skip to content

Commit 6389da7

Browse files
committed
fix booking and db loading
1 parent 6668dbe commit 6389da7

5 files changed

Lines changed: 45 additions & 3 deletions

File tree

.env

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ DB_PASSWORD=password
33
DB_NAME=project_db
44
DB_HOST=127.0.0.1
55
DB_PORT=8529
6-
DATABASE_URL=http://db:8529
6+
DATABASE_URL=http://db:8529
7+
SEED_ON_STARTUP=true

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
Для запуска:
2222
sudo docker compose build --no-cache && sudo docker compose up
2323

24-
http://localhost/
24+
Фронтенд доступен по адресу: http://localhost:8080/
25+
API (бэкенд) слушает: http://localhost:3000/
2526

2627
ivanov@uni.edu - логин пользователя
2728
admin - пароль пользователя

backend/src/app.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,12 @@ app.use((err, req, res, next) => {
3838
});
3939

4040
const PORT = process.env.PORT || 3000;
41-
await seedDatabase();
41+
if (process.env.SEED_ON_STARTUP === 'true') {
42+
await seedDatabase();
43+
} else {
44+
console.log('SEED_ON_STARTUP != true — пропуск автоматического посева БД.');
45+
}
46+
4247
app.listen(PORT, '0.0.0.0', () => {
4348
console.log(`[server]: Server is running at http://localhost:${PORT}`);
4449
});

backend/src/dao/bookings.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,25 @@ class BookingDao {
9595
return await cursor.all();
9696
}
9797

98+
/**
99+
* Method: findActiveByUser
100+
* Найти активные бронирования пользователя на конкретное время.
101+
*/
102+
async findActiveByUser(userId, startTime, transaction = null) {
103+
const fromId = userId.includes('/') ? userId : `Users/${userId}`;
104+
105+
const queryFn = () => db.query(aql`
106+
FOR b IN Bookings
107+
FILTER b._from == ${fromId}
108+
FILTER b.status != 'cancelled'
109+
FILTER b.start_at == ${startTime}
110+
RETURN b
111+
`);
112+
113+
const cursor = transaction ? await transaction.step(queryFn) : await queryFn();
114+
return await cursor.all();
115+
}
116+
98117
/**
99118
* Method: findFreePC
100119
* Найти первый свободный ПК по тегам.

backend/src/services/bookingService.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,14 @@ class BookingService {
108108
const toId = pcId.includes('/') ? pcId : `Computers/${pcId}`;
109109

110110
return withTransaction({ write: ['Bookings'], read: ['Computers'] }, async (transaction) => {
111+
// Проверяем, не забронировал ли пользователь уже ПК на этот урок
112+
const userExisting = await BookingDao.findActiveByUser(userId, interval.start, transaction);
113+
if (userExisting.length > 0) {
114+
const e = new Error("У вас уже есть бронь на этот урок");
115+
e.status = 409;
116+
throw e;
117+
}
118+
111119
const existing = await BookingDao.findActiveByPC(toId, interval.start, transaction);
112120
if (existing.length > 0) {
113121
const e = new Error("Это место уже забронировано");
@@ -187,6 +195,14 @@ class BookingService {
187195
const fromId = `Users/${userId}`;
188196

189197
return withTransaction({ read: ['Computers'], write: ['Bookings'] }, async (transaction) => {
198+
// Проверяем, не забронировал ли пользователь уже место на этот урок
199+
const userExisting = await BookingDao.findActiveByUser(userId, interval.start, transaction);
200+
if (userExisting.length > 0) {
201+
const e = new Error("У вас уже есть бронь на этот урок");
202+
e.status = 409;
203+
throw e;
204+
}
205+
190206
const freePC = await BookingDao.findFreePC({ startTime: interval.start, tagList }, transaction);
191207
if (!freePC) {
192208
const e = new Error("Нет свободных ПК по вашему запросу");

0 commit comments

Comments
 (0)