using Application.Abstractions.Messaging; using Application.Abstractions.Data; using SharedKernel.Storage; using Microsoft.EntityFrameworkCore; namespace Application.Features.Forum.Post.Create; public sealed class Handler(IAppDbContext db, IFileStorage fileStorage) : ICommandHandler { public async Task Handle(Command request, CancellationToken ct) { if (!await db.Board.AnyAsync(x => x.ID == request.BoardID, ct)) { throw new KeyNotFoundException("게시판을 찾을 수 없습니다."); } var post = new Domain.Entities.Forum.Posts.Post { BoardID = request.BoardID, MemberID = null, Subject = request.Subject, Content = request.Content ?? string.Empty, IsNotice = request.IsNotice, IsSecret = request.IsSecret, IsAnonymous = request.IsAnonymous, Name = "관리자" }; await db.Post.AddAsync(post, ct); await db.SaveChangesAsync(ct); if (request.ThumbnailFile is not null) { FileStoragePath uploadPath = new(UploadTarget.Upload, UploadFolder.Post, post.ID); string[] allowedFileExtensions = [".jpg", ".jpeg", ".png", ".gif", ".webp", ".bmp"]; var result = await fileStorage.SaveFileAsync(request.ThumbnailFile, uploadPath, allowedFileExtensions, ct); post.Thumbnail = result?.Url; await db.SaveChangesAsync(ct); } // Board 게시글 카운트 증가 var board = await db.Board.FirstOrDefaultAsync(x => x.ID == request.BoardID, ct); if (board is not null) { board.Posts++; board.UpdatedAt = DateTime.UtcNow; // BoardGroup 게시글 카운트 증가 var boardGroup = await db.BoardGroup.FirstOrDefaultAsync(x => x.ID == board.BoardGroupID, ct); if (boardGroup is not null) { boardGroup.Posts++; boardGroup.UpdatedAt = DateTime.UtcNow; } await db.SaveChangesAsync(ct); } } }