diff --git a/desktop/server.js b/desktop/server.js index f3f9081..48fa87a 100644 --- a/desktop/server.js +++ b/desktop/server.js @@ -31,6 +31,7 @@ const userRoutes = require("./electron/api/user/user.routes"); const listRoutes = require("./electron/api/list/list.routes"); const anilistRoute = require("./electron/api/anilist/anilist"); const localRoutes = require("./electron/api/local/local.routes"); +const configRoutes = require("./electron/api/config/config.routes"); fastify.addHook("preHandler", async (request) => { const auth = request.headers.authorization; @@ -73,6 +74,7 @@ fastify.register(userRoutes, { prefix: "/api" }); fastify.register(anilistRoute, { prefix: "/api" }); fastify.register(listRoutes, { prefix: "/api" }); fastify.register(localRoutes, { prefix: "/api" }); +fastify.register(configRoutes, { prefix: "/api" }); const sleep = ms => new Promise(r => setTimeout(r, ms)); diff --git a/desktop/src/api/config/config.controller.ts b/desktop/src/api/config/config.controller.ts new file mode 100644 index 0000000..4788a2b --- /dev/null +++ b/desktop/src/api/config/config.controller.ts @@ -0,0 +1,43 @@ +import {FastifyReply, FastifyRequest} from 'fastify'; +import {getConfig, setConfig} from '../../shared/config'; + +export async function getFullConfig(req: FastifyRequest, reply: FastifyReply) { + try { + return getConfig(); + } catch (err) { + return { error: "Error loading config" }; + } +} + +export async function getConfigSection(req: FastifyRequest<{ Params: { section: string } }>, reply: FastifyReply) { + try { + const { section } = req.params; + const config = getConfig(); + + if (config[section] === undefined) { + return { error: "Section not found" }; + } + + return { [section]: config[section] }; + } catch (err) { + return { error: "Error loading config section" }; + } +} + +export async function updateConfig(req: FastifyRequest<{ Body: any }>, reply: FastifyReply) { + try { + return setConfig(req.body); + } catch (err) { + return { error: "Error updating config" }; + } +} + +export async function updateConfigSection(req: FastifyRequest<{ Params: { section: string }, Body: any }>, reply: FastifyReply) { + try { + const { section } = req.params; + const updatedConfig = setConfig({ [section]: req.body }); + return { [section]: updatedConfig[section] }; + } catch (err) { + return { error: "Error updating config section" }; + } +} \ No newline at end of file diff --git a/desktop/src/api/config/config.routes.ts b/desktop/src/api/config/config.routes.ts new file mode 100644 index 0000000..3be05e7 --- /dev/null +++ b/desktop/src/api/config/config.routes.ts @@ -0,0 +1,11 @@ +import { FastifyInstance } from 'fastify'; +import * as controller from './config.controller'; + +async function configRoutes(fastify: FastifyInstance) { + fastify.get('/config', controller.getFullConfig); + fastify.get('/config/:section', controller.getConfigSection); + fastify.post('/config', controller.updateConfig); + fastify.post('/config/:section', controller.updateConfigSection); +} + +export default configRoutes; \ No newline at end of file