using Application.Abstractions.Data;
using Application.Abstractions.Messaging;
using Microsoft.EntityFrameworkCore;

namespace Application.Features.Admin.Crypto.News.Source.Update
{
    public sealed class Handler(IAppDbContext db) : ICommandHandler<Command>
    {
        public async Task Handle(Command request, CancellationToken ct)
        {
            var source = await db.RssFeedSource.FirstOrDefaultAsync(x => x.ID == request.ID, ct);

            if (source is null)
            {
                throw new KeyNotFoundException($"RSS 피드 소스를 찾을 수 없습니다. (ID: {request.ID})");
            }

            source.Update(request.Name, request.Url, request.Description, request.IntervalMinutes, request.IsActive);
            await db.SaveChangesAsync(ct);
        }
    }
}