feat: collections controller, healthcheck
This commit is contained in:
@@ -0,0 +1,109 @@
|
|||||||
|
using MediatR;
|
||||||
|
using MemeVault.Api.Auth;
|
||||||
|
using MemeVault.Application.Commands.CreateCollection;
|
||||||
|
using MemeVault.Application.Commands.DeleteCollection;
|
||||||
|
using MemeVault.Application.Commands.ManageCollectionItem;
|
||||||
|
using MemeVault.Application.Commands.ShareCollection;
|
||||||
|
using MemeVault.Application.Commands.UpdateCollection;
|
||||||
|
using MemeVault.Application.Queries.GetUserCollections;
|
||||||
|
using MemeVault.Domain.Enums;
|
||||||
|
using Microsoft.AspNetCore.Authorization;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
|
||||||
|
namespace MemeVault.Api.Controllers;
|
||||||
|
|
||||||
|
[ApiController]
|
||||||
|
[Authorize]
|
||||||
|
[Route("api/[controller]")]
|
||||||
|
public sealed class CollectionsController(IMediator mediator, CurrentUser currentUser) : ControllerBase
|
||||||
|
{
|
||||||
|
// GET /api/collections
|
||||||
|
[HttpGet]
|
||||||
|
public async Task<IActionResult> GetAll(CancellationToken ct)
|
||||||
|
{
|
||||||
|
var result = await mediator.Send(new GetUserCollectionsQuery(currentUser.TelegramId), ct);
|
||||||
|
return Ok(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
// POST /api/collections
|
||||||
|
[HttpPost]
|
||||||
|
public async Task<IActionResult> Create(
|
||||||
|
[FromBody] CreateCollectionRequest request,
|
||||||
|
CancellationToken ct)
|
||||||
|
{
|
||||||
|
var id = await mediator.Send(
|
||||||
|
new CreateCollectionCommand(currentUser.TelegramId, request.Name, request.Description),
|
||||||
|
ct);
|
||||||
|
|
||||||
|
return CreatedAtAction(nameof(GetAll), new { id }, new { id });
|
||||||
|
}
|
||||||
|
|
||||||
|
// PATCH /api/collections/{id}
|
||||||
|
[HttpPatch("{id:guid}")]
|
||||||
|
public async Task<IActionResult> Update(
|
||||||
|
Guid id,
|
||||||
|
[FromBody] UpdateCollectionRequest request,
|
||||||
|
CancellationToken ct)
|
||||||
|
{
|
||||||
|
var ok = await mediator.Send(
|
||||||
|
new UpdateCollectionCommand(id, currentUser.TelegramId, request.Name, request.Description),
|
||||||
|
ct);
|
||||||
|
|
||||||
|
return ok ? NoContent() : NotFound();
|
||||||
|
}
|
||||||
|
|
||||||
|
// DELETE /api/collections/{id}
|
||||||
|
[HttpDelete("{id:guid}")]
|
||||||
|
public async Task<IActionResult> Delete(Guid id, CancellationToken ct)
|
||||||
|
{
|
||||||
|
var ok = await mediator.Send(new DeleteCollectionCommand(id, currentUser.TelegramId), ct);
|
||||||
|
return ok ? NoContent() : NotFound();
|
||||||
|
}
|
||||||
|
|
||||||
|
// PUT /api/collections/{id}/visibility
|
||||||
|
[HttpPut("{id:guid}/visibility")]
|
||||||
|
public async Task<IActionResult> SetVisibility(
|
||||||
|
Guid id,
|
||||||
|
[FromBody] SetVisibilityRequest request,
|
||||||
|
CancellationToken ct)
|
||||||
|
{
|
||||||
|
var shareToken = await mediator.Send(
|
||||||
|
new ShareCollectionCommand(id, currentUser.TelegramId, request.Visibility),
|
||||||
|
ct);
|
||||||
|
|
||||||
|
return Ok(new { shareToken });
|
||||||
|
}
|
||||||
|
|
||||||
|
// POST /api/collections/{id}/items
|
||||||
|
[HttpPost("{id:guid}/items")]
|
||||||
|
public async Task<IActionResult> AddItem(
|
||||||
|
Guid id,
|
||||||
|
[FromBody] CollectionItemRequest request,
|
||||||
|
CancellationToken ct)
|
||||||
|
{
|
||||||
|
var ok = await mediator.Send(
|
||||||
|
new AddItemToCollectionCommand(id, request.MediaItemId, currentUser.TelegramId),
|
||||||
|
ct);
|
||||||
|
|
||||||
|
return ok ? NoContent() : NotFound();
|
||||||
|
}
|
||||||
|
|
||||||
|
// DELETE /api/collections/{id}/items/{mediaItemId}
|
||||||
|
[HttpDelete("{id:guid}/items/{mediaItemId:guid}")]
|
||||||
|
public async Task<IActionResult> RemoveItem(
|
||||||
|
Guid id,
|
||||||
|
Guid mediaItemId,
|
||||||
|
CancellationToken ct)
|
||||||
|
{
|
||||||
|
var ok = await mediator.Send(
|
||||||
|
new RemoveItemFromCollectionCommand(id, mediaItemId, currentUser.TelegramId),
|
||||||
|
ct);
|
||||||
|
|
||||||
|
return ok ? NoContent() : NotFound();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public sealed record CreateCollectionRequest(string Name, string? Description);
|
||||||
|
public sealed record UpdateCollectionRequest(string Name, string? Description);
|
||||||
|
public sealed record SetVisibilityRequest(CollectionVisibility Visibility);
|
||||||
|
public sealed record CollectionItemRequest(Guid MediaItemId);
|
||||||
@@ -25,6 +25,7 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks.EntityFrameworkCore" Version="10.0.12" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="10.0.0" />
|
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="10.0.0" />
|
||||||
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="8.4.0" />
|
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="8.4.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|||||||
@@ -5,6 +5,8 @@ using MemeVault.Api.Options;
|
|||||||
using MemeVault.Infrastructure;
|
using MemeVault.Infrastructure;
|
||||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||||
using Microsoft.IdentityModel.Tokens;
|
using Microsoft.IdentityModel.Tokens;
|
||||||
|
using MemeVault.Infrastructure.Persistence;
|
||||||
|
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
|
||||||
using Prometheus;
|
using Prometheus;
|
||||||
|
|
||||||
var builder = WebApplication.CreateBuilder(args);
|
var builder = WebApplication.CreateBuilder(args);
|
||||||
@@ -53,6 +55,10 @@ builder.Services.AddExceptionHandler<DomainExceptionHandler>();
|
|||||||
builder.Services.AddAuthorization();
|
builder.Services.AddAuthorization();
|
||||||
builder.Services.AddControllers();
|
builder.Services.AddControllers();
|
||||||
|
|
||||||
|
// ── OpenAPI ───────────────────────────────────────────────────────────────────
|
||||||
|
builder.Services.AddHealthChecks()
|
||||||
|
.AddDbContextCheck<AppDbContext>("postgres");
|
||||||
|
|
||||||
// ── OpenAPI ───────────────────────────────────────────────────────────────────
|
// ── OpenAPI ───────────────────────────────────────────────────────────────────
|
||||||
builder.Services.AddEndpointsApiExplorer();
|
builder.Services.AddEndpointsApiExplorer();
|
||||||
|
|
||||||
@@ -63,7 +69,7 @@ app.UseHttpMetrics();
|
|||||||
app.UseAuthentication();
|
app.UseAuthentication();
|
||||||
app.UseAuthorization();
|
app.UseAuthorization();
|
||||||
|
|
||||||
app.MapGet("/health", () => Results.Ok(new { status = "ok" }));
|
app.MapHealthChecks("/health");
|
||||||
|
|
||||||
app.MapControllers();
|
app.MapControllers();
|
||||||
app.MapMetrics();
|
app.MapMetrics();
|
||||||
|
|||||||
@@ -0,0 +1,8 @@
|
|||||||
|
using MediatR;
|
||||||
|
|
||||||
|
namespace MemeVault.Application.Commands.DeleteCollection;
|
||||||
|
|
||||||
|
public sealed record DeleteCollectionCommand(
|
||||||
|
Guid CollectionId,
|
||||||
|
long RequestingTelegramId
|
||||||
|
) : IRequest<bool>;
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
using MediatR;
|
||||||
|
using MemeVault.Application.Common;
|
||||||
|
using MemeVault.Domain.Repositories;
|
||||||
|
|
||||||
|
namespace MemeVault.Application.Commands.DeleteCollection;
|
||||||
|
|
||||||
|
public sealed class DeleteCollectionCommandHandler(
|
||||||
|
ICollectionRepository collections,
|
||||||
|
IUnitOfWork unitOfWork)
|
||||||
|
: IRequestHandler<DeleteCollectionCommand, bool>
|
||||||
|
{
|
||||||
|
public async Task<bool> Handle(DeleteCollectionCommand request, CancellationToken ct)
|
||||||
|
{
|
||||||
|
var collection = await collections.GetByIdAsync(request.CollectionId, ct);
|
||||||
|
if (collection is null || collection.OwnerTelegramId != request.RequestingTelegramId)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
await collections.RemoveAsync(collection, ct);
|
||||||
|
await unitOfWork.SaveChangesAsync(ct);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
+15
@@ -0,0 +1,15 @@
|
|||||||
|
using MediatR;
|
||||||
|
|
||||||
|
namespace MemeVault.Application.Commands.ManageCollectionItem;
|
||||||
|
|
||||||
|
public sealed record AddItemToCollectionCommand(
|
||||||
|
Guid CollectionId,
|
||||||
|
Guid MediaItemId,
|
||||||
|
long RequestingTelegramId
|
||||||
|
) : IRequest<bool>;
|
||||||
|
|
||||||
|
public sealed record RemoveItemFromCollectionCommand(
|
||||||
|
Guid CollectionId,
|
||||||
|
Guid MediaItemId,
|
||||||
|
long RequestingTelegramId
|
||||||
|
) : IRequest<bool>;
|
||||||
+37
@@ -0,0 +1,37 @@
|
|||||||
|
using MediatR;
|
||||||
|
using MemeVault.Application.Common;
|
||||||
|
using MemeVault.Domain.Repositories;
|
||||||
|
|
||||||
|
namespace MemeVault.Application.Commands.ManageCollectionItem;
|
||||||
|
|
||||||
|
public sealed class AddItemToCollectionCommandHandler(
|
||||||
|
ICollectionRepository collections,
|
||||||
|
IUnitOfWork unitOfWork)
|
||||||
|
: IRequestHandler<AddItemToCollectionCommand, bool>
|
||||||
|
{
|
||||||
|
public async Task<bool> Handle(AddItemToCollectionCommand request, CancellationToken ct)
|
||||||
|
{
|
||||||
|
var collection = await collections.GetByIdAsync(request.CollectionId, ct);
|
||||||
|
if (collection is null) return false;
|
||||||
|
|
||||||
|
collection.AddItem(request.MediaItemId, request.RequestingTelegramId);
|
||||||
|
await unitOfWork.SaveChangesAsync(ct);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public sealed class RemoveItemFromCollectionCommandHandler(
|
||||||
|
ICollectionRepository collections,
|
||||||
|
IUnitOfWork unitOfWork)
|
||||||
|
: IRequestHandler<RemoveItemFromCollectionCommand, bool>
|
||||||
|
{
|
||||||
|
public async Task<bool> Handle(RemoveItemFromCollectionCommand request, CancellationToken ct)
|
||||||
|
{
|
||||||
|
var collection = await collections.GetByIdAsync(request.CollectionId, ct);
|
||||||
|
if (collection is null) return false;
|
||||||
|
|
||||||
|
collection.RemoveItem(request.MediaItemId, request.RequestingTelegramId);
|
||||||
|
await unitOfWork.SaveChangesAsync(ct);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
using MediatR;
|
||||||
|
|
||||||
|
namespace MemeVault.Application.Commands.UpdateCollection;
|
||||||
|
|
||||||
|
public sealed record UpdateCollectionCommand(
|
||||||
|
Guid CollectionId,
|
||||||
|
long RequestingTelegramId,
|
||||||
|
string Name,
|
||||||
|
string? Description
|
||||||
|
) : IRequest<bool>;
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
using MediatR;
|
||||||
|
using MemeVault.Application.Common;
|
||||||
|
using MemeVault.Domain.Repositories;
|
||||||
|
|
||||||
|
namespace MemeVault.Application.Commands.UpdateCollection;
|
||||||
|
|
||||||
|
public sealed class UpdateCollectionCommandHandler(
|
||||||
|
ICollectionRepository collections,
|
||||||
|
IUnitOfWork unitOfWork)
|
||||||
|
: IRequestHandler<UpdateCollectionCommand, bool>
|
||||||
|
{
|
||||||
|
public async Task<bool> Handle(UpdateCollectionCommand request, CancellationToken ct)
|
||||||
|
{
|
||||||
|
var collection = await collections.GetByIdAsync(request.CollectionId, ct);
|
||||||
|
if (collection is null || collection.OwnerTelegramId != request.RequestingTelegramId)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
collection.Rename(request.Name, request.RequestingTelegramId);
|
||||||
|
await unitOfWork.SaveChangesAsync(ct);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
using MediatR;
|
||||||
|
using MemeVault.Domain.Enums;
|
||||||
|
|
||||||
|
namespace MemeVault.Application.Queries.GetUserCollections;
|
||||||
|
|
||||||
|
public sealed record GetUserCollectionsQuery(long OwnerTelegramId)
|
||||||
|
: IRequest<IReadOnlyList<CollectionDto>>;
|
||||||
|
|
||||||
|
public sealed record CollectionDto(
|
||||||
|
Guid Id,
|
||||||
|
string Name,
|
||||||
|
string? Description,
|
||||||
|
CollectionVisibility Visibility,
|
||||||
|
string? ShareToken,
|
||||||
|
int ItemCount,
|
||||||
|
DateTimeOffset CreatedAt
|
||||||
|
);
|
||||||
+25
@@ -0,0 +1,25 @@
|
|||||||
|
using MediatR;
|
||||||
|
using MemeVault.Domain.Repositories;
|
||||||
|
|
||||||
|
namespace MemeVault.Application.Queries.GetUserCollections;
|
||||||
|
|
||||||
|
public sealed class GetUserCollectionsQueryHandler(ICollectionRepository collections)
|
||||||
|
: IRequestHandler<GetUserCollectionsQuery, IReadOnlyList<CollectionDto>>
|
||||||
|
{
|
||||||
|
public async Task<IReadOnlyList<CollectionDto>> Handle(
|
||||||
|
GetUserCollectionsQuery request,
|
||||||
|
CancellationToken ct)
|
||||||
|
{
|
||||||
|
var items = await collections.GetByOwnerAsync(request.OwnerTelegramId, ct);
|
||||||
|
|
||||||
|
return items.Select(c => new CollectionDto(
|
||||||
|
c.Id,
|
||||||
|
c.Name,
|
||||||
|
c.Description,
|
||||||
|
c.Visibility,
|
||||||
|
c.ShareToken,
|
||||||
|
c.MediaItemIds.Count,
|
||||||
|
c.CreatedAt
|
||||||
|
)).ToList();
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user