From da7989da5a2931db91f42522edd6c3f8820c6b40 Mon Sep 17 00:00:00 2001 From: sakacj Date: Tue, 23 Apr 2024 20:44:12 +0200 Subject: [PATCH] =?UTF-8?q?Opraveno=20posouv=C3=A1n=C3=AD=20rozvrhu?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/controllers/ScheduleController.js | 15 ++++++++++++++ .../booking-service/src/models/Schedule.js | 20 ++++++++++++++++++- .../src/routes/scheduleRoutes.js | 3 +++ .../src/services/ScheduleService.js | 9 +++++++++ GymNest-IS/frontend/src/hooks/useSchedules.js | 2 +- GymNest-IS/membership-service/src/app.js | 2 +- GymNest-IS/user-service/src/app.js | 10 +++++----- 7 files changed, 53 insertions(+), 8 deletions(-) diff --git a/GymNest-IS/booking-service/src/controllers/ScheduleController.js b/GymNest-IS/booking-service/src/controllers/ScheduleController.js index dd6d83b09..dc3c43ca0 100644 --- a/GymNest-IS/booking-service/src/controllers/ScheduleController.js +++ b/GymNest-IS/booking-service/src/controllers/ScheduleController.js @@ -57,6 +57,21 @@ class ScheduleController { } } + async findAllSchedulesByRange(req, res) { + try { + const { start, end } = req.query; + let schedules; + if (start && end) { + schedules = await ScheduleService.findSchedulesByDateRange(start, end); + } else { + schedules = await ScheduleService.findAllSchedules(); + } + res.json(schedules); + } catch (error) { + res.status(500).json({ message: error.message }); + } + } + } module.exports = new ScheduleController(); diff --git a/GymNest-IS/booking-service/src/models/Schedule.js b/GymNest-IS/booking-service/src/models/Schedule.js index ff1377b72..359f03e83 100644 --- a/GymNest-IS/booking-service/src/models/Schedule.js +++ b/GymNest-IS/booking-service/src/models/Schedule.js @@ -1,4 +1,5 @@ -const { Model, DataTypes } = require('sequelize'); +const { Sequelize, Model, DataTypes } = require('sequelize'); +const { Op } = require('sequelize'); const sequelize = require('../sequelize'); class Schedule extends Model { @@ -62,6 +63,23 @@ class Schedule extends Model { } } + static async findSchedulesByDateRange(start, end) { + try { + return await this.findAll({ + where: { + startTime: { + [Op.gte]: new Date(start) + }, + endTime: { + [Op.lte]: new Date(end) + } + } + }); + } catch (error) { + throw error; + } + } + } Schedule.init({ diff --git a/GymNest-IS/booking-service/src/routes/scheduleRoutes.js b/GymNest-IS/booking-service/src/routes/scheduleRoutes.js index 64cd308dc..91f03e3cb 100644 --- a/GymNest-IS/booking-service/src/routes/scheduleRoutes.js +++ b/GymNest-IS/booking-service/src/routes/scheduleRoutes.js @@ -116,4 +116,7 @@ router.get('/schedules/find/:scheduleId', ScheduleController.findScheduleById); */ router.get('/schedules/all', ScheduleController.findAllSchedules); +// Definování cesty pro získání seznamu všech harmonogramů podle zadaného rozsahu +router.get('/schedules/allByRange', ScheduleController.findAllSchedulesByRange); + module.exports = router; diff --git a/GymNest-IS/booking-service/src/services/ScheduleService.js b/GymNest-IS/booking-service/src/services/ScheduleService.js index c2c942d90..9cca9612f 100644 --- a/GymNest-IS/booking-service/src/services/ScheduleService.js +++ b/GymNest-IS/booking-service/src/services/ScheduleService.js @@ -51,6 +51,15 @@ class ScheduleService { } } + async findSchedulesByDateRange(start, end) { + try { + return await Schedule.findSchedulesByDateRange(start, end); + } catch (error) { + console.error("Error finding schedules by date range:", error); + throw error; + } + } + } module.exports = new ScheduleService(); diff --git a/GymNest-IS/frontend/src/hooks/useSchedules.js b/GymNest-IS/frontend/src/hooks/useSchedules.js index 4b8808f31..565d99ed2 100644 --- a/GymNest-IS/frontend/src/hooks/useSchedules.js +++ b/GymNest-IS/frontend/src/hooks/useSchedules.js @@ -33,7 +33,7 @@ export function useSchedules(currentWeek, viewMode, setLoading, setError) { try { const [schedulesResponse, activitiesResponse] = await Promise.all([ - axios.get(`${apiAddress}/schedules/all`, { + axios.get(`${apiAddress}/schedules/allByRange`, { params: { start: startDate, end: endDate }, headers: { 'Authorization': `Bearer ${token}` }, }), diff --git a/GymNest-IS/membership-service/src/app.js b/GymNest-IS/membership-service/src/app.js index 7ae2aaaa2..c9c147ba4 100644 --- a/GymNest-IS/membership-service/src/app.js +++ b/GymNest-IS/membership-service/src/app.js @@ -95,7 +95,7 @@ async function createInitialMembership(userId, typeId) { endDate, status: 'active' }); - console.log(`Membership created for user ID: ${userId}`); + console.log(`Membership created : ${userId}`); return membership; // Return the created membership object for further use } else { console.log(`Membership already exists for user ID: ${userId}`); diff --git a/GymNest-IS/user-service/src/app.js b/GymNest-IS/user-service/src/app.js index 84d6d2eab..eb39d98a7 100644 --- a/GymNest-IS/user-service/src/app.js +++ b/GymNest-IS/user-service/src/app.js @@ -58,10 +58,10 @@ app.listen(PORT, () => { // Synchronizace modelů s databází a nastavení asociací sequelize.sync({ force: false }).then(() => { - createInitialUser('admin', 'Test_admin1', 'admin@email.com', 'Ada', 'admin', 4, 1000).then(r => console.log('Inicial user created')).catch(err => console.error('Failed to create inicial user:', err)); - createInitialUser('coach', 'Test_coach1', 'coach@email.com', 'Cach', 'admin', 3, 500).then(r => console.log('Inicial user created')).catch(err => console.error('Failed to create inicial user:', err)); - createInitialUser('member', 'Test_member1', 'member@email.com', 'Martin', 'admin', 2, 800).then(r => console.log('Inicial user created')).catch(err => console.error('Failed to create inicial user:', err)); - createInitialUser('user', 'Test_user1', 'user@email.com', 'Usalam', 'admin', 1, 700).then(r => console.log('Inicial user created')).catch(err => console.error('Failed to create inicial user:', err)); + createInitialUser('admin', 'Test_admin1', 'admin@email.com', 'Ada', 'Adida', 4, 1000).then(r => console.log('Inicial user "admin" was created')).catch(err => console.error('Failed to create inicial user:', err)); + createInitialUser('coach', 'Test_coach1', 'coach@email.com', 'Cach', 'CocaCola', 3, 500).then(r => console.log('Inicial user "coach" was created')).catch(err => console.error('Failed to create inicial user:', err)); + createInitialUser('member', 'Test_member1', 'member@email.com', 'Martin', 'Merino', 2, 800).then(r => console.log('Inicial user "member" was created')).catch(err => console.error('Failed to create inicial user:', err)); + createInitialUser('user', 'Test_user1', 'user@email.com', 'Usalam', 'Upatla', 1, 700).then(r => console.log('Inicial user "user" was created')).catch(err => console.error('Failed to create inicial user:', err)); console.log('Databáze a tabulky byly synchronizovány'); }).catch(err => console.error('Při synchronizaci databáze došlo k chybě:', err)); @@ -84,7 +84,7 @@ async function createInitialUser(username, password, email, firstName, lastName, credits }); - console.log('Initial user created:', user); + console.log('Initial user created:', username); } else { console.log('Initial user already exists:', username); }