Skip to content

Commit 0665f4c

Browse files
feat: GuildSeason table. Everything else is WIP
1 parent e682b66 commit 0665f4c

8 files changed

Lines changed: 61 additions & 14 deletions

File tree

apps/backend/src/db/init_tables.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { knexDb } from "@db/knexfile";
22
import { getLogger } from "@logtape/logtape";
3+
import { GuildSeasonTable } from "@v1/guild/models";
34
import {
45
MatchCharacterTable,
56
MatchPlayerTable,
@@ -8,7 +9,6 @@ import {
89
} from "@v1/match/models";
910
import { MatchReportView, MatchWinnerView } from "@v1/match/views";
1011
import { SeasonTable } from "@v1/season/models";
11-
import { Season } from "@v1/season/schemas";
1212
import type { Knex } from "knex";
1313

1414
const log = getLogger(["grindcord", "db"]);
@@ -19,6 +19,7 @@ const tables = [
1919
MatchCharacterTable,
2020
SSBUCharTable,
2121
SeasonTable,
22+
GuildSeasonTable,
2223
];
2324
const views = [MatchWinnerView, MatchReportView];
2425

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import type { Knex } from "knex";
2+
import { z } from "zod";
3+
4+
export const GuildSeasonRecord = z.object({
5+
guild_id: z.string(),
6+
season_id: z.int(),
7+
});
8+
export type GuildSeasonRecord = z.infer<typeof GuildSeasonRecord>;
9+
export const GuildSeasonTable = {
10+
table_name: "GuildSeason",
11+
initialize(table: Knex.TableBuilder) {
12+
table.primary(["guild_id"]);
13+
14+
table.string("guild_id");
15+
table.integer("season_id");
16+
17+
table
18+
.foreign("season_id")
19+
.references("season_id")
20+
.inTable("Season")
21+
.onUpdate("CASCADE")
22+
.onDelete("RESTRICT");
23+
},
24+
};
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { SeasonId } from "@v1/season/schemas";
2+
import { Hono } from "hono";
3+
import { describeRoute, resolver, validator } from "hono-openapi";
4+
import { z } from "zod";
5+
6+
const app = new Hono();
7+
8+
app.get(
9+
"/season/:season_id",
10+
describeRoute({
11+
description: "Get the guild's currently active season",
12+
responses: {
13+
200: {
14+
description: "Successful response",
15+
content: {
16+
"application/json": {
17+
// schema: resolver(UserPoints),
18+
},
19+
},
20+
},
21+
},
22+
}),
23+
validator("param", SeasonId),
24+
async (c) => {
25+
const { season_id } = c.req.valid("param");
26+
// return c.json({ points: await getUserPoints( user_id) });
27+
},
28+
);
29+
// Get a guild's leaderboard
30+
// app.get("/:guild_id", c);
31+
32+
export default app;

apps/backend/src/v1/season/models.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { z } from "zod";
33

44
export const SeasonRecord = z.object({
55
season_id: z.int(),
6-
guild_id: z.string(),
76
season_name: z.string(),
87
start_at: z.iso.datetime(),
98
end_at: z.iso.datetime(),
@@ -12,10 +11,9 @@ export type SeasonRecord = z.infer<typeof SeasonRecord>;
1211
export const SeasonTable = {
1312
table_name: "Season",
1413
initialize(table: Knex.TableBuilder) {
15-
table.primary(["season_id", "guild_id"]);
14+
table.primary(["season_id"]);
1615

1716
table.increments("season_id");
18-
table.string("guild_id");
1917

2018
table.string("season_name");
2119
table.dateTime("start_at");

apps/backend/src/v1/season/schemas.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { z } from "zod";
22

33
export const Season = z.object({
44
season_id: z.int(),
5-
guild_id: z.string(),
65
season_name: z.string(),
76
start_at: z.iso.datetime(),
87
end_at: z.iso.datetime(),
@@ -21,7 +20,6 @@ export type SeasonId = z.infer<typeof SeasonId>;
2120

2221
export const SeasonQuery = z
2322
.object({
24-
guild_id: z.string(),
2523
season_name: z.string(),
2624
after: z.iso.datetime(),
2725
before: z.iso.datetime(),

apps/backend/src/v1/season/service.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,9 @@ export async function getSeasonIds(
1212
season_query: SeasonQuery,
1313
db: Knex = knexDb,
1414
): Promise<Array<number>> {
15-
const { guild_id, season_name, after, before } = season_query;
15+
const { season_name, after, before } = season_query;
1616
const query = db<SeasonRecord>("Season").select("season_id");
17-
if (guild_id) {
18-
query.where({ guild_id });
19-
}
17+
2018
if (season_name) {
2119
// https://github.com/knex/knex/issues/5920
2220
query.whereLike("season_name", `%${season_name}%`);

apps/backend/src/v1/season/test/models.factories.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
import { faker } from "@faker-js/faker";
2-
import { snowflake } from "@test/factories";
32
import type { SeasonRecord } from "@v1/season/models";
43
import type { Knex } from "knex";
54

65
export const seasonRecordFactory = (
76
season_record: Partial<SeasonRecord> | undefined = undefined,
87
): Omit<SeasonRecord, "season_id"> => {
98
return {
10-
guild_id: snowflake(),
119
season_name: faker.animal.fish(),
1210
start_at: faker.date.recent().toISOString(),
1311
end_at: faker.date.soon().toISOString(),

apps/backend/src/v1/season/test/schemas.factory.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
import { faker } from "@faker-js/faker";
2-
import { snowflake } from "@test/factories";
32
import type { SeasonCreate } from "@v1/season/schemas";
43

54
export const seasonCreateFactory = (
65
season_create: SeasonCreate | undefined = undefined,
76
): SeasonCreate => {
87
return {
9-
guild_id: snowflake(),
108
season_name: faker.animal.fish(),
119
start_at: faker.date.recent().toISOString(),
1210
end_at: faker.date.soon().toISOString(),

0 commit comments

Comments
 (0)