using System.Text.Json; using MediatR; using Nashel.BuildingBlocks.Application.Abstractions; using Nashel.Modules.Catalog.Domain.Aggregates; using Nashel.Modules.Catalog.Domain.Repositories; using Nashel.Modules.Catalog.Domain.ValueObjects; namespace Nashel.Modules.Catalog.Application.Commands; public record UpdateOfferCommand( Guid OfferId, string Title, string Description, decimal PriceAmount, int PriceType, // 0-Fixed, 1-Hourly, 2-Negotiable Dictionary? Attributes, List? Images) : IRequest; public class UpdateOfferCommandHandler : IRequestHandler { private readonly IOfferRepository _repository; private readonly ICurrentUserService _currentUser; public UpdateOfferCommandHandler(IOfferRepository repository, ICurrentUserService currentUser) { _repository = repository; _currentUser = currentUser; } public async Task Handle(UpdateOfferCommand request, CancellationToken cancellationToken) { var offer = await _repository.GetByIdAsync(request.OfferId, cancellationToken); if (offer == null) throw new Exception("Offer not found"); if (offer.PerformerId != _currentUser.UserId) throw new UnauthorizedAccessException("Not your offer"); var price = new Price(request.PriceAmount, (Nashel.Modules.Catalog.Domain.Enums.OfferType)request.PriceType); var jsonAttrs = request.Attributes != null && request.Attributes.Count > 0 ? JsonDocument.Parse(JsonSerializer.Serialize(request.Attributes)) : null; offer.Update(request.Title, request.Description, price, jsonAttrs, request.Images); await _repository.UpdateAsync(offer, cancellationToken); return true; } }