| | | 1 | | using Microsoft.EntityFrameworkCore; |
| | | 2 | | using Microsoft.EntityFrameworkCore.Metadata.Builders; |
| | | 3 | | using MechanicsSoftware.Domain.Entities; |
| | | 4 | | using MechanicsSoftware.Domain.ValueObjects; |
| | | 5 | | using MechanicsSoftware.Domain.Enums; |
| | | 6 | | using MechanicsSoftware.Domain.Exceptions; |
| | | 7 | | |
| | | 8 | | namespace MechanicsSoftware.Infrastructure.Persistence.Configurations; |
| | | 9 | | |
| | 1 | 10 | | public sealed class CustomerConfiguration : IEntityTypeConfiguration<Customer> |
| | 1 | 11 | | { |
| | | 12 | | public void Configure(EntityTypeBuilder<Customer> builder) |
| | 3 | 13 | | { |
| | 3 | 14 | | builder.ToTable("customers"); |
| | | 15 | | |
| | 3 | 16 | | builder.HasKey(c => c.Id); |
| | 3 | 17 | | builder.Property(c => c.Id).HasColumnName("id"); |
| | 1 | 18 | | |
| | 3 | 19 | | builder.Property(c => c.Name) |
| | 2 | 20 | | .HasColumnName("name") |
| | 3 | 21 | | .HasMaxLength(100) |
| | 3 | 22 | | .IsRequired(); |
| | 1 | 23 | | |
| | 3 | 24 | | builder.Property(c => c.Phone) |
| | 2 | 25 | | .HasColumnName("phone") |
| | 2 | 26 | | .HasMaxLength(20) |
| | 3 | 27 | | .IsRequired(); |
| | 1 | 28 | | |
| | 1 | 29 | | // TaxId — PersonType is inferred from digit count (11=CPF/INDIVIDUAL, 14=CNPJ/COMPANY) |
| | 3 | 30 | | builder.Property(c => c.Document) |
| | 3 | 31 | | .HasConversion( |
| | 3 | 32 | | v => v.Value, |
| | 3 | 33 | | v => new TaxId(v, v.Length == 11 ? PersonType.INDIVIDUAL : PersonType.COMPANY)) |
| | 2 | 34 | | .HasColumnName("document") |
| | 3 | 35 | | .HasMaxLength(14) |
| | 3 | 36 | | .IsRequired(); |
| | 1 | 37 | | |
| | 3 | 38 | | builder.Property(c => c.Email) |
| | 3 | 39 | | .HasConversion( |
| | 3 | 40 | | v => v.Value, |
| | 3 | 41 | | v => new Email(v)) |
| | 2 | 42 | | .HasColumnName("email") |
| | 3 | 43 | | .HasMaxLength(200) |
| | 3 | 44 | | .IsRequired(); |
| | 1 | 45 | | |
| | 2 | 46 | | builder.HasIndex(c => c.Document) |
| | 3 | 47 | | .IsUnique() |
| | 3 | 48 | | .HasDatabaseName("ix_customers_document"); |
| | 1 | 49 | | |
| | 3 | 50 | | builder.HasIndex(c => c.Email) |
| | 2 | 51 | | .IsUnique() |
| | 2 | 52 | | .HasDatabaseName("ix_customers_email"); |
| | 2 | 53 | | } |
| | | 54 | | } |