From 933d762f24231e0d6644b6be57b370838888395d Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Sat, 28 Jun 2025 19:20:36 +0800 Subject: [PATCH] :sparkles: Custom apps developer APIs --- .../Developer/CustomAppController.cs | 58 ++++++++++++++- .../Developer/CustomAppService.cs | 73 +++++++++++++++++++ .../Startup/ServiceCollectionExtensions.cs | 2 + 3 files changed, 130 insertions(+), 3 deletions(-) create mode 100644 DysonNetwork.Sphere/Developer/CustomAppService.cs diff --git a/DysonNetwork.Sphere/Developer/CustomAppController.cs b/DysonNetwork.Sphere/Developer/CustomAppController.cs index 834a3f4..8441e8d 100644 --- a/DysonNetwork.Sphere/Developer/CustomAppController.cs +++ b/DysonNetwork.Sphere/Developer/CustomAppController.cs @@ -5,7 +5,59 @@ namespace DysonNetwork.Sphere.Developer; [ApiController] [Route("/developers/apps")] -public class CustomAppController(PublisherService ps) : ControllerBase +public class CustomAppController(CustomAppService customAppService, PublisherService ps) : ControllerBase { - -} \ No newline at end of file + [HttpGet("")] + public async Task GetApps([FromQuery] Guid publisherId) + { + var apps = await customAppService.GetAppsByPublisherAsync(publisherId); + return Ok(apps); + } + + [HttpGet("{id:guid}")] + public async Task GetApp(Guid id) + { + var app = await customAppService.GetAppAsync(id); + if (app == null) + { + return NotFound(); + } + return Ok(app); + } + + [HttpPost("")] + public async Task CreateApp([FromBody] CreateAppDto dto) + { + var app = await customAppService.CreateAppAsync(dto.PublisherId, dto.Name, dto.Slug); + if (app == null) + { + return BadRequest("Invalid publisher ID or missing developer feature flag"); + } + return CreatedAtAction(nameof(GetApp), new { id = app.Id }, app); + } + + [HttpPut("{id:guid}")] + public async Task UpdateApp(Guid id, [FromBody] UpdateAppDto dto) + { + var app = await customAppService.UpdateAppAsync(id, dto.Name, dto.Slug); + if (app == null) + { + return NotFound(); + } + return Ok(app); + } + + [HttpDelete("{id:guid}")] + public async Task DeleteApp(Guid id) + { + var result = await customAppService.DeleteAppAsync(id); + if (!result) + { + return NotFound(); + } + return NoContent(); + } +} + +public record CreateAppDto(Guid PublisherId, string Name, string Slug); +public record UpdateAppDto(string Name, string Slug); \ No newline at end of file diff --git a/DysonNetwork.Sphere/Developer/CustomAppService.cs b/DysonNetwork.Sphere/Developer/CustomAppService.cs new file mode 100644 index 0000000..461b08e --- /dev/null +++ b/DysonNetwork.Sphere/Developer/CustomAppService.cs @@ -0,0 +1,73 @@ +using DysonNetwork.Sphere.Publisher; +using Microsoft.EntityFrameworkCore; + +namespace DysonNetwork.Sphere.Developer; + +public class CustomAppService(AppDatabase db, PublisherService ps) +{ + public async Task CreateAppAsync(Guid publisherId, string name, string slug) + { + var publisher = await db.Publishers.FirstOrDefaultAsync(p => p.Id == publisherId); + if (publisher == null) + { + return null; + } + + if (!await ps.HasFeature(publisherId, "developer")) + { + return null; + } + + var app = new CustomApp + { + Name = name, + Slug = slug, + PublisherId = publisher.Id + }; + + db.CustomApps.Add(app); + await db.SaveChangesAsync(); + + return app; + } + + public async Task GetAppAsync(Guid id) + { + return await db.CustomApps.FindAsync(id); + } + + public async Task> GetAppsByPublisherAsync(Guid publisherId) + { + return await db.CustomApps.Where(a => a.PublisherId == publisherId).ToListAsync(); + } + + public async Task UpdateAppAsync(Guid id, string name, string slug) + { + var app = await db.CustomApps.FindAsync(id); + if (app == null) + { + return null; + } + + app.Name = name; + app.Slug = slug; + + await db.SaveChangesAsync(); + + return app; + } + + public async Task DeleteAppAsync(Guid id) + { + var app = await db.CustomApps.FindAsync(id); + if (app == null) + { + return false; + } + + db.CustomApps.Remove(app); + await db.SaveChangesAsync(); + + return true; + } +} \ No newline at end of file diff --git a/DysonNetwork.Sphere/Startup/ServiceCollectionExtensions.cs b/DysonNetwork.Sphere/Startup/ServiceCollectionExtensions.cs index ab71e5d..9f66050 100644 --- a/DysonNetwork.Sphere/Startup/ServiceCollectionExtensions.cs +++ b/DysonNetwork.Sphere/Startup/ServiceCollectionExtensions.cs @@ -25,6 +25,7 @@ using StackExchange.Redis; using System.Text.Json; using System.Threading.RateLimiting; using DysonNetwork.Sphere.Connection.WebReader; +using DysonNetwork.Sphere.Developer; using DysonNetwork.Sphere.Discovery; using DysonNetwork.Sphere.Safety; using DysonNetwork.Sphere.Wallet.PaymentHandlers; @@ -231,6 +232,7 @@ public static class ServiceCollectionExtensions services.AddScoped(); services.AddScoped(); services.AddScoped(); + services.AddScoped(); return services; }