1+ using Microsoft . AspNetCore . Mvc ;
2+ using Microsoft . EntityFrameworkCore ;
3+ using OpenShock . API . Controller . Admin . DTOs ;
4+ using OpenShock . Common . Services . Configuration ;
5+
6+ namespace OpenShock . API . Controller . Admin ;
7+
8+ public sealed partial class AdminController
9+ {
10+ /// <summary>
11+ /// Gets all configuration items
12+ /// </summary>
13+ /// <response code="200"></response>
14+ /// <response code="401">Unauthorized</response>
15+ [ HttpGet ( "config" ) ]
16+ public IAsyncEnumerable < ConfigurationItemDto > ConfigurationList ( [ FromServices ] IConfigurationService configurationService )
17+ {
18+ return configurationService
19+ . GetAllItemsQuery ( )
20+ . Select ( ci => new ConfigurationItemDto
21+ {
22+ Name = ci . Name ,
23+ Description = ci . Description ,
24+ Type = ci . Type ,
25+ Value = ci . Value ,
26+ UpdatedAt = ci . UpdatedAt ,
27+ CreatedAt = ci . CreatedAt ,
28+ } )
29+ . AsAsyncEnumerable ( ) ;
30+ }
31+
32+ /// <summary>
33+ /// Adds a new configuration
34+ /// </summary>
35+ /// <response code="200">Configuration added</response>
36+ /// <response code="400">Invalid name or value format</response>
37+ /// <response code="409">Already exists</response>
38+ /// <response code="401">Unauthorized</response>
39+ [ HttpPost ( "config" ) ]
40+ public async Task < IActionResult > ConfigurationAdd (
41+ [ FromBody ] ConfigurationAddItemRequest body ,
42+ [ FromServices ] IConfigurationService configurationService ,
43+ CancellationToken cancellationToken )
44+ {
45+ var result = await configurationService . TryAddItemAsync (
46+ body . Name ,
47+ body . Description ,
48+ body . Type ,
49+ body . Value
50+ ) ;
51+
52+ return result . Match < IActionResult > (
53+ success => Ok ( ) ,
54+ alreadyExists => Conflict ( new { Error = "AlreadyExists" , Message = $ "A configuration named '{ body . Name } ' already exists." } ) ,
55+ invalidName => BadRequest ( new { Error = "InvalidNameFormat" , Message = $ "Invalid configuration name: '{ body . Name } '. Only A–Z and '_' allowed." } ) ,
56+ invalidValue => BadRequest ( new { Error = "InvalidValueFormat" , Message = $ "Value '{ body . Value } ' is not a valid { body . Type } ." } )
57+ ) ;
58+ }
59+
60+ /// <summary>
61+ /// Updates an existing configuration
62+ /// </summary>
63+ /// <response code="200">Configuration updated (or nothing to do)</response>
64+ /// <response code="401">Unauthorized</response>
65+ /// <response code="400">Invalid name or value format or type mismatch</response>
66+ /// <response code="404">Not found</response>
67+ [ HttpPut ( "config" ) ]
68+ public async Task < IActionResult > ConfigurationUpdate (
69+ [ FromBody ] ConfigurationUpdateItemRequest body ,
70+ [ FromServices ] IConfigurationService configurationService ,
71+ CancellationToken cancellationToken )
72+ {
73+ var result = await configurationService . TryUpdateItemAsync (
74+ body . Name ,
75+ body . Description ,
76+ body . Value
77+ ) ;
78+
79+ return result . Match < IActionResult > (
80+ success => Ok ( ) ,
81+ notFound => NotFound ( new { Error = "NotFound" , Message = $ "No configuration named '{ body . Name } '." } ) ,
82+ invalidName => BadRequest ( new { Error = "InvalidNameFormat" , Message = $ "Invalid configuration name: '{ body . Name } '." } ) ,
83+ invalidValue => BadRequest ( new { Error = "InvalidValueFormat" , Message = $ "Value '{ body . Value } ' is not a valid format for '{ body . Name } '." } ) ,
84+ invalidType => BadRequest ( new { Error = "InvalidValueType" , Message = $ "Type mismatch when updating '{ body . Name } '." } )
85+ ) ;
86+ }
87+
88+ /// <summary>
89+ /// Deletes a configuration
90+ /// </summary>
91+ /// <response code="200">Configuration deleted</response>
92+ /// <response code="401">Unauthorized</response>
93+ /// <response code="400">Invalid name format</response>
94+ /// <response code="404">Not found</response>
95+ [ HttpDelete ( "config/{name}" ) ]
96+ public async Task < IActionResult > ConfigurationDelete (
97+ string name ,
98+ [ FromServices ] IConfigurationService configurationService ,
99+ CancellationToken cancellationToken )
100+ {
101+ var result = await configurationService . TryDeleteItemAsync ( name ) ;
102+
103+ return result . Match < IActionResult > (
104+ success => Ok ( ) ,
105+ notFound => NotFound ( new { Error = "NotFound" , Message = $ "No configuration named '{ name } '." } ) ,
106+ invalidName => BadRequest ( new { Error = "InvalidNameFormat" , Message = $ "Invalid configuration name: '{ name } '." } )
107+ ) ;
108+ }
109+ }
0 commit comments