✨ Custom apps developer APIs
This commit is contained in:
parent
8251a9ec7d
commit
933d762f24
@ -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
|
||||
{
|
||||
|
||||
[HttpGet("")]
|
||||
public async Task<IActionResult> GetApps([FromQuery] Guid publisherId)
|
||||
{
|
||||
var apps = await customAppService.GetAppsByPublisherAsync(publisherId);
|
||||
return Ok(apps);
|
||||
}
|
||||
|
||||
[HttpGet("{id:guid}")]
|
||||
public async Task<IActionResult> GetApp(Guid id)
|
||||
{
|
||||
var app = await customAppService.GetAppAsync(id);
|
||||
if (app == null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
return Ok(app);
|
||||
}
|
||||
|
||||
[HttpPost("")]
|
||||
public async Task<IActionResult> 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<IActionResult> 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<IActionResult> 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);
|
73
DysonNetwork.Sphere/Developer/CustomAppService.cs
Normal file
73
DysonNetwork.Sphere/Developer/CustomAppService.cs
Normal file
@ -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<CustomApp?> 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<CustomApp?> GetAppAsync(Guid id)
|
||||
{
|
||||
return await db.CustomApps.FindAsync(id);
|
||||
}
|
||||
|
||||
public async Task<List<CustomApp>> GetAppsByPublisherAsync(Guid publisherId)
|
||||
{
|
||||
return await db.CustomApps.Where(a => a.PublisherId == publisherId).ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<CustomApp?> 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<bool> DeleteAppAsync(Guid id)
|
||||
{
|
||||
var app = await db.CustomApps.FindAsync(id);
|
||||
if (app == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
db.CustomApps.Remove(app);
|
||||
await db.SaveChangesAsync();
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
@ -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<AfdianPaymentHandler>();
|
||||
services.AddScoped<SafetyService>();
|
||||
services.AddScoped<DiscoveryService>();
|
||||
services.AddScoped<CustomAppService>();
|
||||
|
||||
return services;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user