-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Styled them a tad better, too - Fixed not being able to edit shelves despite being logged-in as the owner
- Loading branch information
Showing
21 changed files
with
412 additions
and
462 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.