mirror of
https://github.com/Sendouc/sendou.ink.git
synced 2026-09-14 15:16:09 -05:00
Add database table for user result highlights
This commit is contained in:
@@ -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"
|
||||
|
||||
@@ -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,
|
||||
|
||||
10
app/db/models/users/addResultHighlight.sql
Normal file
10
app/db/models/users/addResultHighlight.sql
Normal file
@@ -0,0 +1,10 @@
|
||||
insert into
|
||||
"UserResultHighlight" (
|
||||
"userId",
|
||||
"teamId"
|
||||
)
|
||||
values
|
||||
(
|
||||
@userId,
|
||||
@teamId
|
||||
)
|
||||
4
app/db/models/users/deleteAllResultHighlights.sql
Normal file
4
app/db/models/users/deleteAllResultHighlights.sql
Normal file
@@ -0,0 +1,4 @@
|
||||
delete from
|
||||
"UserResultHighlight"
|
||||
where
|
||||
"userId" = @userId
|
||||
@@ -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 });
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
25
migrations/011-user-result-highlights.js
Normal file
25
migrations/011-user-result-highlights.js
Normal 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();
|
||||
};
|
||||
Reference in New Issue
Block a user