diff --git a/src/MemeVault.Api/Controllers/CollectionsController.cs b/src/MemeVault.Api/Controllers/CollectionsController.cs new file mode 100644 index 0000000..181d3b3 --- /dev/null +++ b/src/MemeVault.Api/Controllers/CollectionsController.cs @@ -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 GetAll(CancellationToken ct) + { + var result = await mediator.Send(new GetUserCollectionsQuery(currentUser.TelegramId), ct); + return Ok(result); + } + + // POST /api/collections + [HttpPost] + public async Task 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 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 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 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 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 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); diff --git a/src/MemeVault.Api/MemeVault.Api.csproj b/src/MemeVault.Api/MemeVault.Api.csproj index a497713..f49fe20 100644 --- a/src/MemeVault.Api/MemeVault.Api.csproj +++ b/src/MemeVault.Api/MemeVault.Api.csproj @@ -25,6 +25,7 @@ + diff --git a/src/MemeVault.Api/Program.cs b/src/MemeVault.Api/Program.cs index 63ed42f..72daf2f 100644 --- a/src/MemeVault.Api/Program.cs +++ b/src/MemeVault.Api/Program.cs @@ -5,6 +5,8 @@ using MemeVault.Api.Options; using MemeVault.Infrastructure; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.IdentityModel.Tokens; +using MemeVault.Infrastructure.Persistence; +using Microsoft.AspNetCore.Diagnostics.HealthChecks; using Prometheus; var builder = WebApplication.CreateBuilder(args); @@ -53,6 +55,10 @@ builder.Services.AddExceptionHandler(); builder.Services.AddAuthorization(); builder.Services.AddControllers(); +// ── OpenAPI ─────────────────────────────────────────────────────────────────── +builder.Services.AddHealthChecks() + .AddDbContextCheck("postgres"); + // ── OpenAPI ─────────────────────────────────────────────────────────────────── builder.Services.AddEndpointsApiExplorer(); @@ -63,7 +69,7 @@ app.UseHttpMetrics(); app.UseAuthentication(); app.UseAuthorization(); -app.MapGet("/health", () => Results.Ok(new { status = "ok" })); +app.MapHealthChecks("/health"); app.MapControllers(); app.MapMetrics(); diff --git a/src/MemeVault.Application/Commands/DeleteCollection/DeleteCollectionCommand.cs b/src/MemeVault.Application/Commands/DeleteCollection/DeleteCollectionCommand.cs new file mode 100644 index 0000000..8174e16 --- /dev/null +++ b/src/MemeVault.Application/Commands/DeleteCollection/DeleteCollectionCommand.cs @@ -0,0 +1,8 @@ +using MediatR; + +namespace MemeVault.Application.Commands.DeleteCollection; + +public sealed record DeleteCollectionCommand( + Guid CollectionId, + long RequestingTelegramId +) : IRequest; diff --git a/src/MemeVault.Application/Commands/DeleteCollection/DeleteCollectionCommandHandler.cs b/src/MemeVault.Application/Commands/DeleteCollection/DeleteCollectionCommandHandler.cs new file mode 100644 index 0000000..b2629a9 --- /dev/null +++ b/src/MemeVault.Application/Commands/DeleteCollection/DeleteCollectionCommandHandler.cs @@ -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 +{ + public async Task 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; + } +} diff --git a/src/MemeVault.Application/Commands/ManageCollectionItem/ManageCollectionItemCommand.cs b/src/MemeVault.Application/Commands/ManageCollectionItem/ManageCollectionItemCommand.cs new file mode 100644 index 0000000..249038f --- /dev/null +++ b/src/MemeVault.Application/Commands/ManageCollectionItem/ManageCollectionItemCommand.cs @@ -0,0 +1,15 @@ +using MediatR; + +namespace MemeVault.Application.Commands.ManageCollectionItem; + +public sealed record AddItemToCollectionCommand( + Guid CollectionId, + Guid MediaItemId, + long RequestingTelegramId +) : IRequest; + +public sealed record RemoveItemFromCollectionCommand( + Guid CollectionId, + Guid MediaItemId, + long RequestingTelegramId +) : IRequest; diff --git a/src/MemeVault.Application/Commands/ManageCollectionItem/ManageCollectionItemCommandHandler.cs b/src/MemeVault.Application/Commands/ManageCollectionItem/ManageCollectionItemCommandHandler.cs new file mode 100644 index 0000000..8ed9084 --- /dev/null +++ b/src/MemeVault.Application/Commands/ManageCollectionItem/ManageCollectionItemCommandHandler.cs @@ -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 +{ + public async Task 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 +{ + public async Task 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; + } +} diff --git a/src/MemeVault.Application/Commands/UpdateCollection/UpdateCollectionCommand.cs b/src/MemeVault.Application/Commands/UpdateCollection/UpdateCollectionCommand.cs new file mode 100644 index 0000000..ac5dea7 --- /dev/null +++ b/src/MemeVault.Application/Commands/UpdateCollection/UpdateCollectionCommand.cs @@ -0,0 +1,10 @@ +using MediatR; + +namespace MemeVault.Application.Commands.UpdateCollection; + +public sealed record UpdateCollectionCommand( + Guid CollectionId, + long RequestingTelegramId, + string Name, + string? Description +) : IRequest; diff --git a/src/MemeVault.Application/Commands/UpdateCollection/UpdateCollectionCommandHandler.cs b/src/MemeVault.Application/Commands/UpdateCollection/UpdateCollectionCommandHandler.cs new file mode 100644 index 0000000..8c224f9 --- /dev/null +++ b/src/MemeVault.Application/Commands/UpdateCollection/UpdateCollectionCommandHandler.cs @@ -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 +{ + public async Task 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; + } +} diff --git a/src/MemeVault.Application/Queries/GetUserCollections/GetUserCollectionsQuery.cs b/src/MemeVault.Application/Queries/GetUserCollections/GetUserCollectionsQuery.cs new file mode 100644 index 0000000..4d6da90 --- /dev/null +++ b/src/MemeVault.Application/Queries/GetUserCollections/GetUserCollectionsQuery.cs @@ -0,0 +1,17 @@ +using MediatR; +using MemeVault.Domain.Enums; + +namespace MemeVault.Application.Queries.GetUserCollections; + +public sealed record GetUserCollectionsQuery(long OwnerTelegramId) + : IRequest>; + +public sealed record CollectionDto( + Guid Id, + string Name, + string? Description, + CollectionVisibility Visibility, + string? ShareToken, + int ItemCount, + DateTimeOffset CreatedAt +); diff --git a/src/MemeVault.Application/Queries/GetUserCollections/GetUserCollectionsQueryHandler.cs b/src/MemeVault.Application/Queries/GetUserCollections/GetUserCollectionsQueryHandler.cs new file mode 100644 index 0000000..0975157 --- /dev/null +++ b/src/MemeVault.Application/Queries/GetUserCollections/GetUserCollectionsQueryHandler.cs @@ -0,0 +1,25 @@ +using MediatR; +using MemeVault.Domain.Repositories; + +namespace MemeVault.Application.Queries.GetUserCollections; + +public sealed class GetUserCollectionsQueryHandler(ICollectionRepository collections) + : IRequestHandler> +{ + public async Task> 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(); + } +}