feat(backend): add create flag endpoint and job
This commit is contained in:
parent
ff2ba1fb1b
commit
14e6e35cb7
4 changed files with 87 additions and 5 deletions
|
@ -1,4 +1,7 @@
|
|||
using Coravel.Queuing.Interfaces;
|
||||
using Foxnouns.Backend.Database;
|
||||
using Foxnouns.Backend.Database.Models;
|
||||
using Foxnouns.Backend.Jobs;
|
||||
using Foxnouns.Backend.Middleware;
|
||||
using Foxnouns.Backend.Services;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
@ -7,7 +10,11 @@ using Microsoft.EntityFrameworkCore;
|
|||
namespace Foxnouns.Backend.Controllers;
|
||||
|
||||
[Route("/api/v2/users/@me/flags")]
|
||||
public class FlagsController(DatabaseContext db, UserRendererService userRenderer) : ApiControllerBase
|
||||
public class FlagsController(
|
||||
DatabaseContext db,
|
||||
UserRendererService userRenderer,
|
||||
ISnowflakeGenerator snowflakeGenerator,
|
||||
IQueue queue) : ApiControllerBase
|
||||
{
|
||||
[HttpGet]
|
||||
[Authorize("identify")]
|
||||
|
@ -16,10 +23,29 @@ public class FlagsController(DatabaseContext db, UserRendererService userRendere
|
|||
{
|
||||
var flags = await db.PrideFlags.Where(f => f.UserId == CurrentUser!.Id).ToListAsync(ct);
|
||||
|
||||
return Ok(flags.Select(f => new PrideFlagResponse(
|
||||
f.Id, userRenderer.ImageUrlFor(f), f.Name, f.Description)));
|
||||
return Ok(flags.Select(ToResponse));
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[Authorize("user.update")]
|
||||
[ProducesResponseType<PrideFlagResponse>(statusCode: StatusCodes.Status202Accepted)]
|
||||
public IActionResult CreateFlag([FromBody] CreateFlagRequest req)
|
||||
{
|
||||
var id = snowflakeGenerator.GenerateSnowflake();
|
||||
|
||||
queue.QueueInvocableWithPayload<CreateFlagInvocable, CreateFlagPayload>(
|
||||
new CreateFlagPayload(id, CurrentUser!.Id, req.Name, req.Image, req.Description));
|
||||
|
||||
return Accepted(new CreateFlagResponse(id, req.Name, req.Description));
|
||||
}
|
||||
|
||||
public record CreateFlagRequest(string Name, string Image, string? Description);
|
||||
|
||||
public record CreateFlagResponse(Snowflake Id, string Name, string? Description);
|
||||
|
||||
private PrideFlagResponse ToResponse(PrideFlag flag) =>
|
||||
new(flag.Id, userRenderer.ImageUrlFor(flag), flag.Name, flag.Description);
|
||||
|
||||
private record PrideFlagResponse(
|
||||
Snowflake Id,
|
||||
string ImageUrl,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue