using Books.Api.Domain.Companies.Events; using EventFlow.Aggregates; namespace Books.Api.Domain.Companies; public class CompanyAggregate(CompanyId id) : AggregateRoot(id) { private bool _isCreated; public void Apply(CompanyCreatedEvent e) => _isCreated = true; public void Apply(CompanyUpdatedEvent e) { } public void Create( string name, string? cvr, string? address, string? postalCode, string? city, string country, int fiscalYearStartMonth, string currency, bool vatRegistered, string? vatPeriodFrequency) { if (_isCreated) throw new DomainException("Company already exists"); if (string.IsNullOrWhiteSpace(name)) throw new DomainException("Company name is required"); if (fiscalYearStartMonth < 1 || fiscalYearStartMonth > 12) throw new DomainException("Fiscal year start month must be between 1 and 12"); Emit(new CompanyCreatedEvent( name.Trim(), cvr?.Trim(), address?.Trim(), postalCode?.Trim(), city?.Trim(), country, fiscalYearStartMonth, currency, vatRegistered, vatPeriodFrequency)); } public void Update( string name, string? cvr, string? address, string? postalCode, string? city, string country, int fiscalYearStartMonth, string currency, bool vatRegistered, string? vatPeriodFrequency) { if (!_isCreated) throw new DomainException("Company does not exist"); if (string.IsNullOrWhiteSpace(name)) throw new DomainException("Company name is required"); if (fiscalYearStartMonth < 1 || fiscalYearStartMonth > 12) throw new DomainException("Fiscal year start month must be between 1 and 12"); Emit(new CompanyUpdatedEvent( name.Trim(), cvr?.Trim(), address?.Trim(), postalCode?.Trim(), city?.Trim(), country, fiscalYearStartMonth, currency, vatRegistered, vatPeriodFrequency)); } }