Add database table for user result highlights

This commit is contained in:
Remmy Cat Stock
2022-10-17 13:15:55 +02:00
parent 521ab1d348
commit 3bc75f58d3
6 changed files with 73 additions and 2 deletions

View File

@@ -12,7 +12,16 @@ select
"CalendarEventDate"
where
"eventId" = "CalendarEvent"."id"
) as "startTime"
) as "startTime",
exists (
select
1
from
"UserResultHighlight"
where
"userId" = @userId and
"teamId" = "CalendarEventResultTeam"."id"
) as "isHighlight"
from
"CalendarEventResultPlayer"
join "CalendarEventResultTeam" on "CalendarEventResultTeam"."id" = "CalendarEventResultPlayer"."teamId"

View File

@@ -244,9 +244,11 @@ export function findResultsByUserId(userId: User["id"]) {
placement: CalendarEventResultTeam["placement"];
participantCount: CalendarEvent["participantCount"];
startTime: CalendarEventDate["startTime"];
isHighlight: number;
}>
).map((row) => ({
...row,
isHighlight: Boolean(row.isHighlight),
mates: (
findMatesByResultTeamIdStm.all({
teamId: row.teamId,

View File

@@ -0,0 +1,10 @@
insert into
"UserResultHighlight" (
"userId",
"teamId"
)
values
(
@userId,
@teamId
)

View File

@@ -0,0 +1,4 @@
delete from
"UserResultHighlight"
where
"userId" = @userId

View File

@@ -1,5 +1,9 @@
import { sql } from "../../sql";
import type { User, UserWithPlusTier } from "../../types";
import type {
CalendarEventResultTeam,
User,
UserWithPlusTier,
} from "../../types";
import upsertSql from "./upsert.sql";
import updateProfileSql from "./updateProfile.sql";
@@ -12,6 +16,8 @@ import updateDiscordIdSql from "./updateDiscordId.sql";
import findByIdentifierSql from "./findByIdentifier.sql";
import findAllPlusMembersSql from "./findAllPlusMembers.sql";
import findAllPatronsSql from "./findAllPatrons.sql";
import addResultHighlightSql from "./addResultHighlight.sql";
import deleteAllResultHighlightsSql from "./deleteAllResultHighlights.sql";
const upsertStm = sql.prepare(upsertSql);
export function upsert(
@@ -123,3 +129,18 @@ export type FindAllPatrons = Array<
export function findAllPatrons() {
return findAllPatronsStm.all() as FindAllPatrons;
}
const deleteAllResultHighlightsStm = sql.prepare(deleteAllResultHighlightsSql);
const addResultHighlightStm = sql.prepare(addResultHighlightSql);
export type UpdateResultHighlightsArgs = {
userId: User["id"];
resultTeamIds: Array<CalendarEventResultTeam["id"]>;
};
export const updateResultHighlights = sql.transaction(
({ userId, resultTeamIds }: UpdateResultHighlightsArgs) => {
deleteAllResultHighlightsStm.run({ userId });
for (const teamId of resultTeamIds) {
addResultHighlightStm.run({ userId, teamId });
}
}
);

View File

@@ -0,0 +1,25 @@
module.exports.up = function (db) {
db.prepare(
`
create table "UserResultHighlight" (
"teamId" integer not null,
"userId" integer not null,
foreign key ("teamId") references "CalendarEventResultTeam"("id") on delete cascade,
foreign key ("userId") references "User"("id") on delete cascade,
unique("teamId", "userId") on conflict rollback
) strict
`
).run();
db.prepare(
`create index user_result_highlight_user_id on "UserResultHighlight"("userId")`
).run();
db.prepare(
`create index user_result_highlight_team_id on "UserResultHighlight"("teamId")`
).run();
};
module.exports.down = function (db) {
db.prepare(`drop table "UserResultHighlight"`).run();
};