From 78578d93bf5c58eecfd703f3b018e1fc2ae67366 Mon Sep 17 00:00:00 2001 From: Kalle <38327916+Sendouc@users.noreply.github.com> Date: Sun, 14 Aug 2022 13:41:05 +0300 Subject: [PATCH] PlusSuggestions db queries to sql files --- app/db/index.ts | 2 +- app/db/models/plusSuggestions/create.sql | 18 ++++ app/db/models/plusSuggestions/delete.sql | 4 + app/db/models/plusSuggestions/deleteAll.sql | 7 ++ .../deleteSuggestionWithComments.sql | 7 ++ .../plusSuggestions/findVisibleForUser.sql | 25 +++++ ...uggestions.server.ts => queries.server.ts} | 101 +++--------------- .../plusSuggestions/tiersSuggestedFor.sql | 15 +++ app/db/models/plusVotes/plusVotes.server.ts | 2 +- app/permissions.ts | 2 +- app/routes/plus/suggestions.tsx | 2 +- 11 files changed, 95 insertions(+), 90 deletions(-) create mode 100644 app/db/models/plusSuggestions/create.sql create mode 100644 app/db/models/plusSuggestions/delete.sql create mode 100644 app/db/models/plusSuggestions/deleteAll.sql create mode 100644 app/db/models/plusSuggestions/deleteSuggestionWithComments.sql create mode 100644 app/db/models/plusSuggestions/findVisibleForUser.sql rename app/db/models/plusSuggestions/{plusSuggestions.server.ts => queries.server.ts} (64%) create mode 100644 app/db/models/plusSuggestions/tiersSuggestedFor.sql diff --git a/app/db/index.ts b/app/db/index.ts index 4ca7d13c2..48ab43559 100644 --- a/app/db/index.ts +++ b/app/db/index.ts @@ -1,5 +1,5 @@ import * as users from "./models/users/users.server"; -import * as plusSuggestions from "./models/plusSuggestions/plusSuggestions.server"; +import * as plusSuggestions from "./models/plusSuggestions/queries.server"; import * as plusVotes from "./models/plusVotes/plusVotes.server"; import * as badges from "./models/badges/queries.server"; import * as calendarEvents from "./models/calendar/queries.server"; diff --git a/app/db/models/plusSuggestions/create.sql b/app/db/models/plusSuggestions/create.sql new file mode 100644 index 000000000..1e63f66db --- /dev/null +++ b/app/db/models/plusSuggestions/create.sql @@ -0,0 +1,18 @@ +insert into + "PlusSuggestion" ( + "text", + "authorId", + "suggestedId", + "month", + "year", + "tier" + ) +values + ( + @text, + @authorId, + @suggestedId, + @month, + @year, + @tier + ) \ No newline at end of file diff --git a/app/db/models/plusSuggestions/delete.sql b/app/db/models/plusSuggestions/delete.sql new file mode 100644 index 000000000..d7494039b --- /dev/null +++ b/app/db/models/plusSuggestions/delete.sql @@ -0,0 +1,4 @@ +delete from + "PlusSuggestion" +where + "id" = @id \ No newline at end of file diff --git a/app/db/models/plusSuggestions/deleteAll.sql b/app/db/models/plusSuggestions/deleteAll.sql new file mode 100644 index 000000000..0f35d8d60 --- /dev/null +++ b/app/db/models/plusSuggestions/deleteAll.sql @@ -0,0 +1,7 @@ +delete from + "PlusSuggestion" +where + "suggestedId" = @suggestedId + and "tier" = @tier + and "month" = @month + and "year" = @year \ No newline at end of file diff --git a/app/db/models/plusSuggestions/deleteSuggestionWithComments.sql b/app/db/models/plusSuggestions/deleteSuggestionWithComments.sql new file mode 100644 index 000000000..cef2867c6 --- /dev/null +++ b/app/db/models/plusSuggestions/deleteSuggestionWithComments.sql @@ -0,0 +1,7 @@ +delete from + "PlusSuggestion" +where + "month" = @month + and "year" = @year + and "suggestedId" = @suggestedId + and "tier" = @tier \ No newline at end of file diff --git a/app/db/models/plusSuggestions/findVisibleForUser.sql b/app/db/models/plusSuggestions/findVisibleForUser.sql new file mode 100644 index 000000000..889d74127 --- /dev/null +++ b/app/db/models/plusSuggestions/findVisibleForUser.sql @@ -0,0 +1,25 @@ +select + suggestion."id", + suggestion."createdAt", + suggestion."text", + suggestion."tier", + author."id" as "authorId", + author."discordId" as "authorDiscordId", + author."discordName" as "authorDiscordName", + author."discordDiscriminator" as "authorDiscordDiscriminator", + suggested."id" as "suggestedId", + suggested."discordId" as "suggestedDiscordId", + suggested."discordName" as "suggestedDiscordName", + suggested."discordDiscriminator" as "suggestedDiscordDiscriminator", + suggested."discordAvatar" as "suggestedDiscordAvatar", + suggested."bio" as "suggestedBio" +from + "PlusSuggestion" as suggestion + join "User" as author on suggestion."authorId" = author."id" + join "User" as suggested on suggestion."suggestedId" = suggested."id" +where + "month" = @month + and "year" = @year + and "tier" >= @plusTier +order by + suggestion."id" asc \ No newline at end of file diff --git a/app/db/models/plusSuggestions/plusSuggestions.server.ts b/app/db/models/plusSuggestions/queries.server.ts similarity index 64% rename from app/db/models/plusSuggestions/plusSuggestions.server.ts rename to app/db/models/plusSuggestions/queries.server.ts index 444303784..70a70ec6a 100644 --- a/app/db/models/plusSuggestions/plusSuggestions.server.ts +++ b/app/db/models/plusSuggestions/queries.server.ts @@ -6,27 +6,14 @@ import { databaseTimestampToDate } from "~/utils/dates"; import { sql } from "../../sql"; import type { PlusSuggestion, User, UserWithPlusTier } from "../../types"; -const createStm = sql.prepare(` - INSERT INTO - "PlusSuggestion" ( - "text", - "authorId", - "suggestedId", - "month", - "year", - "tier" - ) - VALUES - ( - $text, - $authorId, - $suggestedId, - $month, - $year, - $tier - ) -`); +import createSql from "./create.sql"; +import findVisibleForUserSql from "./findVisibleForUser.sql"; +import tiersSuggestedForSql from "./tiersSuggestedFor.sql"; +import deleteSql from "./delete.sql"; +import deleteAllSql from "./deleteAll.sql"; +import deleteSuggestionWithCommentsSql from "./deleteSuggestionWithComments.sql"; +const createStm = sql.prepare(createSql); export function create( args: Pick< PlusSuggestion, @@ -36,32 +23,7 @@ export function create( createStm.run(args); } -const findVisibleForUserStm = sql.prepare(` - SELECT - suggestion."id", - suggestion."createdAt", - suggestion."text", - suggestion."tier", - author."id" as "authorId", - author."discordId" as "authorDiscordId", - author."discordName" as "authorDiscordName", - author."discordDiscriminator" as "authorDiscordDiscriminator", - suggested."id" as "suggestedId", - suggested."discordId" as "suggestedDiscordId", - suggested."discordName" as "suggestedDiscordName", - suggested."discordDiscriminator" as "suggestedDiscordDiscriminator", - suggested."discordAvatar" as "suggestedDiscordAvatar", - suggested."bio" as "suggestedBio" - FROM "PlusSuggestion" as suggestion - JOIN "User" AS author ON suggestion."authorId" = author."id" - JOIN "User" AS suggested ON suggestion."suggestedId" = suggested."id" - WHERE - "month" = $month - AND "year" = $year - AND "tier" >= $plusTier - ORDER BY - suggestion."id" ASC -`); +const findVisibleForUserStm = sql.prepare(findVisibleForUserSql); export interface FindVisibleForUserSuggestedUserInfo { suggestedUser: Pick< @@ -162,58 +124,25 @@ function sortNewestPlayersToBeSuggestedFirst( ); } -const tiersSuggestedForStm = sql.prepare(` - SELECT - json_group_array(tier) - FROM - ( - SELECT - DISTINCT tier - FROM - "PlusSuggestion" - WHERE - "month" = $month - AND "year" = $year - AND "suggestedId" = $userId - ORDER BY tier ASC - ) -`); - +const tiersSuggestedForStm = sql.prepare(tiersSuggestedForSql); export function tiersSuggestedFor(args: MonthYear & { userId: User["id"] }) { return JSON.parse(tiersSuggestedForStm.pluck().get(args)) as User["id"][]; } -const delStm = sql.prepare(` - DELETE FROM "PlusSuggestion" - WHERE - "id" = $id -`); +const deleteStm = sql.prepare(deleteSql); export function del(id: PlusSuggestion["id"]) { - delStm.run({ id }); + deleteStm.run({ id }); } -const deleteAllStm = sql.prepare(` - DELETE FROM "PlusSuggestion" - WHERE - "suggestedId" = $suggestedId - AND tier = $tier - AND month = $month - AND year = $year -`); - +const deleteAllStm = sql.prepare(deleteAllSql); export function deleteAll(args: Pick) { deleteAllStm.run({ ...args, ...nextNonCompletedVoting(new Date()) }); } -const deleteSuggestionWithCommentsStm = sql.prepare(` - delete from "PlusSuggestion" - where "month" = $month - and "year" = $year - and "suggestedId" = $suggestedId - and "tier" = $tier -`); - +const deleteSuggestionWithCommentsStm = sql.prepare( + deleteSuggestionWithCommentsSql +); export function deleteSuggestionWithComments( args: Pick ) { diff --git a/app/db/models/plusSuggestions/tiersSuggestedFor.sql b/app/db/models/plusSuggestions/tiersSuggestedFor.sql new file mode 100644 index 000000000..daf7b220c --- /dev/null +++ b/app/db/models/plusSuggestions/tiersSuggestedFor.sql @@ -0,0 +1,15 @@ +select + json_group_array("tier") +from + ( + select + distinct "tier" + from + "PlusSuggestion" + where + "month" = @month + and "year" = @year + and "suggestedId" = @userId + order by + "tier" asc + ) \ No newline at end of file diff --git a/app/db/models/plusVotes/plusVotes.server.ts b/app/db/models/plusVotes/plusVotes.server.ts index 098b597c5..740022599 100644 --- a/app/db/models/plusVotes/plusVotes.server.ts +++ b/app/db/models/plusVotes/plusVotes.server.ts @@ -13,7 +13,7 @@ import type { User, UserWithPlusTier, } from "../../types"; -import type { FindVisibleForUserSuggestedUserInfo } from "../plusSuggestions/plusSuggestions.server"; +import type { FindVisibleForUserSuggestedUserInfo } from "../plusSuggestions/queries.server"; const createStm = sql.prepare(` INSERT INTO diff --git a/app/permissions.ts b/app/permissions.ts index 7863aa7f7..68dee22f9 100644 --- a/app/permissions.ts +++ b/app/permissions.ts @@ -1,4 +1,4 @@ -import type * as plusSuggestions from "~/db/models/plusSuggestions/plusSuggestions.server"; +import type * as plusSuggestions from "~/db/models/plusSuggestions/queries.server"; import { monthsVotingRange } from "./modules/plus-server"; import type { CalendarEvent, diff --git a/app/routes/plus/suggestions.tsx b/app/routes/plus/suggestions.tsx index d9c270821..229f8c33d 100644 --- a/app/routes/plus/suggestions.tsx +++ b/app/routes/plus/suggestions.tsx @@ -16,7 +16,7 @@ import { FormWithConfirm } from "~/components/FormWithConfirm"; import { TrashIcon } from "~/components/icons/Trash"; import { nextNonCompletedVoting } from "~/modules/plus-server"; import { db } from "~/db"; -import type * as plusSuggestions from "~/db/models/plusSuggestions/plusSuggestions.server"; +import type * as plusSuggestions from "~/db/models/plusSuggestions/queries.server"; import type { PlusSuggestion, User } from "~/db/types"; import { getUser, requireUser, useUser } from "~/modules/auth"; import {