Skip to content

Commit 35881f0

Browse files
committed
- refactoring
1 parent 7a262f3 commit 35881f0

File tree

4 files changed

+145
-67
lines changed

4 files changed

+145
-67
lines changed

Botticelli.Server.Back/Controllers/AdminController.cs

Lines changed: 65 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,29 @@ namespace Botticelli.Server.Back.Controllers;
2020
[Authorize(AuthenticationSchemes = "Bearer")]
2121
[Route("/v1/admin")]
2222
public class AdminController(
23-
IBotManagementService botManagementService,
24-
IBotStatusDataService botStatusDataService,
25-
ILogger<AdminController> logger,
26-
IBroadcastService broadcastService) : ControllerBase
23+
IBotManagementService botManagementService,
24+
IBotStatusDataService botStatusDataService,
25+
ILogger<AdminController> logger,
26+
IBroadcastService broadcastService) : ControllerBase
2727
{
28+
/// <summary>
29+
/// Adds a new bot
30+
/// </summary>
31+
/// <param name="request"></param>
32+
/// <returns></returns>
2833
[HttpPost("[action]")]
2934
public async Task<RegisterBotResponse> AddNewBot([FromBody] RegisterBotRequest request)
3035
{
3136
logger.LogInformation("{AddNewBotName}({RequestBotId}) started...", nameof(AddNewBot), request.BotId);
3237
var success = await botManagementService.RegisterBot(request.BotId,
33-
request.BotKey,
34-
request.BotName,
35-
request.Type);
38+
request.BotKey,
39+
request.BotName,
40+
request.Type);
3641

37-
logger.LogInformation("{AddNewBotName}({RequestBotId}) success: {Success}...", nameof(AddNewBot), request.BotId, success);
42+
logger.LogInformation("{AddNewBotName}({RequestBotId}) success: {Success}...",
43+
nameof(AddNewBot),
44+
request.BotId,
45+
success);
3846

3947
return new RegisterBotResponse
4048
{
@@ -43,15 +51,23 @@ public async Task<RegisterBotResponse> AddNewBot([FromBody] RegisterBotRequest r
4351
};
4452
}
4553

54+
/// <summary>
55+
/// Updates a bot
56+
/// </summary>
57+
/// <param name="request"></param>
58+
/// <returns></returns>
4659
[HttpPut("[action]")]
4760
public async Task<UpdateBotResponse> UpdateBot([FromBody] UpdateBotRequest request)
4861
{
4962
logger.LogInformation("{UpdateBotName}({RequestBotId}) started...", nameof(UpdateBot), request.BotId);
5063
var success = await botManagementService.UpdateBot(request.BotId,
51-
request.BotKey,
52-
request.BotName);
64+
request.BotKey,
65+
request.BotName);
5366

54-
logger.LogInformation("{UpdateBotName}({RequestBotId}) success: {Success}...", nameof(UpdateBot), request.BotId, success);
67+
logger.LogInformation("{UpdateBotName}({RequestBotId}) success: {Success}...",
68+
nameof(UpdateBot),
69+
request.BotId,
70+
success);
5571

5672
return new UpdateBotResponse
5773
{
@@ -67,7 +83,10 @@ public async Task<UpdateBotResponse> UpdateBot([FromBody] UpdateBotRequest reque
6783
/// <param name="message"></param>
6884
/// <returns></returns>
6985
[HttpPost("[action]")]
70-
public async Task SendBroadcast([FromQuery] string botId, [FromBody] Message message) => await DoBroadcast(botId, message);
86+
public async Task SendBroadcast([FromQuery] string botId, [FromBody] Message message)
87+
{
88+
await DoBroadcast(botId, message);
89+
}
7190

7291
/// <summary>
7392
/// Sends a broadcast message in binary stream
@@ -82,57 +101,72 @@ public async Task SendBroadcastBinary([FromQuery] string botId)
82101
await Request.Body.CopyToAsync(memoryStream);
83102

84103
var message = JsonSerializer.Deserialize<Message>(Encoding.UTF8.GetString(memoryStream.ToArray()));
85-
104+
86105
await DoBroadcast(botId, message);
87106
}
88107

89108
private async Task DoBroadcast(string botId, Message? message)
90109
{
91110
await broadcastService.BroadcastMessage(new Broadcast
92111
{
93-
Id = message.Uid ?? throw new NullReferenceException("Id cannot be null!"),
112+
Id = message.Uid ?? throw new NullReferenceException("Id cannot be null!"),
94113
BotId = botId ?? throw new NullReferenceException("BotId cannot be null!"),
95114
Body = message.Body ?? throw new NullReferenceException("Body cannot be null!"),
96115
Attachments = message.Attachments
97-
.Where(a => a is BinaryBaseAttachment)
98-
.Select(a =>
99-
{
100-
if (a is BinaryBaseAttachment baseAttachment)
101-
return new BroadcastAttachment
102-
{
103-
Id = Guid.NewGuid(),
104-
BroadcastId = message.Uid,
105-
MediaType = baseAttachment.MediaType,
106-
Filename = baseAttachment.Name,
107-
Content = baseAttachment.Data
108-
};
109-
110-
return null!;
111-
})
112-
.ToList(),
116+
.Where(a => a is BinaryBaseAttachment)
117+
.Select(a =>
118+
{
119+
if (a is BinaryBaseAttachment baseAttachment)
120+
return new BroadcastAttachment
121+
{
122+
Id = Guid.NewGuid(),
123+
BroadcastId = message.Uid,
124+
MediaType = baseAttachment.MediaType,
125+
Filename = baseAttachment.Name,
126+
Content = baseAttachment.Data
127+
};
128+
129+
return null!;
130+
})
131+
.ToList(),
113132
Sent = true
114133
});
115134
}
116135

117-
136+
/// <summary>
137+
/// Get bots list
138+
/// </summary>
139+
/// <returns></returns>
118140
[HttpGet("[action]")]
119141
public Task<ICollection<BotInfo>> GetBots()
120142
{
121143
return Task.FromResult(botStatusDataService.GetBots());
122144
}
123145

146+
/// <summary>
147+
/// Activates a bot (BotStatus.Unlocked)
148+
/// </summary>
149+
/// <param name="botId"></param>
124150
[HttpGet("[action]")]
125151
public async Task ActivateBot([FromQuery] string botId)
126152
{
127153
await botManagementService.SetRequiredBotStatus(botId, BotStatus.Unlocked);
128154
}
129155

156+
/// <summary>
157+
/// Deactivates a bot (BotStatus.Locked)
158+
/// </summary>
159+
/// <param name="botId"></param>
130160
[HttpGet("[action]")]
131161
public async Task DeactivateBot([FromQuery] string botId)
132162
{
133163
await botManagementService.SetRequiredBotStatus(botId, BotStatus.Locked);
134164
}
135165

166+
/// <summary>
167+
/// Removes a bot
168+
/// </summary>
169+
/// <param name="botId"></param>
136170
[HttpGet("[action]")]
137171
public async Task RemoveBot([FromQuery] string botId)
138172
{

Botticelli.Server.Back/Controllers/AuthController.cs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,17 @@ namespace Botticelli.Server.Back.Controllers;
1111
[ApiController]
1212
[AllowAnonymous]
1313
[Route("/v1/auth")]
14-
public class AuthController
14+
public class AuthController(IAdminAuthService adminAuthService)
1515
{
16-
private readonly IAdminAuthService _adminAuthService;
17-
18-
public AuthController(IAdminAuthService adminAuthService)
19-
{
20-
_adminAuthService = adminAuthService;
21-
}
22-
16+
/// <summary>
17+
/// Gets auth token for a user
18+
/// </summary>
19+
/// <param name="request"></param>
20+
/// <returns></returns>
2321
[AllowAnonymous]
2422
[HttpPost("[action]")]
2523
public IActionResult GetToken(UserLoginRequest request)
2624
{
27-
return new OkObjectResult(_adminAuthService.GenerateToken(request));
25+
return new OkObjectResult(adminAuthService.GenerateToken(request));
2826
}
2927
}

Botticelli.Server.Back/Controllers/BotController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
namespace Botticelli.Server.Back.Controllers;
1313

1414
/// <summary>
15-
/// Bot status controller
15+
/// Bot status/data controller
1616
/// </summary>
1717
[ApiController]
1818
[AllowAnonymous]

0 commit comments

Comments
 (0)