Skip to content

Commit

Permalink
Comment move preparations, partial ratings move
Browse files Browse the repository at this point in the history
- Preemptively made comment handlers use primary constructors to make move to `Immediate.Apis` easier
- Partially moved ratings to `Immediate.Apis`, blocked by RicoSuter/NSwag#4626
  • Loading branch information
Atulin committed Aug 30, 2024
1 parent 388f2ba commit 17ae78d
Show file tree
Hide file tree
Showing 31 changed files with 451 additions and 600 deletions.
28 changes: 9 additions & 19 deletions Ogma3/Api/V1/Comments/Commands/CreateBlogpostComment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,34 +28,24 @@ public CommandValidator()
}
}

public class Handler : BaseHandler, IRequestHandler<Command, ActionResult<CommentDto>>
public class Handler(ApplicationDbContext context, IMapper mapper, NotificationsRepository notificationsRepo, IUserService userService)
: BaseHandler, IRequestHandler<Command, ActionResult<CommentDto>>
{
private readonly ApplicationDbContext _context;
private readonly IMapper _mapper;
private readonly NotificationsRepository _notificationsRepo;
private readonly long? _uid;

public Handler(ApplicationDbContext context, IMapper mapper, NotificationsRepository notificationsRepo, IUserService userService)
{
_context = context;
_mapper = mapper;
_notificationsRepo = notificationsRepo;
_uid = userService.User?.GetNumericId();
}
private readonly long? _uid = userService.User?.GetNumericId();

public async ValueTask<ActionResult<CommentDto>> Handle(Command request, CancellationToken cancellationToken)
{
if (_uid is null) return Unauthorized();

// Check if user is muted
var isMuted = await _context.Infractions
var isMuted = await context.Infractions
.Where(i => i.UserId == _uid && i.Type == InfractionType.Mute)
.AnyAsync(cancellationToken);
if (isMuted) return Unauthorized();

var (body, threadId) = request;

var thread = await _context.CommentThreads
var thread = await context.CommentThreads
.Where(ct => ct.Id == threadId)
.FirstOrDefaultAsync(cancellationToken);

Expand All @@ -69,21 +59,21 @@ public async ValueTask<ActionResult<CommentDto>> Handle(Command request, Cancell
CommentsThreadId = threadId,
};

_context.Comments.Add(comment);
context.Comments.Add(comment);
thread.CommentsCount++;

await _context.SaveChangesAsync(cancellationToken);
await context.SaveChangesAsync(cancellationToken);

if (thread.UserId != _uid)
{
await _notificationsRepo.NotifyUsers(thread.Id, comment.Id, comment.Body.Truncate(50), cancellationToken);
await notificationsRepo.NotifyUsers(thread.Id, comment.Id, comment.Body.Truncate(50), cancellationToken);
}

return CreatedAtAction(
nameof(CommentsController.GetComment),
nameof(CommentsController)[..^10],
new { comment.Id },
_mapper.Map<Comment, CommentDto>(comment)
mapper.Map<Comment, CommentDto>(comment)
);
}
}
Expand Down
28 changes: 9 additions & 19 deletions Ogma3/Api/V1/Comments/Commands/CreateChapterComment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,34 +28,24 @@ public CommandValidator()
}
}

public class Handler : BaseHandler, IRequestHandler<Command, ActionResult<CommentDto>>
public class Handler(ApplicationDbContext context, IMapper mapper, NotificationsRepository notificationsRepo, IUserService userService)
: BaseHandler, IRequestHandler<Command, ActionResult<CommentDto>>
{
private readonly ApplicationDbContext _context;
private readonly IMapper _mapper;
private readonly NotificationsRepository _notificationsRepo;
private readonly long? _uid;

public Handler(ApplicationDbContext context, IMapper mapper, NotificationsRepository notificationsRepo, IUserService userService)
{
_context = context;
_mapper = mapper;
_notificationsRepo = notificationsRepo;
_uid = userService.User?.GetNumericId();
}
private readonly long? _uid = userService.User?.GetNumericId();

public async ValueTask<ActionResult<CommentDto>> Handle(Command request, CancellationToken cancellationToken)
{
if (_uid is null) return Unauthorized();

// Check if user is muted
var isMuted = await _context.Infractions
var isMuted = await context.Infractions
.Where(i => i.UserId == _uid && i.Type == InfractionType.Mute)
.AnyAsync(cancellationToken);
if (isMuted) return Unauthorized();

var (body, threadId) = request;

var thread = await _context.CommentThreads
var thread = await context.CommentThreads
.Where(ct => ct.Id == threadId)
.FirstOrDefaultAsync(cancellationToken);

Expand All @@ -69,21 +59,21 @@ public async ValueTask<ActionResult<CommentDto>> Handle(Command request, Cancell
CommentsThreadId = threadId,
};

_context.Comments.Add(comment);
context.Comments.Add(comment);
thread.CommentsCount++;

await _context.SaveChangesAsync(cancellationToken);
await context.SaveChangesAsync(cancellationToken);

if (thread.UserId != _uid)
{
await _notificationsRepo.NotifyUsers(thread.Id, comment.Id, comment.Body.Truncate(50), cancellationToken);
await notificationsRepo.NotifyUsers(thread.Id, comment.Id, comment.Body.Truncate(50), cancellationToken);
}

return CreatedAtAction(
nameof(CommentsController.GetComment),
nameof(CommentsController)[..^10],
new { comment.Id },
_mapper.Map<Comment, CommentDto>(comment)
mapper.Map<Comment, CommentDto>(comment)
);
}
}
Expand Down
37 changes: 14 additions & 23 deletions Ogma3/Api/V1/Comments/Commands/CreateClubThreadComment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,38 +28,29 @@ public CommandValidator()
}
}

