Files
peakskills/services/teams-service.ts
2025-08-21 09:44:29 +02:00

172 lines
3.9 KiB
TypeScript

import { getPool } from "./database";
import { Team } from "@/lib/types";
export class TeamsService {
/**
* Get all teams
*/
static async getTeams(): Promise<Team[]> {
const pool = getPool();
const query = `
SELECT id, name, direction
FROM teams
ORDER BY direction, name
`;
try {
const result = await pool.query(query);
return result.rows;
} catch (error) {
console.error("Error fetching teams:", error);
throw new Error("Failed to fetch teams");
}
}
/**
* Get teams by direction
*/
static async getTeamsByDirection(direction: string): Promise<Team[]> {
const pool = getPool();
const query = `
SELECT id, name, direction
FROM teams
WHERE direction = $1
ORDER BY name
`;
try {
const result = await pool.query(query, [direction]);
return result.rows;
} catch (error) {
console.error("Error fetching teams by direction:", error);
throw new Error("Failed to fetch teams by direction");
}
}
/**
* Get team by ID
*/
static async getTeamById(id: string): Promise<Team | null> {
const pool = getPool();
const query = `
SELECT id, name, direction
FROM teams
WHERE id = $1
`;
try {
const result = await pool.query(query, [id]);
return result.rows[0] || null;
} catch (error) {
console.error("Error fetching team by ID:", error);
throw new Error("Failed to fetch team");
}
}
/**
* Create a new team
*/
static async createTeam(
team: Omit<Team, "created_at" | "updated_at">
): Promise<Team> {
const pool = getPool();
const query = `
INSERT INTO teams (id, name, direction)
VALUES ($1, $2, $3)
RETURNING id, name, direction
`;
try {
const result = await pool.query(query, [
team.id,
team.name,
team.direction,
]);
return result.rows[0];
} catch (error) {
console.error("Error creating team:", error);
throw new Error("Failed to create team");
}
}
/**
* Update a team
*/
static async updateTeam(
id: string,
updates: Partial<Omit<Team, "id">>
): Promise<Team | null> {
const pool = getPool();
const fields = [];
const values = [];
let paramIndex = 1;
if (updates.name !== undefined) {
fields.push(`name = $${paramIndex++}`);
values.push(updates.name);
}
if (updates.direction !== undefined) {
fields.push(`direction = $${paramIndex++}`);
values.push(updates.direction);
}
if (fields.length === 0) {
return this.getTeamById(id);
}
values.push(id);
const query = `
UPDATE teams
SET ${fields.join(", ")}
WHERE id = $${paramIndex}
RETURNING id, name, direction
`;
try {
const result = await pool.query(query, values);
return result.rows[0] || null;
} catch (error) {
console.error("Error updating team:", error);
throw new Error("Failed to update team");
}
}
/**
* Delete a team
*/
static async deleteTeam(id: string): Promise<boolean> {
const pool = getPool();
const query = `DELETE FROM teams WHERE id = $1`;
try {
const result = await pool.query(query, [id]);
return (result.rowCount ?? 0) > 0;
} catch (error) {
console.error("Error deleting team:", error);
throw new Error("Failed to delete team");
}
}
/**
* Get all directions
*/
static async getDirections(): Promise<string[]> {
const pool = getPool();
const query = `
SELECT DISTINCT direction
FROM teams
ORDER BY direction
`;
try {
const result = await pool.query(query);
return result.rows.map((row) => row.direction);
} catch (error) {
console.error("Error fetching directions:", error);
throw new Error("Failed to fetch directions");
}
}
}