Add editorconfig and analyzers, format backend code
This commit is contained in:
@@ -7,37 +7,36 @@ using Musify.Application.Tracks.Responses;
|
||||
using X.PagedList;
|
||||
using X.PagedList.EF;
|
||||
|
||||
namespace Musify.Application.Likes
|
||||
namespace Musify.Application.Likes;
|
||||
|
||||
public record GetLikedTracksQuery(
|
||||
long UserId,
|
||||
int PageNumber,
|
||||
int PageSize)
|
||||
: IQuery<ErrorOr<PaginatedResponse<TrackApplicationResponse>>>;
|
||||
|
||||
public class GetLikedTracksQueryHandler(IDatabase database)
|
||||
: IQueryHandler<GetLikedTracksQuery, ErrorOr<PaginatedResponse<TrackApplicationResponse>>>
|
||||
{
|
||||
public record GetLikedTracksQuery(
|
||||
long UserId,
|
||||
int PageNumber,
|
||||
int PageSize)
|
||||
: IQuery<ErrorOr<PaginatedResponse<TrackApplicationResponse>>>;
|
||||
|
||||
public class GetLikedTracksQueryHandler(IDatabase database)
|
||||
: IQueryHandler<GetLikedTracksQuery, ErrorOr<PaginatedResponse<TrackApplicationResponse>>>
|
||||
public async ValueTask<ErrorOr<PaginatedResponse<TrackApplicationResponse>>> Handle(GetLikedTracksQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
public async ValueTask<ErrorOr<PaginatedResponse<TrackApplicationResponse>>> Handle(GetLikedTracksQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
var likesQuery = database.TrackLikes
|
||||
.AsNoTracking()
|
||||
.Include(like => like.Track.Owner)
|
||||
.Include(like => like.Track.Tags)
|
||||
.Where(like => like.UserId == request.UserId);
|
||||
var likesQuery = database.TrackLikes
|
||||
.AsNoTracking()
|
||||
.Include(like => like.Track.Owner)
|
||||
.Include(like => like.Track.Tags)
|
||||
.Where(like => like.UserId == request.UserId);
|
||||
|
||||
var totalCount = await likesQuery.CountAsync(cancellationToken);
|
||||
var totalCount = await likesQuery.CountAsync(cancellationToken);
|
||||
|
||||
var pagedEntities = await likesQuery
|
||||
.OrderByDescending(like => like.CreatedAt)
|
||||
.Select(like => new { like.Track, ListensCount = like.Track.ListeningHistories.Count(l => l.IsCounted) })
|
||||
.ToPagedListAsync(request.PageNumber, request.PageSize, totalCount, cancellationToken);
|
||||
var pagedEntities = await likesQuery
|
||||
.OrderByDescending(like => like.CreatedAt)
|
||||
.Select(like => new { like.Track, ListensCount = like.Track.ListeningHistories.Count(l => l.IsCounted) })
|
||||
.ToPagedListAsync(request.PageNumber, request.PageSize, totalCount, cancellationToken);
|
||||
|
||||
var pagedTracks = new StaticPagedList<TrackApplicationResponse>(
|
||||
pagedEntities.Select(x => TrackApplicationResponse.FromEntity(x.Track, x.ListensCount)),
|
||||
pagedEntities.PageNumber, pagedEntities.PageSize, pagedEntities.TotalItemCount);
|
||||
var pagedTracks = new StaticPagedList<TrackApplicationResponse>(
|
||||
pagedEntities.Select(x => TrackApplicationResponse.FromEntity(x.Track, x.ListensCount)),
|
||||
pagedEntities.PageNumber, pagedEntities.PageSize, pagedEntities.TotalItemCount);
|
||||
|
||||
return PaginatedResponse<TrackApplicationResponse>.FromPagedList(pagedTracks);
|
||||
}
|
||||
return PaginatedResponse<TrackApplicationResponse>.FromPagedList(pagedTracks);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,52 +5,51 @@ using Microsoft.Extensions.Logging;
|
||||
using Musify.Application.Contracts;
|
||||
using Musify.Domain.Entities;
|
||||
|
||||
namespace Musify.Application.Likes
|
||||
namespace Musify.Application.Likes;
|
||||
|
||||
public record ToggleTrackLikeCommand(long UserId, Guid TrackId)
|
||||
: ICommand<ErrorOr<bool>>;
|
||||
|
||||
public class ToggleTrackLikeCommandHandler(
|
||||
IDatabase database,
|
||||
ILogger<ToggleTrackLikeCommandHandler> logger)
|
||||
: ICommandHandler<ToggleTrackLikeCommand, ErrorOr<bool>>
|
||||
{
|
||||
public record ToggleTrackLikeCommand(long UserId, Guid TrackId)
|
||||
: ICommand<ErrorOr<bool>>;
|
||||
|
||||
public class ToggleTrackLikeCommandHandler(
|
||||
IDatabase database,
|
||||
ILogger<ToggleTrackLikeCommandHandler> logger)
|
||||
: ICommandHandler<ToggleTrackLikeCommand, ErrorOr<bool>>
|
||||
public async ValueTask<ErrorOr<bool>> Handle(ToggleTrackLikeCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
public async ValueTask<ErrorOr<bool>> Handle(ToggleTrackLikeCommand request, CancellationToken cancellationToken)
|
||||
var trackExists = await database.Tracks
|
||||
.AsNoTracking()
|
||||
.AnyAsync(t => t.Id == request.TrackId, cancellationToken);
|
||||
if (!trackExists)
|
||||
{
|
||||
var trackExists = await database.Tracks
|
||||
.AsNoTracking()
|
||||
.AnyAsync(t => t.Id == request.TrackId, cancellationToken);
|
||||
if (!trackExists)
|
||||
{
|
||||
logger.LogInformation("Track {TrackId} not found", request.TrackId);
|
||||
return Error.NotFound(description: $"Track {request.TrackId} not found");
|
||||
}
|
||||
|
||||
var existingLike = await database.TrackLikes
|
||||
.SingleOrDefaultAsync(like => like.UserId == request.UserId && like.TrackId == request.TrackId, cancellationToken);
|
||||
|
||||
bool nowLiked;
|
||||
if (existingLike != null)
|
||||
{
|
||||
database.TrackLikes.Remove(existingLike);
|
||||
nowLiked = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
await database.TrackLikes.AddAsync(new TrackLike
|
||||
{
|
||||
UserId = request.UserId,
|
||||
TrackId = request.TrackId
|
||||
}, cancellationToken);
|
||||
nowLiked = true;
|
||||
}
|
||||
|
||||
await database.SaveChangesAsync(cancellationToken);
|
||||
|
||||
logger.LogInformation("User {UserId} {Action} track {TrackId}",
|
||||
request.UserId, nowLiked ? "liked" : "unliked", request.TrackId);
|
||||
|
||||
return nowLiked;
|
||||
logger.LogInformation("Track {TrackId} not found", request.TrackId);
|
||||
return Error.NotFound(description: $"Track {request.TrackId} not found");
|
||||
}
|
||||
|
||||
var existingLike = await database.TrackLikes
|
||||
.SingleOrDefaultAsync(like => like.UserId == request.UserId && like.TrackId == request.TrackId, cancellationToken);
|
||||
|
||||
bool nowLiked;
|
||||
if (existingLike != null)
|
||||
{
|
||||
database.TrackLikes.Remove(existingLike);
|
||||
nowLiked = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
await database.TrackLikes.AddAsync(new TrackLike
|
||||
{
|
||||
UserId = request.UserId,
|
||||
TrackId = request.TrackId
|
||||
}, cancellationToken);
|
||||
nowLiked = true;
|
||||
}
|
||||
|
||||
await database.SaveChangesAsync(cancellationToken);
|
||||
|
||||
logger.LogInformation("User {UserId} {Action} track {TrackId}",
|
||||
request.UserId, nowLiked ? "liked" : "unliked", request.TrackId);
|
||||
|
||||
return nowLiked;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user