Erweitert die Backend- und Frontend-Funktionalität zur Unterstützung von Teams und Saisons. Fügt neue Routen für Team- und Club-Team-Management hinzu, aktualisiert die Match- und Team-Modelle zur Berücksichtigung von Saisons, und implementiert die Saison-Auswahl in der Benutzeroberfläche. Optimiert die Logik zur Abfrage von Ligen und Spielen basierend auf der ausgewählten Saison.
This commit is contained in:
192
backend/services/clubTeamService.js
Normal file
192
backend/services/clubTeamService.js
Normal file
@@ -0,0 +1,192 @@
|
||||
import ClubTeam from '../models/ClubTeam.js';
|
||||
import League from '../models/League.js';
|
||||
import Season from '../models/Season.js';
|
||||
import SeasonService from './seasonService.js';
|
||||
import { devLog } from '../utils/logger.js';
|
||||
|
||||
class ClubTeamService {
|
||||
/**
|
||||
* Holt alle ClubTeams für einen Verein, optional gefiltert nach Saison.
|
||||
* Wenn keine Saison-ID angegeben ist, wird die aktuelle Saison verwendet.
|
||||
* @param {number} clubId - Die ID des Vereins.
|
||||
* @param {number|null} seasonId - Optionale Saison-ID.
|
||||
* @returns {Promise<Array<ClubTeam>>} Eine Liste von ClubTeams.
|
||||
*/
|
||||
static async getAllClubTeamsByClub(clubId, seasonId = null) {
|
||||
try {
|
||||
devLog('[ClubTeamService.getAllClubTeamsByClub] - Getting club teams for club:', clubId, 'season:', seasonId);
|
||||
|
||||
// Wenn keine Saison angegeben, verwende die aktuelle
|
||||
if (!seasonId) {
|
||||
const currentSeason = await SeasonService.getOrCreateCurrentSeason();
|
||||
seasonId = currentSeason.id;
|
||||
}
|
||||
|
||||
const clubTeams = await ClubTeam.findAll({
|
||||
where: { clubId, seasonId },
|
||||
order: [['name', 'ASC']]
|
||||
});
|
||||
|
||||
// Manuelle Datenanreicherung für Liga und Saison
|
||||
const enrichedClubTeams = [];
|
||||
for (const clubTeam of clubTeams) {
|
||||
const enrichedTeam = {
|
||||
id: clubTeam.id,
|
||||
name: clubTeam.name,
|
||||
clubId: clubTeam.clubId,
|
||||
leagueId: clubTeam.leagueId,
|
||||
seasonId: clubTeam.seasonId,
|
||||
createdAt: clubTeam.createdAt,
|
||||
updatedAt: clubTeam.updatedAt,
|
||||
league: { name: 'Unbekannt' },
|
||||
season: { season: 'Unbekannt' }
|
||||
};
|
||||
|
||||
// Lade Liga-Daten
|
||||
if (clubTeam.leagueId) {
|
||||
const league = await League.findByPk(clubTeam.leagueId, { attributes: ['name'] });
|
||||
if (league) enrichedTeam.league = league;
|
||||
}
|
||||
|
||||
// Lade Saison-Daten
|
||||
if (clubTeam.seasonId) {
|
||||
const season = await Season.findByPk(clubTeam.seasonId, { attributes: ['season'] });
|
||||
if (season) enrichedTeam.season = season;
|
||||
}
|
||||
|
||||
enrichedClubTeams.push(enrichedTeam);
|
||||
}
|
||||
devLog('[ClubTeamService.getAllClubTeamsByClub] - Found club teams:', enrichedClubTeams.length);
|
||||
return enrichedClubTeams;
|
||||
} catch (error) {
|
||||
console.error('[ClubTeamService.getAllClubTeamsByClub] - Error:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Holt ein ClubTeam anhand seiner ID
|
||||
* @param {number} clubTeamId - Die ID des ClubTeams
|
||||
* @returns {Promise<ClubTeam|null>} Das ClubTeam oder null, wenn nicht gefunden
|
||||
*/
|
||||
static async getClubTeamById(clubTeamId) {
|
||||
try {
|
||||
devLog('[ClubTeamService.getClubTeamById] - Getting club team:', clubTeamId);
|
||||
const clubTeam = await ClubTeam.findByPk(clubTeamId, {
|
||||
include: [
|
||||
{
|
||||
model: League,
|
||||
as: 'league',
|
||||
attributes: ['id', 'name']
|
||||
},
|
||||
{
|
||||
model: Season,
|
||||
as: 'season',
|
||||
attributes: ['id', 'season']
|
||||
}
|
||||
]
|
||||
});
|
||||
devLog('[ClubTeamService.getClubTeamById] - Found club team:', clubTeam ? 'yes' : 'no');
|
||||
return clubTeam;
|
||||
} catch (error) {
|
||||
console.error('[ClubTeamService.getClubTeamById] - Error:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Erstellt ein neues ClubTeam.
|
||||
* Wenn keine Saison-ID angegeben ist, wird die aktuelle Saison zugewiesen.
|
||||
* @param {object} clubTeamData - Die Daten des neuen ClubTeams (name, clubId, optional leagueId, seasonId).
|
||||
* @returns {Promise<ClubTeam>} Das erstellte ClubTeam.
|
||||
*/
|
||||
static async createClubTeam(clubTeamData) {
|
||||
try {
|
||||
devLog('[ClubTeamService.createClubTeam] - Creating club team:', clubTeamData);
|
||||
|
||||
// Wenn keine Saison angegeben, verwende die aktuelle
|
||||
if (!clubTeamData.seasonId) {
|
||||
const currentSeason = await SeasonService.getOrCreateCurrentSeason();
|
||||
clubTeamData.seasonId = currentSeason.id;
|
||||
}
|
||||
|
||||
const clubTeam = await ClubTeam.create(clubTeamData);
|
||||
devLog('[ClubTeamService.createClubTeam] - Club team created with ID:', clubTeam.id);
|
||||
return clubTeam;
|
||||
} catch (error) {
|
||||
console.error('[ClubTeamService.createClubTeam] - Error:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Aktualisiert ein bestehendes ClubTeam.
|
||||
* @param {number} clubTeamId - Die ID des zu aktualisierenden ClubTeams.
|
||||
* @param {object} updateData - Die zu aktualisierenden Daten.
|
||||
* @returns {Promise<boolean>} True, wenn das ClubTeam aktualisiert wurde, sonst false.
|
||||
*/
|
||||
static async updateClubTeam(clubTeamId, updateData) {
|
||||
try {
|
||||
devLog('[ClubTeamService.updateClubTeam] - Updating club team:', clubTeamId, updateData);
|
||||
const [updatedRowsCount] = await ClubTeam.update(updateData, {
|
||||
where: { id: clubTeamId }
|
||||
});
|
||||
devLog('[ClubTeamService.updateClubTeam] - Updated rows:', updatedRowsCount);
|
||||
return updatedRowsCount > 0;
|
||||
} catch (error) {
|
||||
console.error('[ClubTeamService.updateClubTeam] - Error:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Löscht ein ClubTeam.
|
||||
* @param {number} clubTeamId - Die ID des zu löschenden ClubTeams.
|
||||
* @returns {Promise<boolean>} True, wenn das ClubTeam gelöscht wurde, sonst false.
|
||||
*/
|
||||
static async deleteClubTeam(clubTeamId) {
|
||||
try {
|
||||
devLog('[ClubTeamService.deleteClubTeam] - Deleting club team:', clubTeamId);
|
||||
const deletedRows = await ClubTeam.destroy({
|
||||
where: { id: clubTeamId }
|
||||
});
|
||||
devLog('[ClubTeamService.deleteClubTeam] - Deleted rows:', deletedRows);
|
||||
return deletedRows > 0;
|
||||
} catch (error) {
|
||||
console.error('[ClubTeamService.deleteClubTeam] - Error:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Holt alle Ligen für einen Verein, optional gefiltert nach Saison.
|
||||
* Wenn keine Saison-ID angegeben ist, wird die aktuelle Saison verwendet.
|
||||
* @param {number} clubId - Die ID des Vereins.
|
||||
* @param {number|null} seasonId - Optionale Saison-ID.
|
||||
* @returns {Promise<Array<League>>} Eine Liste von Ligen.
|
||||
*/
|
||||
static async getLeaguesByClub(clubId, seasonId = null) {
|
||||
try {
|
||||
devLog('[ClubTeamService.getLeaguesByClub] - Getting leagues for club:', clubId, 'season:', seasonId);
|
||||
|
||||
// Wenn keine Saison angegeben, verwende die aktuelle
|
||||
if (!seasonId) {
|
||||
const currentSeason = await SeasonService.getOrCreateCurrentSeason();
|
||||
seasonId = currentSeason.id;
|
||||
}
|
||||
|
||||
const leagues = await League.findAll({
|
||||
where: { clubId, seasonId },
|
||||
attributes: ['id', 'name', 'seasonId'],
|
||||
order: [['name', 'ASC']]
|
||||
});
|
||||
devLog('[ClubTeamService.getLeaguesByClub] - Found leagues:', leagues.length);
|
||||
return leagues;
|
||||
} catch (error) {
|
||||
console.error('[ClubTeamService.getLeaguesByClub] - Error:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default ClubTeamService;
|
||||
104
backend/services/leagueService.js
Normal file
104
backend/services/leagueService.js
Normal file
@@ -0,0 +1,104 @@
|
||||
import League from '../models/League.js';
|
||||
import Season from '../models/Season.js';
|
||||
import SeasonService from './seasonService.js';
|
||||
import { devLog } from '../utils/logger.js';
|
||||
|
||||
class LeagueService {
|
||||
static async getAllLeaguesByClub(clubId, seasonId = null) {
|
||||
try {
|
||||
devLog('[LeagueService.getAllLeaguesByClub] - Getting leagues for club:', clubId, 'season:', seasonId);
|
||||
|
||||
// Wenn keine Saison angegeben, verwende die aktuelle
|
||||
if (!seasonId) {
|
||||
const currentSeason = await SeasonService.getOrCreateCurrentSeason();
|
||||
seasonId = currentSeason.id;
|
||||
}
|
||||
|
||||
const leagues = await League.findAll({
|
||||
where: { clubId, seasonId },
|
||||
include: [
|
||||
{
|
||||
model: Season,
|
||||
as: 'season',
|
||||
attributes: ['id', 'season']
|
||||
}
|
||||
],
|
||||
order: [['name', 'ASC']]
|
||||
});
|
||||
devLog('[LeagueService.getAllLeaguesByClub] - Found leagues:', leagues.length);
|
||||
return leagues;
|
||||
} catch (error) {
|
||||
console.error('[LeagueService.getAllLeaguesByClub] - Error:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
static async getLeagueById(leagueId) {
|
||||
try {
|
||||
devLog('[LeagueService.getLeagueById] - Getting league:', leagueId);
|
||||
const league = await League.findByPk(leagueId, {
|
||||
include: [
|
||||
{
|
||||
model: Season,
|
||||
as: 'season',
|
||||
attributes: ['id', 'season']
|
||||
}
|
||||
]
|
||||
});
|
||||
devLog('[LeagueService.getLeagueById] - Found league:', league ? 'yes' : 'no');
|
||||
return league;
|
||||
} catch (error) {
|
||||
console.error('[LeagueService.getLeagueById] - Error:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
static async createLeague(leagueData) {
|
||||
try {
|
||||
devLog('[LeagueService.createLeague] - Creating league:', leagueData);
|
||||
|
||||
// Wenn keine Saison angegeben, verwende die aktuelle
|
||||
if (!leagueData.seasonId) {
|
||||
const currentSeason = await SeasonService.getOrCreateCurrentSeason();
|
||||
leagueData.seasonId = currentSeason.id;
|
||||
}
|
||||
|
||||
const league = await League.create(leagueData);
|
||||
devLog('[LeagueService.createLeague] - League created with ID:', league.id);
|
||||
return league;
|
||||
} catch (error) {
|
||||
console.error('[LeagueService.createLeague] - Error:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
static async updateLeague(leagueId, updateData) {
|
||||
try {
|
||||
devLog('[LeagueService.updateLeague] - Updating league:', leagueId, updateData);
|
||||
const [updatedRowsCount] = await League.update(updateData, {
|
||||
where: { id: leagueId }
|
||||
});
|
||||
devLog('[LeagueService.updateLeague] - Updated rows:', updatedRowsCount);
|
||||
return updatedRowsCount > 0;
|
||||
} catch (error) {
|
||||
console.error('[LeagueService.updateLeague] - Error:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
static async deleteLeague(leagueId) {
|
||||
try {
|
||||
devLog('[LeagueService.deleteLeague] - Deleting league:', leagueId);
|
||||
const deletedRowsCount = await League.destroy({
|
||||
where: { id: leagueId }
|
||||
});
|
||||
devLog('[LeagueService.deleteLeague] - Deleted rows:', deletedRowsCount);
|
||||
return deletedRowsCount > 0;
|
||||
} catch (error) {
|
||||
console.error('[LeagueService.deleteLeague] - Error:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default LeagueService;
|
||||
@@ -7,6 +7,7 @@ import Season from '../models/Season.js';
|
||||
import Location from '../models/Location.js';
|
||||
import League from '../models/League.js';
|
||||
import Team from '../models/Team.js';
|
||||
import SeasonService from './seasonService.js';
|
||||
import { checkAccess } from '../utils/userUtils.js';
|
||||
import { Op } from 'sequelize';
|
||||
|
||||
@@ -22,8 +23,7 @@ class MatchService {
|
||||
seasonStartYear = currentYear - 1;
|
||||
}
|
||||
const seasonEndYear = seasonStartYear + 1;
|
||||
const seasonEndYearString = seasonEndYear.toString().slice(-2);
|
||||
return `${seasonStartYear}/${seasonEndYearString}`;
|
||||
return `${seasonStartYear}/${seasonEndYear}`;
|
||||
}
|
||||
|
||||
async importCSV(userToken, clubId, filePath) {
|
||||
@@ -58,7 +58,6 @@ class MatchService {
|
||||
},
|
||||
});
|
||||
matches.push({
|
||||
seasonId: season.id,
|
||||
date: parsedDate,
|
||||
time: row['Termin'].split(' ')[1],
|
||||
homeTeamId: homeTeamId,
|
||||
@@ -72,7 +71,14 @@ class MatchService {
|
||||
if (seasonString) {
|
||||
season = await Season.findOne({ where: { season: seasonString } });
|
||||
if (season) {
|
||||
await Match.destroy({ where: { clubId, seasonId: season.id } });
|
||||
// Lösche alle Matches für Ligen dieser Saison
|
||||
const leagues = await League.findAll({
|
||||
where: { seasonId: season.id, clubId }
|
||||
});
|
||||
const leagueIds = leagues.map(league => league.id);
|
||||
if (leagueIds.length > 0) {
|
||||
await Match.destroy({ where: { clubId, leagueId: leagueIds } });
|
||||
}
|
||||
}
|
||||
}
|
||||
const result = await Match.bulkCreate(matches);
|
||||
@@ -99,33 +105,28 @@ class MatchService {
|
||||
}
|
||||
|
||||
|
||||
async getLeaguesForCurrentSeason(userToken, clubId) {
|
||||
async getLeaguesForCurrentSeason(userToken, clubId, seasonId = null) {
|
||||
await checkAccess(userToken, clubId);
|
||||
const seasonString = this.generateSeasonString();
|
||||
const season = await Season.findOne({
|
||||
where: {
|
||||
season: {
|
||||
[Op.like]: `%${seasonString}%`
|
||||
}
|
||||
|
||||
// Verwende SeasonService für korrekte Saison-Verwaltung
|
||||
let season;
|
||||
if (!seasonId) {
|
||||
season = await SeasonService.getOrCreateCurrentSeason();
|
||||
} else {
|
||||
season = await SeasonService.getSeasonById(seasonId);
|
||||
if (!season) {
|
||||
throw new Error('Season not found');
|
||||
}
|
||||
});
|
||||
if (!season) {
|
||||
await Season.create({ season: seasonString });
|
||||
throw new Error('Season not found');
|
||||
}
|
||||
|
||||
try {
|
||||
const leagues = await League.findAll({
|
||||
include: [{
|
||||
model: Match,
|
||||
as: 'leagueMatches',
|
||||
where: {
|
||||
seasonId: season.id,
|
||||
clubId: clubId
|
||||
},
|
||||
attributes: [],
|
||||
}],
|
||||
where: {
|
||||
clubId: clubId,
|
||||
seasonId: season.id
|
||||
},
|
||||
attributes: ['id', 'name'],
|
||||
group: ['League.id'],
|
||||
order: [['name', 'ASC']]
|
||||
});
|
||||
return leagues;
|
||||
} catch (error) {
|
||||
@@ -134,48 +135,66 @@ class MatchService {
|
||||
}
|
||||
}
|
||||
|
||||
async getMatchesForLeagues(userToken, clubId) {
|
||||
async getMatchesForLeagues(userToken, clubId, seasonId = null) {
|
||||
await checkAccess(userToken, clubId);
|
||||
const seasonString = this.generateSeasonString();
|
||||
const season = await Season.findOne({
|
||||
where: {
|
||||
season: {
|
||||
[Op.like]: `%${seasonString}%`
|
||||
}
|
||||
|
||||
// Wenn keine Saison angegeben, verwende die aktuelle
|
||||
let season;
|
||||
if (!seasonId) {
|
||||
season = await SeasonService.getOrCreateCurrentSeason();
|
||||
} else {
|
||||
season = await SeasonService.getSeasonById(seasonId);
|
||||
if (!season) {
|
||||
throw new Error('Season not found');
|
||||
}
|
||||
});
|
||||
if (!season) {
|
||||
throw new Error('Season not found');
|
||||
}
|
||||
const matches = await Match.findAll({
|
||||
where: {
|
||||
seasonId: season.id,
|
||||
clubId: clubId,
|
||||
},
|
||||
include: [
|
||||
{
|
||||
model: League,
|
||||
as: 'leagueDetails',
|
||||
attributes: ['name'],
|
||||
},
|
||||
{
|
||||
model: Team,
|
||||
as: 'homeTeam', // Assuming your associations are set correctly
|
||||
attributes: ['name'],
|
||||
},
|
||||
{
|
||||
model: Team,
|
||||
as: 'guestTeam',
|
||||
attributes: ['name'],
|
||||
},
|
||||
{
|
||||
model: Location,
|
||||
as: 'location',
|
||||
attributes: ['name', 'address', 'city', 'zip'],
|
||||
}
|
||||
]
|
||||
}
|
||||
});
|
||||
return matches;
|
||||
|
||||
// Filtere Matches nach Liga-Saison und lade Daten manuell
|
||||
const enrichedMatches = [];
|
||||
for (const match of matches) {
|
||||
// Lade Liga-Daten
|
||||
const league = await League.findByPk(match.leagueId, { attributes: ['name', 'seasonId'] });
|
||||
if (!league || league.seasonId !== season.id) {
|
||||
continue; // Skip matches from other seasons
|
||||
}
|
||||
|
||||
const enrichedMatch = {
|
||||
id: match.id,
|
||||
date: match.date,
|
||||
time: match.time,
|
||||
homeTeamId: match.homeTeamId,
|
||||
guestTeamId: match.guestTeamId,
|
||||
locationId: match.locationId,
|
||||
leagueId: match.leagueId,
|
||||
homeTeam: { name: 'Unbekannt' },
|
||||
guestTeam: { name: 'Unbekannt' },
|
||||
location: { name: 'Unbekannt', address: '', city: '', zip: '' },
|
||||
leagueDetails: { name: league.name }
|
||||
};
|
||||
|
||||
if (match.homeTeamId) {
|
||||
const homeTeam = await Team.findByPk(match.homeTeamId, { attributes: ['name'] });
|
||||
if (homeTeam) enrichedMatch.homeTeam = homeTeam;
|
||||
}
|
||||
if (match.guestTeamId) {
|
||||
const guestTeam = await Team.findByPk(match.guestTeamId, { attributes: ['name'] });
|
||||
if (guestTeam) enrichedMatch.guestTeam = guestTeam;
|
||||
}
|
||||
if (match.locationId) {
|
||||
const location = await Location.findByPk(match.locationId, {
|
||||
attributes: ['name', 'address', 'city', 'zip']
|
||||
});
|
||||
if (location) enrichedMatch.location = location;
|
||||
}
|
||||
|
||||
enrichedMatches.push(enrichedMatch);
|
||||
}
|
||||
return enrichedMatches;
|
||||
}
|
||||
|
||||
async getMatchesForLeague(userToken, clubId, leagueId) {
|
||||
@@ -193,34 +212,50 @@ class MatchService {
|
||||
}
|
||||
const matches = await Match.findAll({
|
||||
where: {
|
||||
seasonId: season.id,
|
||||
clubId: clubId,
|
||||
leagueId: leagueId
|
||||
},
|
||||
include: [
|
||||
{
|
||||
model: League,
|
||||
as: 'leagueDetails',
|
||||
attributes: ['name'],
|
||||
},
|
||||
{
|
||||
model: Team,
|
||||
as: 'homeTeam',
|
||||
attributes: ['name'],
|
||||
},
|
||||
{
|
||||
model: Team,
|
||||
as: 'guestTeam',
|
||||
attributes: ['name'],
|
||||
},
|
||||
{
|
||||
model: Location,
|
||||
as: 'location',
|
||||
attributes: ['name', 'address', 'city', 'zip'],
|
||||
}
|
||||
]
|
||||
}
|
||||
});
|
||||
return matches;
|
||||
|
||||
// Lade Team- und Location-Daten manuell
|
||||
const enrichedMatches = [];
|
||||
for (const match of matches) {
|
||||
const enrichedMatch = {
|
||||
id: match.id,
|
||||
date: match.date,
|
||||
time: match.time,
|
||||
homeTeamId: match.homeTeamId,
|
||||
guestTeamId: match.guestTeamId,
|
||||
locationId: match.locationId,
|
||||
leagueId: match.leagueId,
|
||||
homeTeam: { name: 'Unbekannt' },
|
||||
guestTeam: { name: 'Unbekannt' },
|
||||
location: { name: 'Unbekannt', address: '', city: '', zip: '' },
|
||||
leagueDetails: { name: 'Unbekannt' }
|
||||
};
|
||||
|
||||
if (match.homeTeamId) {
|
||||
const homeTeam = await Team.findByPk(match.homeTeamId, { attributes: ['name'] });
|
||||
if (homeTeam) enrichedMatch.homeTeam = homeTeam;
|
||||
}
|
||||
if (match.guestTeamId) {
|
||||
const guestTeam = await Team.findByPk(match.guestTeamId, { attributes: ['name'] });
|
||||
if (guestTeam) enrichedMatch.guestTeam = guestTeam;
|
||||
}
|
||||
if (match.locationId) {
|
||||
const location = await Location.findByPk(match.locationId, {
|
||||
attributes: ['name', 'address', 'city', 'zip']
|
||||
});
|
||||
if (location) enrichedMatch.location = location;
|
||||
}
|
||||
if (match.leagueId) {
|
||||
const league = await League.findByPk(match.leagueId, { attributes: ['name'] });
|
||||
if (league) enrichedMatch.leagueDetails = league;
|
||||
}
|
||||
|
||||
enrichedMatches.push(enrichedMatch);
|
||||
}
|
||||
return enrichedMatches;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
160
backend/services/seasonService.js
Normal file
160
backend/services/seasonService.js
Normal file
@@ -0,0 +1,160 @@
|
||||
import Season from '../models/Season.js';
|
||||
import { devLog } from '../utils/logger.js';
|
||||
|
||||
class SeasonService {
|
||||
/**
|
||||
* Ermittelt die aktuelle Saison basierend auf dem aktuellen Datum
|
||||
* @returns {string} Saison im Format "2023/2024"
|
||||
*/
|
||||
static getCurrentSeasonString() {
|
||||
const now = new Date();
|
||||
const currentYear = now.getFullYear();
|
||||
const currentMonth = now.getMonth() + 1; // getMonth() ist 0-basiert
|
||||
|
||||
// Ab 1. Juli: neue Saison beginnt
|
||||
if (currentMonth >= 7) {
|
||||
return `${currentYear}/${currentYear + 1}`;
|
||||
} else {
|
||||
return `${currentYear - 1}/${currentYear}`;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Holt oder erstellt die aktuelle Saison
|
||||
* @returns {Promise<Season>} Die aktuelle Saison
|
||||
*/
|
||||
static async getOrCreateCurrentSeason() {
|
||||
try {
|
||||
const currentSeasonString = this.getCurrentSeasonString();
|
||||
devLog('[SeasonService.getOrCreateCurrentSeason] - Current season string:', currentSeasonString);
|
||||
|
||||
// Versuche die aktuelle Saison zu finden
|
||||
let season = await Season.findOne({
|
||||
where: { season: currentSeasonString }
|
||||
});
|
||||
|
||||
// Falls nicht vorhanden, erstelle sie
|
||||
if (!season) {
|
||||
devLog('[SeasonService.getOrCreateCurrentSeason] - Creating new season:', currentSeasonString);
|
||||
season = await Season.create({
|
||||
season: currentSeasonString
|
||||
});
|
||||
}
|
||||
|
||||
devLog('[SeasonService.getOrCreateCurrentSeason] - Season found/created:', season.id);
|
||||
return season;
|
||||
} catch (error) {
|
||||
console.error('[SeasonService.getOrCreateCurrentSeason] - Error:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Holt alle verfügbaren Saisons
|
||||
* @returns {Promise<Array<Season>>} Alle Saisons sortiert nach Name
|
||||
*/
|
||||
static async getAllSeasons() {
|
||||
try {
|
||||
devLog('[SeasonService.getAllSeasons] - Getting all seasons');
|
||||
const seasons = await Season.findAll({
|
||||
order: [['season', 'DESC']] // Neueste zuerst
|
||||
});
|
||||
devLog('[SeasonService.getAllSeasons] - Found seasons:', seasons.length);
|
||||
return seasons;
|
||||
} catch (error) {
|
||||
console.error('[SeasonService.getAllSeasons] - Error:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Erstellt eine neue Saison
|
||||
* @param {string} seasonString - Saison im Format "2023/2024"
|
||||
* @returns {Promise<Season>} Die erstellte Saison
|
||||
*/
|
||||
static async createSeason(seasonString) {
|
||||
try {
|
||||
devLog('[SeasonService.createSeason] - Creating season:', seasonString);
|
||||
|
||||
// Prüfe ob Saison bereits existiert
|
||||
const existingSeason = await Season.findOne({
|
||||
where: { season: seasonString }
|
||||
});
|
||||
|
||||
if (existingSeason) {
|
||||
throw new Error('Season already exists');
|
||||
}
|
||||
|
||||
const season = await Season.create({
|
||||
season: seasonString
|
||||
});
|
||||
|
||||
devLog('[SeasonService.createSeason] - Season created with ID:', season.id);
|
||||
return season;
|
||||
} catch (error) {
|
||||
console.error('[SeasonService.createSeason] - Error:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Holt eine Saison nach ID
|
||||
* @param {number} seasonId - Die Saison-ID
|
||||
* @returns {Promise<Season|null>} Die Saison oder null
|
||||
*/
|
||||
static async getSeasonById(seasonId) {
|
||||
try {
|
||||
devLog('[SeasonService.getSeasonById] - Getting season:', seasonId);
|
||||
const season = await Season.findByPk(seasonId);
|
||||
devLog('[SeasonService.getSeasonById] - Found season:', season ? 'yes' : 'no');
|
||||
return season;
|
||||
} catch (error) {
|
||||
console.error('[SeasonService.getSeasonById] - Error:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Löscht eine Saison (nur wenn keine Teams/Ligen damit verknüpft sind)
|
||||
* @param {number} seasonId - Die Saison-ID
|
||||
* @returns {Promise<boolean>} True wenn gelöscht, false wenn nicht möglich
|
||||
*/
|
||||
static async deleteSeason(seasonId) {
|
||||
try {
|
||||
devLog('[SeasonService.deleteSeason] - Deleting season:', seasonId);
|
||||
|
||||
// Prüfe ob Saison verwendet wird
|
||||
const season = await Season.findByPk(seasonId, {
|
||||
include: [
|
||||
{ association: 'teams' },
|
||||
{ association: 'leagues' }
|
||||
]
|
||||
});
|
||||
|
||||
if (!season) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Prüfe ob Saison verwendet wird
|
||||
if (season.teams && season.teams.length > 0) {
|
||||
throw new Error('Season is used by teams');
|
||||
}
|
||||
|
||||
if (season.leagues && season.leagues.length > 0) {
|
||||
throw new Error('Season is used by leagues');
|
||||
}
|
||||
|
||||
await Season.destroy({
|
||||
where: { id: seasonId }
|
||||
});
|
||||
|
||||
devLog('[SeasonService.deleteSeason] - Season deleted');
|
||||
return true;
|
||||
} catch (error) {
|
||||
console.error('[SeasonService.deleteSeason] - Error:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default SeasonService;
|
||||
144
backend/services/teamService.js
Normal file
144
backend/services/teamService.js
Normal file
@@ -0,0 +1,144 @@
|
||||
import Team from '../models/Team.js';
|
||||
import League from '../models/League.js';
|
||||
import Club from '../models/Club.js';
|
||||
import Season from '../models/Season.js';
|
||||
import SeasonService from './seasonService.js';
|
||||
import { devLog } from '../utils/logger.js';
|
||||
|
||||
class TeamService {
|
||||
static async getAllTeamsByClub(clubId, seasonId = null) {
|
||||
try {
|
||||
devLog('[TeamService.getAllTeamsByClub] - Getting teams for club:', clubId, 'season:', seasonId);
|
||||
|
||||
// Wenn keine Saison angegeben, verwende die aktuelle
|
||||
if (!seasonId) {
|
||||
const currentSeason = await SeasonService.getOrCreateCurrentSeason();
|
||||
seasonId = currentSeason.id;
|
||||
}
|
||||
|
||||
const teams = await Team.findAll({
|
||||
where: { clubId, seasonId },
|
||||
include: [
|
||||
{
|
||||
model: League,
|
||||
as: 'league',
|
||||
attributes: ['id', 'name']
|
||||
},
|
||||
{
|
||||
model: Season,
|
||||
as: 'season',
|
||||
attributes: ['id', 'season']
|
||||
}
|
||||
],
|
||||
order: [['name', 'ASC']]
|
||||
});
|
||||
devLog('[TeamService.getAllTeamsByClub] - Found teams:', teams.length);
|
||||
return teams;
|
||||
} catch (error) {
|
||||
console.error('[TeamService.getAllTeamsByClub] - Error:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
static async getTeamById(teamId) {
|
||||
try {
|
||||
devLog('[TeamService.getTeamById] - Getting team:', teamId);
|
||||
const team = await Team.findByPk(teamId, {
|
||||
include: [
|
||||
{
|
||||
model: League,
|
||||
as: 'league',
|
||||
attributes: ['id', 'name']
|
||||
},
|
||||
{
|
||||
model: Club,
|
||||
as: 'club',
|
||||
attributes: ['id', 'name']
|
||||
},
|
||||
{
|
||||
model: Season,
|
||||
as: 'season',
|
||||
attributes: ['id', 'season']
|
||||
}
|
||||
]
|
||||
});
|
||||
devLog('[TeamService.getTeamById] - Found team:', team ? 'yes' : 'no');
|
||||
return team;
|
||||
} catch (error) {
|
||||
console.error('[TeamService.getTeamById] - Error:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
static async createTeam(teamData) {
|
||||
try {
|
||||
devLog('[TeamService.createTeam] - Creating team:', teamData);
|
||||
|
||||
// Wenn keine Saison angegeben, verwende die aktuelle
|
||||
if (!teamData.seasonId) {
|
||||
const currentSeason = await SeasonService.getOrCreateCurrentSeason();
|
||||
teamData.seasonId = currentSeason.id;
|
||||
}
|
||||
|
||||
const team = await Team.create(teamData);
|
||||
devLog('[TeamService.createTeam] - Team created with ID:', team.id);
|
||||
return team;
|
||||
} catch (error) {
|
||||
console.error('[TeamService.createTeam] - Error:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
static async updateTeam(teamId, updateData) {
|
||||
try {
|
||||
devLog('[TeamService.updateTeam] - Updating team:', teamId, updateData);
|
||||
const [updatedRowsCount] = await Team.update(updateData, {
|
||||
where: { id: teamId }
|
||||
});
|
||||
devLog('[TeamService.updateTeam] - Updated rows:', updatedRowsCount);
|
||||
return updatedRowsCount > 0;
|
||||
} catch (error) {
|
||||
console.error('[TeamService.updateTeam] - Error:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
static async deleteTeam(teamId) {
|
||||
try {
|
||||
devLog('[TeamService.deleteTeam] - Deleting team:', teamId);
|
||||
const deletedRowsCount = await Team.destroy({
|
||||
where: { id: teamId }
|
||||
});
|
||||
devLog('[TeamService.deleteTeam] - Deleted rows:', deletedRowsCount);
|
||||
return deletedRowsCount > 0;
|
||||
} catch (error) {
|
||||
console.error('[TeamService.deleteTeam] - Error:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
static async getLeaguesByClub(clubId, seasonId = null) {
|
||||
try {
|
||||
devLog('[TeamService.getLeaguesByClub] - Getting leagues for club:', clubId, 'season:', seasonId);
|
||||
|
||||
// Wenn keine Saison angegeben, verwende die aktuelle
|
||||
if (!seasonId) {
|
||||
const currentSeason = await SeasonService.getOrCreateCurrentSeason();
|
||||
seasonId = currentSeason.id;
|
||||
}
|
||||
|
||||
const leagues = await League.findAll({
|
||||
where: { clubId, seasonId },
|
||||
attributes: ['id', 'name', 'seasonId'],
|
||||
order: [['name', 'ASC']]
|
||||
});
|
||||
devLog('[TeamService.getLeaguesByClub] - Found leagues:', leagues.length);
|
||||
return leagues;
|
||||
} catch (error) {
|
||||
console.error('[TeamService.getLeaguesByClub] - Error:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default TeamService;
|
||||
Reference in New Issue
Block a user