From d9620fd6a40b4434d0f9396b764f1860255f5ed6 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Sat, 23 Aug 2025 17:55:42 +0800 Subject: [PATCH] :sparkles: Bot transparency API --- DysonNetwork.Develop/Identity/BotAccount.cs | 6 ++++ .../Identity/BotAccountPublicController.cs | 35 +++++++++++++++++++ .../Identity/DeveloperService.cs | 10 +++++- 3 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 DysonNetwork.Develop/Identity/BotAccountPublicController.cs diff --git a/DysonNetwork.Develop/Identity/BotAccount.cs b/DysonNetwork.Develop/Identity/BotAccount.cs index bbd12bc..bad036a 100644 --- a/DysonNetwork.Develop/Identity/BotAccount.cs +++ b/DysonNetwork.Develop/Identity/BotAccount.cs @@ -17,6 +17,12 @@ public class BotAccount : ModelBase public DevProject Project { get; set; } = null!; [NotMapped] public AccountReference? Account { get; set; } + + /// + /// This developer field is to serve the transparent info for user to know which developer + /// published this robot. Not for relationships usage. + /// + [NotMapped] public Developer? Developer { get; set; } public Shared.Proto.BotAccount ToProtoValue() { diff --git a/DysonNetwork.Develop/Identity/BotAccountPublicController.cs b/DysonNetwork.Develop/Identity/BotAccountPublicController.cs new file mode 100644 index 0000000..75c00af --- /dev/null +++ b/DysonNetwork.Develop/Identity/BotAccountPublicController.cs @@ -0,0 +1,35 @@ +using Microsoft.AspNetCore.Mvc; + +namespace DysonNetwork.Develop.Identity; + +[ApiController] +[Route("api/bots")] +public class BotAccountPublicController(BotAccountService botService, DeveloperService developerService) : ControllerBase +{ + [HttpGet("{botId:guid}")] + public async Task> GetBotTransparentInfo([FromRoute] Guid botId) + { + var bot = await botService.GetBotByIdAsync(botId); + if (bot is null) return NotFound("Bot not found"); + bot = await botService.LoadBotAccountAsync(bot); + + var developer = await developerService.GetDeveloperById(bot!.Project.DeveloperId); + if (developer is null) return NotFound("Developer not found"); + bot.Developer = await developerService.LoadDeveloperPublisher(developer); + + return Ok(bot); + } + + [HttpGet("{botId:guid}/developer")] + public async Task> GetBotDeveloper([FromRoute] Guid botId) + { + var bot = await botService.GetBotByIdAsync(botId); + if (bot is null) return NotFound("Bot not found"); + + var developer = await developerService.GetDeveloperById(bot!.Project.DeveloperId); + if (developer is null) return NotFound("Developer not found"); + developer = await developerService.LoadDeveloperPublisher(developer); + + return Ok(developer); + } +} \ No newline at end of file diff --git a/DysonNetwork.Develop/Identity/DeveloperService.cs b/DysonNetwork.Develop/Identity/DeveloperService.cs index 3d5bfa8..72b5611 100644 --- a/DysonNetwork.Develop/Identity/DeveloperService.cs +++ b/DysonNetwork.Develop/Identity/DeveloperService.cs @@ -4,7 +4,10 @@ using Microsoft.EntityFrameworkCore; namespace DysonNetwork.Develop.Identity; -public class DeveloperService(AppDatabase db, PublisherService.PublisherServiceClient ps, ILogger logger) +public class DeveloperService( + AppDatabase db, + PublisherService.PublisherServiceClient ps, + ILogger logger) { public async Task LoadDeveloperPublisher(Developer developer) { @@ -47,6 +50,11 @@ public class DeveloperService(AppDatabase db, PublisherService.PublisherServiceC } } + public async Task GetDeveloperById(Guid id) + { + return await db.Developers.FirstOrDefaultAsync(d => d.Id == id); + } + public async Task IsMemberWithRole(Guid pubId, Guid accountId, PublisherMemberRole role) { try