From 02ee59817a8607bd1d7b68d24f0a45c85310a0d1 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sat, 21 Mar 2020 18:54:30 +0100 Subject: [PATCH] Update (save) new language settings --- src/controllers/Routes.ts | 2 ++ src/controllers/SettingsController.ts | 27 ++++++++++++++++++++++----- src/entities/User.ts | 4 ++-- src/helpers/AccountHelper.ts | 19 ++++++++++++++++++- 4 files changed, 44 insertions(+), 8 deletions(-) diff --git a/src/controllers/Routes.ts b/src/controllers/Routes.ts index 1c7961b..e19df96 100644 --- a/src/controllers/Routes.ts +++ b/src/controllers/Routes.ts @@ -89,6 +89,8 @@ export const Routes : Route[] = [ {path: "/settings/get_language", cb: (h) => SettingsController.GetLanguage(h)}, + {path: "/settings/set_language", cb: (h) => SettingsController.SetLanguage(h)}, + // Friends controller {path: "/friends/getList", cb: (h) => FriendsController.GetList(h)}, diff --git a/src/controllers/SettingsController.ts b/src/controllers/SettingsController.ts index b684fd0..11824df 100644 --- a/src/controllers/SettingsController.ts +++ b/src/controllers/SettingsController.ts @@ -6,10 +6,8 @@ import { RequestHandler } from "../entities/RequestHandler"; import { UserHelper } from "../helpers/UserHelper"; -import { UserController } from "./UserController"; -import { GeneralSettings, User, UserPageStatus } from "../entities/User"; +import { GeneralSettings, UserPageStatus, SupportedLanguages, LangSettings } from "../entities/User"; import { removeHTMLNodes, checkURL } from "../utils/StringUtils"; -import { VirtualDirectoryController } from "./VirtualDirectoryController"; import { checkVirtualDirectoryAvailability, VirtualDirType } from "../utils/VirtualDirsUtils"; import { AccountHelper } from "../helpers/AccountHelper"; @@ -109,13 +107,32 @@ export class SettingsController { * @param h Request handler */ public static async GetLanguage(h: RequestHandler) { - const userInfo = await UserHelper.GetUserInfo(h.getUserId()); h.send({ lang: userInfo.lang }); - } + /** + * Update (set) language settings + * + * @param h Request handler + */ + public static async SetLanguage(h: RequestHandler) { + const lang = h.postString("lang", 2); + + if(!SupportedLanguages.includes(lang)) + h.error(401, "Language not supported!"); + + + const newSettings: LangSettings = { + id: h.getUserId(), + lang: lang + } + + await AccountHelper.SetLanguageSettings(newSettings); + + h.success(); + } } \ No newline at end of file diff --git a/src/entities/User.ts b/src/entities/User.ts index 0f12525..1ffeaeb 100644 --- a/src/entities/User.ts +++ b/src/entities/User.ts @@ -6,14 +6,14 @@ import { AccountImage } from "./AccountImage"; * @author Pierre HUBERT */ +export const SupportedLanguages = ["fr", "en"] + export enum UserPageStatus { PRIVATE, PUBLIC, OPEN } - - export interface UserInfo { id: number, firstName: string, diff --git a/src/helpers/AccountHelper.ts b/src/helpers/AccountHelper.ts index ddb645d..96d47c4 100644 --- a/src/helpers/AccountHelper.ts +++ b/src/helpers/AccountHelper.ts @@ -5,7 +5,7 @@ import { DatabaseHelper } from "./DatabaseHelper"; import { UserHelper } from "./UserHelper"; import { time, mysql_date } from "../utils/DateUtils"; import { NewAccount } from "../entities/NewAccount"; -import { GeneralSettings, UserPageStatus } from "../entities/User"; +import { GeneralSettings, UserPageStatus, LangSettings } from "../entities/User"; /** * Account helper @@ -337,4 +337,21 @@ export class AccountHelper { } }); } + + /** + * Set (save) new language settings + * + * @param settings New settings + */ + public static async SetLanguageSettings(settings: LangSettings) { + await DatabaseHelper.UpdateRows({ + table: USER_TABLE, + where: { + ID: settings.id + }, + set: { + lang: settings.lang + } + }); + } } \ No newline at end of file