From 1c28a4025884e1ea51bfd07e44c442d629b6c6c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C4=81nis?= <97699850+janisslsm@users.noreply.github.com> Date: Thu, 4 Apr 2024 01:55:51 +0300 Subject: [PATCH] feat: theme saving (#137) --- src/controllers/api/updateThemeController.ts | 25 ++++++++++++++++++++ src/routes/api.ts | 2 ++ src/services/inventoryService.ts | 11 ++++++++- src/types/requestTypes.ts | 6 +++++ 4 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 src/controllers/api/updateThemeController.ts diff --git a/src/controllers/api/updateThemeController.ts b/src/controllers/api/updateThemeController.ts new file mode 100644 index 00000000..5da05cb3 --- /dev/null +++ b/src/controllers/api/updateThemeController.ts @@ -0,0 +1,25 @@ +import { getJSONfromString } from "@/src/helpers/stringHelpers"; +import { updateTheme } from "@/src/services/inventoryService"; +import { IThemeUpdateRequest } from "@/src/types/requestTypes"; +import { RequestHandler } from "express"; + +// eslint-disable-next-line @typescript-eslint/no-misused-promises +const updateThemeController: RequestHandler = async (request, response) => { + const accountId = request.query.accountId as string; + const body = String(request.body); + + try { + const json = getJSONfromString(body) as IThemeUpdateRequest; + if (typeof json !== "object") { + throw new Error("Invalid data format"); + } + + await updateTheme(json, accountId); + } catch (err) { + console.error("Error parsing JSON data:", err); + } + + response.json({}); +}; + +export { updateThemeController }; diff --git a/src/routes/api.ts b/src/routes/api.ts index 71648809..1d550502 100644 --- a/src/routes/api.ts +++ b/src/routes/api.ts @@ -40,6 +40,7 @@ import { claimCompletedRecipeController } from "@/src/controllers/api/claimCompl import { shipDecorationsController } from "@/src/controllers/api/shipDecorationsController"; import { setShipCustomizationsController } from "@/src/controllers/api/setShipCustomizationsController"; import { setActiveShipController } from "@/src/controllers/api/setActiveShipController"; +import { updateThemeController } from "../controllers/api/updateThemeController"; const apiRouter = express.Router(); @@ -90,5 +91,6 @@ apiRouter.post("/rerollRandomMod.php", rerollRandomModController); apiRouter.post("/joinSession.php", joinSessionController); apiRouter.post("/saveLoadout.php", saveLoadoutController); apiRouter.post("/trainingResult.php", trainingResultController); +apiRouter.post("/updateTheme.php", updateThemeController); export { apiRouter }; diff --git a/src/services/inventoryService.ts b/src/services/inventoryService.ts index cc7fd350..8dd086fa 100644 --- a/src/services/inventoryService.ts +++ b/src/services/inventoryService.ts @@ -15,7 +15,7 @@ import { IRawUpgrade } from "@/src/types/inventoryTypes/inventoryTypes"; import { IGenericUpdate } from "../types/genericUpdate"; -import { IArtifactsRequest, IMissionInventoryUpdateRequest } from "../types/requestTypes"; +import { IArtifactsRequest, IMissionInventoryUpdateRequest, IThemeUpdateRequest } from "../types/requestTypes"; import { logger } from "@/src/utils/logger"; import { WeaponTypeInternal } from "@/src/services/itemDataService"; @@ -150,6 +150,15 @@ export const updateGeneric = async (data: IGenericUpdate, accountId: string) => return data; }; +export const updateTheme = async (data: IThemeUpdateRequest, accountId: string) => { + const inventory = await getInventory(accountId); + if (data.Style) inventory.ThemeStyle = data.Style; + if (data.Background) inventory.ThemeBackground = data.Background; + if (data.Sounds) inventory.ThemeSounds = data.Sounds; + + await inventory.save(); +}; + export const addWeapon = async ( weaponType: WeaponTypeInternal, weaponName: string, diff --git a/src/types/requestTypes.ts b/src/types/requestTypes.ts index e4f94980..1be732a6 100644 --- a/src/types/requestTypes.ts +++ b/src/types/requestTypes.ts @@ -17,6 +17,12 @@ export interface IArtifactsRequest { FusionPointCost: number; } +export interface IThemeUpdateRequest { + Style?: string; + Background?: string; + Sounds?: string; +} + export interface IMissionInventoryUpdateRequest { rewardsMultiplier?: number; ActiveBoosters?: IBooster[];