better brackets, info types
This commit is contained in:
4
src/lib/pocketbase/services/base.ts
Normal file
4
src/lib/pocketbase/services/base.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
export interface DataFetchOptions {
|
||||
includeRelations?: boolean;
|
||||
expand?: string;
|
||||
}
|
||||
39
src/lib/pocketbase/services/matches.ts
Normal file
39
src/lib/pocketbase/services/matches.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import { logger } from "@/lib/logger";
|
||||
import type { Match, MatchInput } from "@/features/matches/types";
|
||||
import type PocketBase from "pocketbase";
|
||||
|
||||
export function createMatchesService(pb: PocketBase) {
|
||||
return {
|
||||
async createMatch(data: MatchInput): Promise<Match> {
|
||||
logger.info("PocketBase | Creating match", data);
|
||||
const result = await pb.collection("matches").create<Match>(data);
|
||||
return result;
|
||||
},
|
||||
|
||||
async createMatches(matches: MatchInput[]): Promise<Match[]> {
|
||||
logger.info("PocketBase | Creating multiple matches", { count: matches.length });
|
||||
const results = await Promise.all(
|
||||
matches.map(match => pb.collection("matches").create<Match>(match))
|
||||
);
|
||||
return results;
|
||||
},
|
||||
|
||||
async updateMatch(id: string, data: Partial<MatchInput>): Promise<Match> {
|
||||
logger.info("PocketBase | Updating match", { id, data });
|
||||
const result = await pb.collection("matches").update<Match>(id, data);
|
||||
return result;
|
||||
},
|
||||
|
||||
async deleteMatchesByTournament(tournamentId: string): Promise<void> {
|
||||
logger.info("PocketBase | Deleting matches for tournament", tournamentId);
|
||||
const matches = await pb.collection("matches").getFullList({
|
||||
filter: `tournament = "${tournamentId}"`,
|
||||
fields: "id",
|
||||
});
|
||||
|
||||
await Promise.all(
|
||||
matches.map(match => pb.collection("matches").delete(match.id))
|
||||
);
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -1,13 +1,28 @@
|
||||
import type {
|
||||
Player,
|
||||
PlayerInfo,
|
||||
PlayerInput,
|
||||
PlayerUpdateInput,
|
||||
} from "@/features/players/types";
|
||||
import { transformPlayer } from "@/lib/pocketbase/util/transform-types";
|
||||
import { transformPlayer, transformPlayerInfo } from "@/lib/pocketbase/util/transform-types";
|
||||
import PocketBase from "pocketbase";
|
||||
import { DataFetchOptions } from "./base";
|
||||
|
||||
export function createPlayersService(pb: PocketBase) {
|
||||
return {
|
||||
async getPlayerInfo(id: string): Promise<PlayerInfo> {
|
||||
const result = await pb.collection("players").getOne(id, {
|
||||
fields: "id,first_name,last_name"
|
||||
});
|
||||
return transformPlayerInfo(result);
|
||||
},
|
||||
|
||||
async listPlayerInfos(): Promise<PlayerInfo[]> {
|
||||
const result = await pb.collection("players").getFullList({
|
||||
fields: "id,first_name,last_name",
|
||||
});
|
||||
return result.map(transformPlayerInfo);
|
||||
},
|
||||
async getPlayerByAuthId(authId: string): Promise<Player | null> {
|
||||
const result = await pb.collection("players").getList<Player>(1, 1, {
|
||||
filter: `auth_id = "${authId}"`,
|
||||
|
||||
@@ -1,10 +1,26 @@
|
||||
import { logger } from "@/lib/logger";
|
||||
import PocketBase from "pocketbase";
|
||||
import { transformTeam } from "@/lib/pocketbase/util/transform-types";
|
||||
import { Team } from "@/features/teams/types";
|
||||
import { transformTeam, transformTeamInfo } from "@/lib/pocketbase/util/transform-types";
|
||||
import { Team, TeamInfo } from "@/features/teams/types";
|
||||
import { DataFetchOptions } from "./base";
|
||||
|
||||
export function createTeamsService(pb: PocketBase) {
|
||||
return {
|
||||
async getTeamInfo(id: string): Promise<TeamInfo> {
|
||||
logger.info("PocketBase | Getting team info", id);
|
||||
const result = await pb.collection("teams").getOne(id, {
|
||||
fields: "id,name,primary_color,accent_color"
|
||||
});
|
||||
return transformTeamInfo(result);
|
||||
},
|
||||
|
||||
async listTeamInfos(): Promise<TeamInfo[]> {
|
||||
logger.info("PocketBase | Listing team infos");
|
||||
const result = await pb.collection("teams").getFullList({
|
||||
fields: "id,name,primary_color,accent_color"
|
||||
});
|
||||
return result.map(transformTeamInfo);
|
||||
},
|
||||
async getTeam(id: string): Promise<Team | null> {
|
||||
logger.info("PocketBase | Getting team", id);
|
||||
const result = await pb.collection("teams").getOne(id, {
|
||||
|
||||
@@ -1,32 +1,32 @@
|
||||
import { logger } from "@/lib/logger";
|
||||
import type {
|
||||
Tournament,
|
||||
TournamentInfo,
|
||||
TournamentInput,
|
||||
TournamentUpdateInput,
|
||||
} from "@/features/tournaments/types";
|
||||
import type { Team } from "@/features/teams/types";
|
||||
import PocketBase from "pocketbase";
|
||||
import { transformTournament } from "@/lib/pocketbase/util/transform-types";
|
||||
import { transformTournament, transformTournamentInfo } from "@/lib/pocketbase/util/transform-types";
|
||||
import { transformTeam } from "@/lib/pocketbase/util/transform-types";
|
||||
|
||||
export function createTournamentsService(pb: PocketBase) {
|
||||
return {
|
||||
async getTournament(id: string): Promise<Tournament | null> {
|
||||
logger.info("PocketBase | Getting tournament", id);
|
||||
async getTournament(id: string): Promise<Tournament> {
|
||||
const result = await pb.collection("tournaments").getOne(id, {
|
||||
expand: "teams, teams.players",
|
||||
expand: "teams, teams.players, matches, matches.tournament",
|
||||
});
|
||||
return transformTournament(result);
|
||||
},
|
||||
async listTournaments(): Promise<Tournament[]> {
|
||||
async listTournaments(): Promise<TournamentInfo[]> {
|
||||
const result = await pb
|
||||
.collection("tournaments")
|
||||
.getFullList<Tournament>({
|
||||
fields: "id,name,start_time,end_time,logo,created",
|
||||
.getFullList({
|
||||
fields: "id,name,location,start_time,end_time,logo",
|
||||
sort: "-created",
|
||||
});
|
||||
|
||||
return result.map(transformTournament);
|
||||
return result.map(transformTournamentInfo);
|
||||
},
|
||||
async createTournament(data: TournamentInput): Promise<Tournament> {
|
||||
const result = await pb
|
||||
@@ -79,6 +79,18 @@ export function createTournamentsService(pb: PocketBase) {
|
||||
|
||||
return transformTournament(result);
|
||||
},
|
||||
async updateTournamentMatches(
|
||||
tournamentId: string,
|
||||
matchIds: string[]
|
||||
): Promise<Tournament> {
|
||||
logger.info("PocketBase | Updating tournament matches", { tournamentId, matchCount: matchIds.length });
|
||||
const result = await pb
|
||||
.collection("tournaments")
|
||||
.update<Tournament>(tournamentId, {
|
||||
matches: matchIds
|
||||
});
|
||||
return transformTournament(result);
|
||||
},
|
||||
async getUnenrolledTeams(tournamentId: string): Promise<Team[]> {
|
||||
try {
|
||||
logger.info(
|
||||
|
||||
Reference in New Issue
Block a user