public class Handler : BaseHandler, IRequestHandler<Command, ActionResult<CommentDto>>
public class Handler
(
ApplicationDbContext context,
IMapper mapper,
NotificationsRepository notificationsRepo,
IUserService userService)
: BaseHandler, IRequestHandler<Command, ActionResult<CommentDto>>
{
private readonly ApplicationDbContext _context;
private readonly IMapper _mapper;
private readonly NotificationsRepository _notificationsRepo;
private readonly long? _uid;

public Handler(
ApplicationDbContext context,
IMapper mapper,
NotificationsRepository notificationsRepo,
IUserService userService
) {
_context = context;
_mapper = mapper;
_notificationsRepo = notificationsRepo;
_uid = userService.User?.GetNumericId();
}
private readonly long? _uid = userService.User?.GetNumericId();

public async ValueTask<ActionResult<CommentDto>> Handle(Command request, CancellationToken cancellationToken)
{
if (_uid is null) return Unauthorized();

// Check if user is muted
var isMuted = await _context.Infractions
var isMuted = await context.Infractions
.Where(i => i.UserId == _uid && i.Type == InfractionType.Mute)
.AnyAsync(cancellationToken);
if (isMuted) return Unauthorized();

var (body, threadId) = request;

var thread = await _context.CommentThreads
var thread = await context.CommentThreads
.Where(ct => ct.Id == threadId)
.FirstOrDefaultAsync(cancellationToken);

Expand All @@ -73,21 +64,21 @@ public async ValueTask<ActionResult<CommentDto>> Handle(Command request, Cancell
CommentsThreadId = threadId,
};

_context.Comments.Add(comment);
context.Comments.Add(comment);
thread.CommentsCount++;

await _context.SaveChangesAsync(cancellationToken);
await context.SaveChangesAsync(cancellationToken);

if (thread.UserId != _uid)
{
await _notificationsRepo.NotifyUsers(thread.Id, comment.Id, comment.Body.Truncate(50), cancellationToken);
await notificationsRepo.NotifyUsers(thread.Id, comment.Id, comment.Body.Truncate(50), cancellationToken);
}

return CreatedAtAction(
nameof(CommentsController.GetComment),
nameof(CommentsController)[..^10],
new { comment.Id },
_mapper.Map<Comment, CommentDto>(comment)
mapper.Map<Comment, CommentDto>(comment)
);
}
}
Expand Down
39 changes: 15 additions & 24 deletions Ogma3/Api/V1/Comments/Commands/CreateProfileComment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,46 +28,37 @@ public CommandValidator()
}
}

