Skip to content

Commit

Permalink
Moved shelves to Immediate.Apis
Browse files Browse the repository at this point in the history
- Styled them a tad better, too
- Fixed not being able to edit shelves despite being logged-in as the owner
  • Loading branch information
Atulin committed Aug 16, 2024
1 parent 28632f4 commit a8580dc
Show file tree
Hide file tree
Showing 21 changed files with 412 additions and 462 deletions.
77 changes: 0 additions & 77 deletions Ogma3/Api/V1/Shelves/Commands/CreateShelf.cs

This file was deleted.

39 changes: 0 additions & 39 deletions Ogma3/Api/V1/Shelves/Commands/DeleteShelf.cs

This file was deleted.

77 changes: 0 additions & 77 deletions Ogma3/Api/V1/Shelves/Commands/UpdateShelf.cs

This file was deleted.

85 changes: 85 additions & 0 deletions Ogma3/Api/V1/Shelves/CreateShelf.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
using FluentValidation;
using Immediate.Apis.Shared;
using Immediate.Handlers.Shared;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http.HttpResults;
using Ogma3.Data;
using Ogma3.Data.Shelves;
using Ogma3.Infrastructure.Extensions;
using Ogma3.Services.UserService;

namespace Ogma3.Api.V1.Shelves;

using ReturnType = Results<CreatedAtRoute<ShelfDto>, UnauthorizedHttpResult>;

[Handler]
[MapPost("api/shelves")]
[Authorize]
public static partial class CreateShelf
{
public sealed record Command
(
string Name,
string Description,
bool IsQuickAdd,
bool IsPublic,
bool TrackUpdates,
string Color,
long Icon
);

public class CommandValidator : AbstractValidator<Command>
{
public CommandValidator()
{
RuleFor(s => s.Name)
.MaximumLength(CTConfig.CShelf.MaxNameLength)
.MinimumLength(CTConfig.CShelf.MinNameLength);
RuleFor(s => s.Description)
.MaximumLength(CTConfig.CShelf.MaxDescriptionLength);
RuleFor(s => s.Color)
.Length(7);
}
}

private static async ValueTask<ReturnType> HandleAsync(
Command request,
ApplicationDbContext context,
IUserService userService,
CancellationToken cancellationToken
)
{
if (userService.User?.GetNumericId() is not {} uid) return TypedResults.Unauthorized();

var shelf = new Shelf
{
Name = request.Name,
Description = request.Description,
IsQuickAdd = request.IsQuickAdd,
IsPublic = request.IsPublic,
TrackUpdates = request.TrackUpdates,
Color = request.Color,
IconId = request.Icon,
OwnerId = uid,
};

context.Shelves.Add(shelf);
await context.SaveChangesAsync(cancellationToken);

var dto = new ShelfDto
{
Id = shelf.Id,
Name = shelf.Name,
Description = shelf.Description,
IsDefault = shelf.IsDefault,
IsPublic = shelf.IsPublic,
IsQuickAdd = shelf.IsQuickAdd,
TrackUpdates = shelf.TrackUpdates,
StoriesCount = 0,
IconName = shelf.Icon?.Name,
IconId = 0,
};

return TypedResults.CreatedAtRoute(dto, nameof(GetShelf), new GetShelf.Query(shelf.Id));
}
}
37 changes: 37 additions & 0 deletions Ogma3/Api/V1/Shelves/DeleteShelf.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using Immediate.Apis.Shared;
using Immediate.Handlers.Shared;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.EntityFrameworkCore;
using Ogma3.Data;
using Ogma3.Infrastructure.Extensions;
using Ogma3.Services.UserService;

namespace Ogma3.Api.V1.Shelves;

using ReturnType = Results<UnauthorizedHttpResult, Ok<long>, NotFound>;

[Handler]
[MapDelete("api/shelves/{shelfId:long}")]
[Authorize]
public static partial class DeleteShelf
{
public sealed record Command(long ShelfId);

private static async ValueTask<ReturnType> HandleAsync(
Command request,
ApplicationDbContext context,
IUserService userService,
CancellationToken cancellationToken
)
{
if (userService.User?.GetNumericId() is not {} uid) return TypedResults.Unauthorized();

var res = await context.Shelves
.Where(s => s.Id == request.ShelfId)
.Where(s => s.OwnerId == uid)
.ExecuteDeleteAsync(cancellationToken);

return res > 0 ? TypedResults.Ok(request.ShelfId) : TypedResults.NotFound();
}
}
41 changes: 41 additions & 0 deletions Ogma3/Api/V1/Shelves/GetPaginatedUserShelves.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using Immediate.Apis.Shared;
using Immediate.Handlers.Shared;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.EntityFrameworkCore;
using Ogma3.Data;
using Ogma3.Data.Shelves;
using Ogma3.Infrastructure.Extensions;
using Ogma3.Services.UserService;

namespace Ogma3.Api.V1.Shelves;

using ReturnType = Results<Ok<ShelfDto[]>, UnauthorizedHttpResult>;

[Handler]
[MapGet("api/shelves/{userName:alpha}")]
[Authorize]
public static partial class GetPaginatedUserShelves
{
public sealed record Query(string UserName, int Page);

private static async ValueTask<ReturnType> HandleAsync(
Query request,
ApplicationDbContext context,
IUserService userService,
OgmaConfig config,
CancellationToken cancellationToken
)
{
if (userService.User?.GetNumericId() is not {} uid) return TypedResults.Unauthorized();

var shelves = await context.Shelves
.Where(s => s.Owner.NormalizedUserName == request.UserName.Normalize().ToUpperInvariant())
.Where(s => s.OwnerId == uid || s.IsPublic)
.Paginate(request.Page, config.ShelvesPerPage)
.ProjectToDto()
.ToArrayAsync(cancellationToken);

return TypedResults.Ok(shelves);
}
}
Loading

0 comments on commit a8580dc

Please sign in to comment.