| | | 1 | | using MechanicsSoftware.Domain.Exceptions; |
| | | 2 | | using MechanicsSoftware.Domain.ValueObjects; |
| | | 3 | | |
| | | 4 | | namespace MechanicsSoftware.Domain.Entities; |
| | | 5 | | |
| | | 6 | | public sealed class Vehicle : Entity<Guid> |
| | | 7 | | { |
| | | 8 | | public LicensePlate LicensePlate { get; private set; } = null!; |
| | | 9 | | public string Make { get; private set; } = null!; |
| | | 10 | | public string Model { get; private set; } = null!; |
| | | 11 | | public int Year { get; private set; } |
| | | 12 | | public Guid CustomerId { get; private set; } |
| | | 13 | | |
| | 216 | 14 | | private Vehicle() { } |
| | | 15 | | |
| | | 16 | | public static Vehicle Create( |
| | | 17 | | Guid id, |
| | | 18 | | LicensePlate licensePlate, |
| | | 19 | | string make, |
| | | 20 | | string model, |
| | | 21 | | int year, |
| | | 22 | | Guid customerId) |
| | 88 | 23 | | { |
| | 88 | 24 | | if (licensePlate is null) |
| | 2 | 25 | | throw new DomainException("License plate is required."); |
| | | 26 | | |
| | 86 | 27 | | if (customerId == Guid.Empty) |
| | 2 | 28 | | throw new DomainException("CustomerId is required."); |
| | | 29 | | |
| | 84 | 30 | | var (trimmedMake, trimmedModel) = ValidateFields(make, model, year); |
| | | 31 | | |
| | 72 | 32 | | return new Vehicle |
| | 72 | 33 | | { |
| | 72 | 34 | | Id = id, |
| | 72 | 35 | | LicensePlate = licensePlate, |
| | 72 | 36 | | Make = trimmedMake, |
| | 72 | 37 | | Model = trimmedModel, |
| | 72 | 38 | | Year = year, |
| | 72 | 39 | | CustomerId = customerId |
| | 72 | 40 | | }; |
| | 72 | 41 | | } |
| | | 42 | | |
| | | 43 | | public void Update(string make, string model, int year) |
| | 16 | 44 | | { |
| | 16 | 45 | | var (trimmedMake, trimmedModel) = ValidateFields(make, model, year); |
| | 6 | 46 | | Make = trimmedMake; |
| | 6 | 47 | | Model = trimmedModel; |
| | 6 | 48 | | Year = year; |
| | 6 | 49 | | } |
| | | 50 | | |
| | | 51 | | private static (string Make, string Model) ValidateFields(string make, string model, int year) |
| | 100 | 52 | | { |
| | 100 | 53 | | if (string.IsNullOrWhiteSpace(make)) |
| | 8 | 54 | | throw new DomainException("Make is required."); |
| | | 55 | | |
| | 92 | 56 | | if (string.IsNullOrWhiteSpace(model)) |
| | 8 | 57 | | throw new DomainException("Model is required."); |
| | | 58 | | |
| | 84 | 59 | | var currentYear = DateTime.UtcNow.Year; |
| | 84 | 60 | | if (year < 1886 || year > currentYear + 1) |
| | 6 | 61 | | throw new DomainException($"Year must be between 1886 and {currentYear + 1}."); |
| | | 62 | | |
| | 78 | 63 | | return (make.Trim(), model.Trim()); |
| | 78 | 64 | | } |
| | | 65 | | |
| | | 66 | | public void UpdateLicensePlate(LicensePlate newLicensePlate) |
| | 4 | 67 | | { |
| | 4 | 68 | | if (newLicensePlate is null) |
| | 2 | 69 | | throw new DomainException("License plate is required."); |
| | | 70 | | |
| | 2 | 71 | | LicensePlate = newLicensePlate; |
| | 2 | 72 | | } |
| | | 73 | | } |