Änderungen am TAgebuch
This commit is contained in:
@@ -1,52 +1,77 @@
|
||||
import Club from '../models/Club.js';
|
||||
import UserClub from '../models/UserClub.js';
|
||||
import User from '../models/User.js';
|
||||
import Member from '../models/Member.js';
|
||||
import { Op, fn, where, col } from 'sequelize';
|
||||
import { getUserByToken } from '../utils/userUtils.js';
|
||||
|
||||
const getClubs = async (req, res) => {
|
||||
const clubs = await Club.findAll();
|
||||
res.status(200).json(clubs);
|
||||
try {
|
||||
console.log('[getClubs] - get clubs');
|
||||
const clubs = await Club.findAll();
|
||||
console.log('[getClubs] - prepare response');
|
||||
res.status(200).json(clubs);
|
||||
console.log('[getClubs] - done');
|
||||
} catch (error) {
|
||||
console.log('[getClubs] - error');
|
||||
console.log(error);
|
||||
res.status(500).json({ error: "internalerror" });
|
||||
}
|
||||
};
|
||||
|
||||
const addClub = async (req, res) => {
|
||||
console.log('[addClub] - Read out parameters');
|
||||
const { authcode: token } = req.headers;
|
||||
const { name: clubName } = req.body;
|
||||
console.log('[addClub] - find club by name');
|
||||
const club = await Club.findOne({
|
||||
where: where(fn('LOWER', col('name')), 'LIKE', `%${clubName.toLowerCase()}%`)
|
||||
});
|
||||
console.log('[addClub] - get user');
|
||||
const user = await getUserByToken(token);
|
||||
console.log('[addClub] - check if club already exists');
|
||||
if (club) {
|
||||
res.status(409).json({ error: "alreadyexists" });
|
||||
return;
|
||||
}
|
||||
const newClub = Club.create({ name: clubName });
|
||||
console.log('[addClub] - create club');
|
||||
const newClub = await Club.create({ name: clubName });
|
||||
console.log('[addClub] - add user to new club');
|
||||
UserClub.create({ userId: user.id, clubId: newClub.id, approved: true });
|
||||
console.log('[addClub] - prepare response');
|
||||
res.status(200).json(newClub);
|
||||
console.log('[addClub] - done');
|
||||
}
|
||||
|
||||
const getClub = async (req, res) => {
|
||||
const { authcode: token } = req.headers;
|
||||
const { clubid: clubId } = req.params;
|
||||
console.log('load club', token)
|
||||
const user = await getUserByToken(token);
|
||||
console.log(user);
|
||||
const access = await UserClub.findAll({
|
||||
where: {
|
||||
userId: user.id,
|
||||
clubId: clubId,
|
||||
}
|
||||
});
|
||||
if (access.length === 0 || !access[0].approved) {
|
||||
res.status(403).json({ error: "noaccess", status: access.length === 0 ? "notrequested" : "requested" });
|
||||
return;
|
||||
}
|
||||
console.log('[getClub] - start');
|
||||
try {
|
||||
const { authcode: token } = req.headers;
|
||||
const { clubid: clubId } = req.params;
|
||||
console.log('[getClub] - get user');
|
||||
const user = await getUserByToken(token);
|
||||
console.log('[getClub] - get users club');
|
||||
const access = await UserClub.findAll({
|
||||
where: {
|
||||
userId: user.id,
|
||||
clubId: clubId,
|
||||
}
|
||||
});
|
||||
console.log('[getClub] - check access');
|
||||
if (access.length === 0 || !access[0].approved) {
|
||||
res.status(403).json({ error: "noaccess", status: access.length === 0 ? "notrequested" : "requested" });
|
||||
return;
|
||||
}
|
||||
console.log('[getClub] - get club');
|
||||
const club = await Club.findByPk(clubId, {
|
||||
include: [
|
||||
{
|
||||
model: Member,
|
||||
as: 'Members',
|
||||
as: 'members',
|
||||
order: [
|
||||
['lastName', 'ASC'], // Sortiere nach Nachname aufsteigend
|
||||
['firstName', 'ASC'] // Sortiere nach Vorname aufsteigend
|
||||
],
|
||||
},
|
||||
{
|
||||
model: User,
|
||||
@@ -57,13 +82,15 @@ const getClub = async (req, res) => {
|
||||
}
|
||||
]
|
||||
});
|
||||
|
||||
console.log('[getClub] - check club exists');
|
||||
if (!club) {
|
||||
return res.status(404).json({ message: 'Club not found' });
|
||||
}
|
||||
console.log('[getClub] - set response');
|
||||
res.status(200).json(club);
|
||||
console.log('[getClub] - done');
|
||||
} catch (error) {
|
||||
console.error('err', error);
|
||||
console.log(error);
|
||||
res.status(500).json({ message: 'Server error' });
|
||||
}
|
||||
}
|
||||
@@ -106,7 +133,7 @@ const approveClubAccess = async (req, res) => {
|
||||
res.status(200).json({ status: 'ok' });
|
||||
}
|
||||
|
||||
const requestClubAccess = async(req, res) => {
|
||||
const requestClubAccess = async (req, res) => {
|
||||
const { authcode: token } = req.headers;
|
||||
const { clubid: clubId } = req.params;
|
||||
const user = await getUserByToken(token);
|
||||
@@ -114,16 +141,18 @@ const requestClubAccess = async(req, res) => {
|
||||
const access = await UserClub.findAll({
|
||||
where: {
|
||||
userId: user.id,
|
||||
clubId: clubId,
|
||||
clubId: clubId,
|
||||
}
|
||||
});
|
||||
if (access.length > 0) {
|
||||
res.status(409).json({ err: "alreadyrequested"});
|
||||
res.status(409).json({ err: "alreadyrequested" });
|
||||
return;
|
||||
}
|
||||
const club = Club.findOne({ where: {
|
||||
id: clubId
|
||||
}});
|
||||
const club = Club.findOne({
|
||||
where: {
|
||||
id: clubId
|
||||
}
|
||||
});
|
||||
if (!club) {
|
||||
res.status(404).json({ err: "clubnotfound" });
|
||||
return;
|
||||
|
||||
54
backend/controllers/diaryController.js
Normal file
54
backend/controllers/diaryController.js
Normal file
@@ -0,0 +1,54 @@
|
||||
import DiaryService from '../services/diaryService.js';
|
||||
import HttpError from '../exceptions/HttpError.js';
|
||||
|
||||
const diaryService = new DiaryService();
|
||||
|
||||
const getDatesForClub = async (req, res) => {
|
||||
try {
|
||||
const { clubId } = req.params;
|
||||
const { authcode: userToken } = req.headers;
|
||||
const dates = await diaryService.getDatesForClub(userToken, clubId);
|
||||
res.status(200).json(dates);
|
||||
} catch (error) {
|
||||
console.error('[getDatesForClub] - Error:', error);
|
||||
res.status(error.statusCode || 500).json({ error: error.message });
|
||||
}
|
||||
};
|
||||
|
||||
const createDateForClub = async (req, res) => {
|
||||
try {
|
||||
const { clubId } = req.params;
|
||||
const { authcode: userToken } = req.headers;
|
||||
const { date, trainingStart, trainingEnd } = req.body;
|
||||
if (!date) {
|
||||
throw new HttpError('The date field is required', 400);
|
||||
}
|
||||
if (isNaN(new Date(date).getTime())) {
|
||||
throw new HttpError('Invalid date format', 400);
|
||||
}
|
||||
const newDate = await diaryService.createDateForClub(userToken, clubId, date, trainingStart, trainingEnd);
|
||||
res.status(201).json(newDate);
|
||||
} catch (error) {
|
||||
console.error('[createDateForClub] - Error:', error);
|
||||
res.status(error.statusCode || 500).json({ error: error.message });
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
const updateTrainingTimes = async (req, res) => {
|
||||
try {
|
||||
const { clubId } = req.params;
|
||||
const { authcode: userToken } = req.headers;
|
||||
const { date, trainingStart, trainingEnd } = req.body;
|
||||
if (!date || !trainingStart || !trainingEnd) {
|
||||
throw new HttpError('All fields (date, trainingStart, trainingEnd) are required', 400);
|
||||
}
|
||||
const updatedDate = await diaryService.updateTrainingTimes(userToken, clubId, date, trainingStart, trainingEnd);
|
||||
res.status(200).json(updatedDate);
|
||||
} catch (error) {
|
||||
console.error('[updateTrainingTimes] - Error:', error);
|
||||
res.status(error.statusCode || 500).json({ error: error.message });
|
||||
}
|
||||
};
|
||||
|
||||
export { getDatesForClub, createDateForClub, updateTrainingTimes };
|
||||
@@ -1,19 +1,40 @@
|
||||
import MemberService from "../services/memberService.js";
|
||||
|
||||
const getClubMembers = async(req, res) => {
|
||||
const { clubid: clubId } = req.body;
|
||||
res.status(200).json(MemberService.getClubMembers(clubId));
|
||||
try {
|
||||
const { authcode: userToken } = req.headers;
|
||||
const { id: clubId } = req.params;
|
||||
console.log('[getClubMembers]', userToken, clubId);
|
||||
res.status(200).json(await MemberService.getClubMembers(userToken, clubId));
|
||||
} catch(error) {
|
||||
console.log('[getClubMembers] - Error: ', error);
|
||||
res.status(500).json({ error: 'systemerror' });
|
||||
}
|
||||
}
|
||||
|
||||
const getWaitingApprovals = async(req, res) => {
|
||||
try {
|
||||
const { clubid: clubId } = req.params;
|
||||
const { authcode: userToken} = req.headers;
|
||||
const waitingApprovals = MemberService.getApprovalRequests(userToken, clubId);
|
||||
console.log('[getWaitingApprovals] - Start');
|
||||
const { id: clubId } = req.params;
|
||||
console.log('[getWaitingApprovals] - get token');
|
||||
const { authcode: userToken } = req.headers;
|
||||
console.log('[getWaitingApprovals] - load for waiting approvals');
|
||||
const waitingApprovals = await MemberService.getApprovalRequests(userToken, clubId);
|
||||
console.log('[getWaitingApprovals] - set response');
|
||||
res.status(200).json(waitingApprovals);
|
||||
console.log('[getWaitingApprovals] - done');
|
||||
} catch(error) {
|
||||
console.log('[getWaitingApprovals] - Error: ', error);
|
||||
res.status(403).json({ error: error });
|
||||
}
|
||||
}
|
||||
|
||||
const setClubMembers = async(req, res) => {
|
||||
const { id: memberId, firstname: firstName, lastname: lastName, street, city, birthdate, phone, email} = req.body;
|
||||
const { id: clubId } = req.params;
|
||||
const { authcode: userToken } = req.headers;
|
||||
const addResult = await MemberService.setClubMember(userToken, clubId, memberId, firstName, lastName, street, city, birthdate, phone, email);
|
||||
res.status(addResult.status || 500).json(addResult.response);
|
||||
}
|
||||
|
||||
export { getClubMembers, getWaitingApprovals };
|
||||
export { getClubMembers, getWaitingApprovals, setClubMembers };
|
||||
Reference in New Issue
Block a user