public class Handler : BaseHandler, IRequestHandler<Command, ActionResult<CommentDto>>
public class Handler
(
ApplicationDbContext context,
IMapper mapper,
NotificationsRepository notificationsRepo,
IUserService userService)
: BaseHandler, IRequestHandler<Command, ActionResult<CommentDto>>
{
private readonly ApplicationDbContext _context;
private readonly IMapper _mapper;
private readonly NotificationsRepository _notificationsRepo;
private readonly long? _uid;

public Handler(
ApplicationDbContext context,
IMapper mapper,
NotificationsRepository notificationsRepo,
IUserService userService
) {
_context = context;
_mapper = mapper;
_notificationsRepo = notificationsRepo;
_uid = userService.User?.GetNumericId();
}
private readonly long? _uid = userService.User?.GetNumericId();

public async ValueTask<ActionResult<CommentDto>> Handle(Command request, CancellationToken cancellationToken)
{
if (_uid is null) return Unauthorized();

// Check if user is muted
var isMuted = await _context.Infractions
var isMuted = await context.Infractions
.Where(i => i.UserId == _uid && i.Type == InfractionType.Mute)
.AnyAsync(cancellationToken);
if (isMuted) return Unauthorized();

var (body, threadId) = request;

var thread = await _context.CommentThreads
var thread = await context.CommentThreads
.Where(ct => ct.Id == threadId)
.FirstOrDefaultAsync(cancellationToken);

if (thread is null) return NotFound();
if (thread.LockDate is not null) return Unauthorized();

// Check if comment author is blocked by the profile owner
var isBlocked = await _context.BlacklistedUsers
var isBlocked = await context.BlacklistedUsers
.Where(b => b.BlockingUserId == thread.UserId && b.BlockedUserId == _uid)
.AnyAsync(cancellationToken);

Expand All @@ -80,21 +71,21 @@ public async ValueTask<ActionResult<CommentDto>> Handle(Command request, Cancell
CommentsThreadId = threadId,
};

_context.Comments.Add(comment);
context.Comments.Add(comment);
thread.CommentsCount++;

await _context.SaveChangesAsync(cancellationToken);
await context.SaveChangesAsync(cancellationToken);

if (thread.UserId != _uid)
{
await _notificationsRepo.NotifyUsers(thread.Id, comment.Id, comment.Body.Truncate(50), cancellationToken);
await notificationsRepo.NotifyUsers(thread.Id, comment.Id, comment.Body.Truncate(50), cancellationToken);
}

return CreatedAtAction(
nameof(CommentsController.GetComment),
nameof(CommentsController)[..^10],
new { comment.Id },
_mapper.Map<Comment, CommentDto>(comment)
mapper.Map<Comment, CommentDto>(comment)
);
}
}
Expand Down
15 changes: 4 additions & 11 deletions Ogma3/Api/V1/Comments/Commands/DeleteComment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,15 @@ public static class DeleteComment
{
public sealed record Command(long Id) : IRequest<ActionResult<long>>;

public class Handler : BaseHandler, IRequestHandler<Command, ActionResult<long>>
public class Handler(ApplicationDbContext context, IUserService userService) : BaseHandler, IRequestHandler<Command, ActionResult<long>>
{
private readonly ApplicationDbContext _context;
private readonly long? _uid;

public Handler(ApplicationDbContext context, IUserService userService)
{
_context = context;
_uid = userService.User?.GetNumericId();
}
private readonly long? _uid = userService.User?.GetNumericId();

public async ValueTask<ActionResult<long>> Handle(Command request, CancellationToken cancellationToken)
{
if (_uid is null) return Unauthorized();

var comment = await _context.Comments
var comment = await context.Comments
.Where(c => c.Id == request.Id)
.Include(c => c.Revisions)
.FirstOrDefaultAsync(cancellationToken);
Expand All @@ -46,7 +39,7 @@ public async ValueTask<ActionResult<long>> Handle(Command request, CancellationT
// Wipe revisions
comment.Revisions.Clear();

await _context.SaveChangesAsync(cancellationToken);
await context.SaveChangesAsync(cancellationToken);

return Ok(comment.Id);
}
Expand Down
22 changes: 7 additions & 15 deletions Ogma3/Api/V1/Comments/Commands/UpdateComment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,34 +25,26 @@ public CommandValidator()
}
}

public class Handler : BaseHandler, IRequestHandler<Command, ActionResult<CommentDto>>
public class Handler(ApplicationDbContext context, IUserService userService, IMapper mapper)
: BaseHandler, IRequestHandler<Command, ActionResult<CommentDto>>
{
private readonly ApplicationDbContext _context;
private readonly long? _uid;
private readonly IMapper _mapper;

public Handler(ApplicationDbContext context, IUserService userService, IMapper mapper)
{
_context = context;
_mapper = mapper;
_uid = userService.User?.GetNumericId();
}
private readonly long? _uid = userService.User?.GetNumericId();

public async ValueTask<ActionResult<CommentDto>> Handle(Command request, CancellationToken cancellationToken)
{
if (_uid is null) return Unauthorized();

var (body, commentId) = request;

var comm = await _context.Comments
var comm = await context.Comments
.Where(c => c.Id == commentId)
.Where(c => c.AuthorId == _uid)
.FirstOrDefaultAsync(cancellationToken);

if (comm is null) return NotFound();

// Create revision
_context.CommentRevisions.Add(new CommentRevision
context.CommentRevisions.Add(new CommentRevision
{
Body = comm.Body,
ParentId = comm.Id,
Expand All @@ -63,9 +55,9 @@ public async ValueTask<ActionResult<CommentDto>> Handle(Command request, Cancell
comm.LastEdit = DateTime.Now;
comm.EditCount += 1;

await _context.SaveChangesAsync(cancellationToken);
await context.SaveChangesAsync(cancellationToken);

var dto = _mapper.Map<Comment, CommentDto>(comm);
var dto = mapper.Map<Comment, CommentDto>(comm);
dto.Owned = _uid == comm.AuthorId;

return dto;
Expand Down
Loading

0 comments on commit 17ae78d

Please sign in to comment.