| 123456789101112131415161718192021222324252627282930313233343536373839 |
- using Application.Abstractions.Data;
- using Application.Abstractions.Messaging;
- using Microsoft.EntityFrameworkCore;
- using SharedKernel.Results;
- namespace Application.Features.Api.ChannelTitle.UpdateChannelTitle;
- internal sealed class Handler(IAppDbContext db) : ICommandHandler<Command, Result>
- {
- public async Task<Result> Handle(Command request, CancellationToken ct)
- {
- var title = await db.ChannelTitle.Include(t => t.Channel).FirstOrDefaultAsync(t => t.ID == request.TitleID, ct);
- if (title is null)
- {
- return Result.Failure(Error.NotFound("ChannelTitle.NotFound", "칭호를 찾을 수 없습니다."));
- }
- if (title.Channel is null || title.Channel.MemberID != request.MemberID)
- {
- return Result.Failure(Error.Forbidden("ChannelTitle.Forbidden", "본인 채널의 칭호만 수정할 수 있습니다."));
- }
- if (request.MinAmount < 0)
- {
- return Result.Failure(Error.Problem("ChannelTitle.InvalidAmount", "최소 금액은 0원 이상이어야 합니다."));
- }
- var duplicateAmount = await db.ChannelTitle.AnyAsync(t => t.ChannelID == title.ChannelID && t.ID != request.TitleID && t.MinAmount == request.MinAmount, ct);
- if (duplicateAmount)
- {
- return Result.Failure(Error.Conflict("ChannelTitle.DuplicateAmount", "같은 최소 금액의 칭호가 이미 존재합니다."));
- }
- title.Update(request.Name, request.Description, request.MinAmount, request.Color, request.IconUrl, request.IsActive);
- await db.SaveChangesAsync(ct);
- return Result.Success();
- }
- }
|