using Application.Abstractions.Data; using Application.Abstractions.Messaging; using Domain.Entities.Feed; using Microsoft.EntityFrameworkCore; using SharedKernel.Results; namespace Application.Features.Api.Feed.ToggleBookmark; public sealed class Handler(IAppDbContext db) : ICommandHandler> { public async Task> Handle(Command request, CancellationToken ct) { var post = await db.FeedPost.FirstOrDefaultAsync(p => p.ID == request.PostID && !p.IsDeleted, ct); if (post is null) { return Result.Failure(Error.NotFound("Feed.PostNotFound", "게시글을 찾을 수 없습니다.")); } var existing = await db.FeedBookmark.FirstOrDefaultAsync(b => b.FeedPostID == request.PostID && b.MemberID == request.MemberID, ct); bool hasBookmark; if (existing is null) { await db.FeedBookmark.AddAsync(new FeedBookmark { FeedPostID = request.PostID, MemberID = request.MemberID }, ct); post.Bookmarks++; hasBookmark = true; } else { db.FeedBookmark.Remove(existing); post.Bookmarks = post.Bookmarks > 0 ? post.Bookmarks - 1 : 0; hasBookmark = false; } await db.SaveChangesAsync(ct); return new Response(hasBookmark, post.Bookmarks); } }