books/backend/Books.Api/Domain/Companies/CompanyAggregate.cs
Nicolaj Hartmann 66f6fa138d Initial commit: Books accounting system with EventFlow CQRS
Backend (.NET 10):
- EventFlow CQRS/Event Sourcing with PostgreSQL
- GraphQL.NET API with mutations and queries
- Custom ReadModelSqlGenerator for snake_case PostgreSQL columns
- Hangfire for background job processing
- Integration tests with isolated test databases

Frontend (React/Vite):
- Initial project structure

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-18 02:52:30 +01:00

81 lines
2.3 KiB
C#

using Books.Api.Domain.Companies.Events;
using EventFlow.Aggregates;
namespace Books.Api.Domain.Companies;
public class CompanyAggregate(CompanyId id) : AggregateRoot<CompanyAggregate, CompanyId>(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));
}
}