Skip to content

Commit

Permalink
feat: add settings table
Browse files Browse the repository at this point in the history
  • Loading branch information
oae committed Nov 3, 2022
1 parent f8eb9c6 commit 5cb4457
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"express": "^4.18.2",
"framer-motion": "^7.6.1",
"mantine-datatable": "^1.7.9",
"nanoid": "v3",
"next": "12.3.1",
"node-telegram-bot-api": "^0.59.0",
"pino": "^8.6.1",
Expand Down
2 changes: 2 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 47 additions & 0 deletions prisma/migrations/20221103223653_add_settings_schema/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
-- CreateTable
CREATE TABLE "Settings" (
"id" SERIAL NOT NULL,
"telegramEnabled" BOOLEAN NOT NULL DEFAULT false,
"telegramToken" TEXT,
"telegramChatId" TEXT,
"telegramSendSilently" BOOLEAN NOT NULL DEFAULT false,
"appriseEnabled" BOOLEAN NOT NULL DEFAULT false,
"appriseHost" TEXT,
"appriseUrls" TEXT [] DEFAULT ARRAY [] :: TEXT [],
"komgaEnabled" BOOLEAN NOT NULL DEFAULT false,
"komgaHost" TEXT,
"komgaUser" TEXT,
"komgaPassword" TEXT,
CONSTRAINT "Settings_pkey" PRIMARY KEY ("id")
);

INSERT INTO
"Settings" (
id,
"telegramEnabled",
"telegramToken",
"telegramChatId",
"telegramSendSilently",
"appriseEnabled",
"appriseHost",
"appriseUrls",
"komgaEnabled",
"komgaHost",
"komgaUser",
"komgaPassword"
)
VALUES
(
1,
false,
null,
null,
false,
false,
null,
'{}',
false,
null,
null,
null
);
15 changes: 15 additions & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,18 @@ model Metadata {
synonyms String[] @default([])
urls String[] @default([])
}

model Settings {
id Int @id @default(autoincrement())
telegramEnabled Boolean @default(false)
telegramToken String?
telegramChatId String?
telegramSendSilently Boolean @default(false)
appriseEnabled Boolean @default(false)
appriseHost String?
appriseUrls String[] @default([])
komgaEnabled Boolean @default(false)
komgaHost String?
komgaUser String?
komgaPassword String?
}
2 changes: 2 additions & 0 deletions src/server/trpc/router/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import { t } from '../trpc';

import { libraryRouter } from './library';
import { mangaRouter } from './manga';
import { settingsRouter } from './settings';

export const appRouter = t.router({
library: libraryRouter,
manga: mangaRouter,
settings: settingsRouter,
});

// export type definition of API
Expand Down
38 changes: 38 additions & 0 deletions src/server/trpc/router/settings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { z } from 'zod';
import { getMangalConfig, setMangalConfig } from '../../utils/mangal';
import { t } from '../trpc';

export const settingsRouter = t.router({
query: t.procedure.query(async ({ ctx }) => {
const mangalConfig = (await getMangalConfig()).sort((a, b) => a.key.localeCompare(b.key));
const appConfig = await ctx.prisma.settings.findFirstOrThrow();
return {
mangalConfig,
appConfig,
};
}),
update: t.procedure
.input(
z.object({
key: z.string().min(1),
value: z.string().or(z.boolean()).or(z.number()).or(z.string().array()),
updateType: z.literal('mangal').or(z.literal('app')),
}),
)
.mutation(async ({ ctx, input }) => {
const { key, value, updateType } = input;
if (updateType === 'mangal') {
await setMangalConfig(key, value);
} else if (updateType === 'app') {
const appConfig = await ctx.prisma.settings.findFirstOrThrow();
await ctx.prisma.settings.update({
where: {
id: appConfig.id,
},
data: {
[key]: value,
},
});
}
}),
});

0 comments on commit 5cb4457

Please sign in to comment.