chore(codebase): full cleanup pass
This commit is contained in:
@@ -5,7 +5,7 @@ namespace Hutopy.Modules.Contents.Data;
|
||||
|
||||
public class Album : Entity
|
||||
{
|
||||
public bool IsDeleted { get; private set; } // private set → EF updates it
|
||||
public bool IsDeleted { get; private set; } // private set → EF updates it
|
||||
[MaxLength(255)] public required string Title { get; set; }
|
||||
public IList<AlbumPhoto> Photos { get; set; } = new List<AlbumPhoto>();
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@ public class ContentsDbContext(
|
||||
modelBuilder
|
||||
.Entity<Album>()
|
||||
.Property(c => c.IsDeleted)
|
||||
.HasComputedColumnSql("\"DeletedAt\" IS NOT NULL", stored: true);
|
||||
.HasComputedColumnSql("\"DeletedAt\" IS NOT NULL", true);
|
||||
|
||||
modelBuilder
|
||||
.Entity<Album>()
|
||||
@@ -40,7 +40,7 @@ public class ContentsDbContext(
|
||||
modelBuilder
|
||||
.Entity<AlbumPhoto>()
|
||||
.Property(c => c.IsDeleted)
|
||||
.HasComputedColumnSql("\"DeletedAt\" IS NOT NULL", stored: true);
|
||||
.HasComputedColumnSql("\"DeletedAt\" IS NOT NULL", true);
|
||||
|
||||
modelBuilder
|
||||
.Entity<AlbumPhoto>()
|
||||
|
||||
@@ -17,9 +17,9 @@ public static class DependencyInjection
|
||||
this IApplicationBuilder app,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
var scopeFactory = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>();
|
||||
using var scope = scopeFactory.CreateScope();
|
||||
await using var context = scope.ServiceProvider.GetRequiredService<ContentsDbContext>();
|
||||
IServiceScopeFactory scopeFactory = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>();
|
||||
using IServiceScope scope = scopeFactory.CreateScope();
|
||||
await using ContentsDbContext context = scope.ServiceProvider.GetRequiredService<ContentsDbContext>();
|
||||
await context.Database.MigrateAsync(cancellationToken);
|
||||
|
||||
return app;
|
||||
|
||||
@@ -23,6 +23,7 @@ public record AddPhotoToAlbumResponse(
|
||||
public sealed class AddPhotoToAlbumRequestValidator : Validator<AddPhotoToAlbumRequest>
|
||||
{
|
||||
private const int MaxFileSizeBytes = 10 * 1024 * 1024; // 10MB
|
||||
|
||||
private static readonly string[] AllowedImageTypes =
|
||||
[
|
||||
"image/jpeg",
|
||||
@@ -74,14 +75,14 @@ public class AddPhotoToAlbumHandler(
|
||||
AddPhotoToAlbumRequest request,
|
||||
CancellationToken ct)
|
||||
{
|
||||
var userId = User.GetUserId();
|
||||
Guid userId = User.GetUserId();
|
||||
|
||||
// Fetch the album we want to add photos to
|
||||
var album = await context
|
||||
Album? album = await context
|
||||
.Albums
|
||||
.SingleOrDefaultAsync(
|
||||
a => a.Id == request.AlbumId && a.CreatedBy == userId,
|
||||
cancellationToken: ct);
|
||||
ct);
|
||||
|
||||
if (album is null)
|
||||
{
|
||||
@@ -90,7 +91,7 @@ public class AddPhotoToAlbumHandler(
|
||||
}
|
||||
|
||||
// Check if a photo with the same ID already exists
|
||||
var existingPhoto = await context
|
||||
bool existingPhoto = await context
|
||||
.AlbumPhotos
|
||||
.AnyAsync(p => p.Id == request.PhotoId, ct);
|
||||
|
||||
@@ -102,16 +103,16 @@ public class AddPhotoToAlbumHandler(
|
||||
|
||||
try
|
||||
{
|
||||
var (originalUrl, thumbnailUrl) = await ProcessAndUploadImage(request, ct);
|
||||
(string originalUrl, string thumbnailUrl) = await ProcessAndUploadImage(request, ct);
|
||||
|
||||
// Get the next order number
|
||||
var nextOrder = await context
|
||||
int nextOrder = await context
|
||||
.AlbumPhotos
|
||||
.Where(p => p.AlbumId == request.AlbumId)
|
||||
.MaxAsync(p => (int?)p.Order, ct) ?? 0;
|
||||
|
||||
// Create the album photo
|
||||
var photo = new AlbumPhoto
|
||||
AlbumPhoto photo = new()
|
||||
{
|
||||
Id = request.PhotoId,
|
||||
CreatedBy = userId,
|
||||
@@ -143,23 +144,23 @@ public class AddPhotoToAlbumHandler(
|
||||
AddPhotoToAlbumRequest request,
|
||||
CancellationToken ct)
|
||||
{
|
||||
var originalFileName = Path.GetFileName(request.File.FileName);
|
||||
var nameWithoutExt = Path.GetFileNameWithoutExtension(originalFileName);
|
||||
var extension = Path.GetExtension(originalFileName);
|
||||
string originalFileName = Path.GetFileName(request.File.FileName);
|
||||
string nameWithoutExt = Path.GetFileNameWithoutExtension(originalFileName);
|
||||
string extension = Path.GetExtension(originalFileName);
|
||||
|
||||
var filenameOriginal = $"{nameWithoutExt}{extension}";
|
||||
var filenameThumbnail = $"{nameWithoutExt}.thumbnail{extension}";
|
||||
string filenameOriginal = $"{nameWithoutExt}{extension}";
|
||||
string filenameThumbnail = $"{nameWithoutExt}.thumbnail{extension}";
|
||||
|
||||
var blobOriginal = $"{SubDirectoryNames.Albums}/{request.AlbumId}/{filenameOriginal}";
|
||||
var blobThumbnail = $"{SubDirectoryNames.Albums}/{request.AlbumId}/{filenameThumbnail}";
|
||||
string blobOriginal = $"{SubDirectoryNames.Albums}/{request.AlbumId}/{filenameOriginal}";
|
||||
string blobThumbnail = $"{SubDirectoryNames.Albums}/{request.AlbumId}/{filenameThumbnail}";
|
||||
|
||||
// Process the original image
|
||||
await using var originalStream = request.File.OpenReadStream();
|
||||
using var image = await Image.LoadAsync(originalStream, ct);
|
||||
await using Stream originalStream = request.File.OpenReadStream();
|
||||
using Image image = await Image.LoadAsync(originalStream, ct);
|
||||
|
||||
// Calculate target size while preserving the original aspect ratio
|
||||
var originalWidth = image.Width;
|
||||
var originalHeight = image.Height;
|
||||
int originalWidth = image.Width;
|
||||
int originalHeight = image.Height;
|
||||
|
||||
double ratioX = (double)MaxThumbnailWidth / originalWidth;
|
||||
double ratioY = (double)MaxThumbnailHeight / originalHeight;
|
||||
@@ -169,20 +170,20 @@ public class AddPhotoToAlbumHandler(
|
||||
int newHeight = (int)(originalHeight * ratio);
|
||||
|
||||
// Create thumbnail
|
||||
using var thumbnailStream = new MemoryStream();
|
||||
using MemoryStream thumbnailStream = new();
|
||||
image.Mutate(x => x.Resize(newWidth, newHeight));
|
||||
await image.SaveAsync(thumbnailStream, image.Metadata.DecodedImageFormat!, ct);
|
||||
thumbnailStream.Position = 0;
|
||||
|
||||
// Upload both versions
|
||||
var originalUrl = await blobStorage.UploadFileAsync(
|
||||
string originalUrl = await blobStorage.UploadFileAsync(
|
||||
ContainerNames.Creators,
|
||||
blobOriginal,
|
||||
request.File.OpenReadStream(),
|
||||
request.File.ContentType,
|
||||
ct);
|
||||
|
||||
var thumbnailUrl = await blobStorage.UploadFileAsync(
|
||||
string thumbnailUrl = await blobStorage.UploadFileAsync(
|
||||
ContainerNames.Creators,
|
||||
blobThumbnail,
|
||||
thumbnailStream,
|
||||
|
||||
@@ -48,7 +48,7 @@ public class CreateAlbumHandler(
|
||||
CancellationToken ct)
|
||||
{
|
||||
// Check if an album with the same ID already exists
|
||||
var existingAlbum = await context
|
||||
bool existingAlbum = await context
|
||||
.Albums
|
||||
.AnyAsync(a => a.Id == request.AlbumId, ct);
|
||||
|
||||
@@ -58,12 +58,7 @@ public class CreateAlbumHandler(
|
||||
return;
|
||||
}
|
||||
|
||||
var album = new Album
|
||||
{
|
||||
Id = request.AlbumId,
|
||||
CreatedBy = User.GetUserId(),
|
||||
Title = request.Title
|
||||
};
|
||||
Album album = new() { Id = request.AlbumId, CreatedBy = User.GetUserId(), Title = request.Title };
|
||||
|
||||
context.Albums.Add(album);
|
||||
await context.SaveChangesAsync(ct);
|
||||
@@ -72,4 +67,4 @@ public class CreateAlbumHandler(
|
||||
new CreateAlbumResponse(album.Id),
|
||||
ct);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,12 +49,12 @@ public class GetAlbumHandler(
|
||||
GetAlbumRequest request,
|
||||
CancellationToken ct)
|
||||
{
|
||||
var album = await context
|
||||
Album? album = await context
|
||||
.Albums
|
||||
.Include(a => a.Photos.OrderBy(p => p.Order))
|
||||
.SingleOrDefaultAsync(
|
||||
a => a.Id == request.AlbumId,
|
||||
cancellationToken: ct);
|
||||
ct);
|
||||
|
||||
if (album is null)
|
||||
{
|
||||
@@ -62,7 +62,7 @@ public class GetAlbumHandler(
|
||||
return;
|
||||
}
|
||||
|
||||
var photos = album.Photos
|
||||
List<AlbumPhotoDto> photos = album.Photos
|
||||
.Select(p => new AlbumPhotoDto(
|
||||
p.Id,
|
||||
p.OriginalUrl,
|
||||
|
||||
@@ -33,14 +33,14 @@ public class RemoveAlbumHandler(
|
||||
RemoveAlbumRequest request,
|
||||
CancellationToken ct)
|
||||
{
|
||||
var userId = User.GetUserId();
|
||||
Guid userId = User.GetUserId();
|
||||
|
||||
var album = await context
|
||||
Album? album = await context
|
||||
.Albums
|
||||
.Include(a => a.Photos)
|
||||
.SingleOrDefaultAsync(
|
||||
a => a.Id == request.AlbumId && a.CreatedBy == userId,
|
||||
cancellationToken: ct);
|
||||
ct);
|
||||
|
||||
if (album is null)
|
||||
{
|
||||
@@ -53,7 +53,7 @@ public class RemoveAlbumHandler(
|
||||
album.DeletedAt = DateTimeOffset.UtcNow;
|
||||
|
||||
// Soft delete all photos in the album
|
||||
foreach (var photo in album.Photos)
|
||||
foreach (AlbumPhoto photo in album.Photos)
|
||||
{
|
||||
photo.DeletedBy = userId;
|
||||
photo.DeletedAt = DateTimeOffset.UtcNow;
|
||||
@@ -63,4 +63,4 @@ public class RemoveAlbumHandler(
|
||||
|
||||
await SendNoContentAsync(ct);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,14 +38,14 @@ public class RemovePhotoFromAlbumHandler(
|
||||
RemovePhotoFromAlbumRequest request,
|
||||
CancellationToken ct)
|
||||
{
|
||||
var userId = User.GetUserId();
|
||||
Guid userId = User.GetUserId();
|
||||
|
||||
var album = await context
|
||||
Album? album = await context
|
||||
.Albums
|
||||
.Include(a => a.Photos)
|
||||
.SingleOrDefaultAsync(
|
||||
a => a.Id == request.AlbumId && a.CreatedBy == userId,
|
||||
cancellationToken: ct);
|
||||
ct);
|
||||
|
||||
if (album is null)
|
||||
{
|
||||
@@ -53,7 +53,7 @@ public class RemovePhotoFromAlbumHandler(
|
||||
return;
|
||||
}
|
||||
|
||||
var photo = album.Photos
|
||||
AlbumPhoto? photo = album.Photos
|
||||
.SingleOrDefault(p => p.Id == request.PhotoId);
|
||||
|
||||
if (photo is null)
|
||||
@@ -65,9 +65,9 @@ public class RemovePhotoFromAlbumHandler(
|
||||
// Soft delete the photo
|
||||
photo.DeletedBy = userId;
|
||||
photo.DeletedAt = DateTimeOffset.UtcNow;
|
||||
|
||||
|
||||
await context.SaveChangesAsync(ct);
|
||||
|
||||
await SendNoContentAsync(ct);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ public class ContentModel
|
||||
public required string CreatedByName { get; init; }
|
||||
public required string? CreatedByPortraitUrl { get; init; }
|
||||
public required DateTimeOffset CreatedAt { get; init; }
|
||||
public Guid? DeletedBy { get; init; }
|
||||
public Guid? DeletedBy { get; init; }
|
||||
public DateTimeOffset? DeletedAt { get; init; }
|
||||
public required string Title { get; init; }
|
||||
public required string Description { get; init; }
|
||||
|
||||
Reference in New Issue
Block a user