From fade5d8ff047c234fa2a913d4a3f0b1dc363d122 Mon Sep 17 00:00:00 2001 From: sdcb Date: Mon, 9 Dec 2024 19:07:49 +0800 Subject: [PATCH] supports create global config --- .../GlobalConfigs/GlobalConfigController.cs | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/BE/Controllers/Admin/GlobalConfigs/GlobalConfigController.cs b/src/BE/Controllers/Admin/GlobalConfigs/GlobalConfigController.cs index a2c06d23..453eb99f 100644 --- a/src/BE/Controllers/Admin/GlobalConfigs/GlobalConfigController.cs +++ b/src/BE/Controllers/Admin/GlobalConfigs/GlobalConfigController.cs @@ -52,4 +52,31 @@ public async Task UpdateGlobalConfig([FromBody] GlobalConfigDto re } return NoContent(); } + + [HttpPost] + public async Task CreateGlobalConfig([FromBody] GlobalConfigDto req, CancellationToken cancellationToken) + { + Config? config = await db.Configs.FindAsync([req.Key], cancellationToken); + if (config != null) + { + return this.BadRequestMessage("Key already exists"); + } + // ensure value is valid json + try + { + JsonDocument.Parse(req.Value); + } + catch (JsonException) + { + return this.BadRequestMessage("Invalid JSON"); + } + await db.Configs.AddAsync(new Config() + { + Key = req.Key, + Value = req.Value, + Description = req.Description, + }, cancellationToken); + await db.SaveChangesAsync(cancellationToken); + return NoContent(); + } }