Skip to content

Commit

Permalink
Merge pull request #266 from amirhossein18121380/AddValueObject(Seats…
Browse files Browse the repository at this point in the history
…/Flights/Airports)

AddValueObjects(Seats/Flights/Airports)
  • Loading branch information
meysamhadeli authored Jun 11, 2023
2 parents d324e87 + 188a121 commit 7348c2c
Show file tree
Hide file tree
Showing 56 changed files with 2,058 additions and 231 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Flight.Airports.Exceptions;
using BuildingBlocks.Exception;

public class InvalidAddressException : BadRequestException
{
public InvalidAddressException() : base("Address cannot be empty or whitespace.")
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace Flight.Airports.Exceptions;
using System;
using BuildingBlocks.Exception;

public class InvalidAirportIdExceptions : BadRequestException
{
public InvalidAirportIdExceptions(Guid airportId)
: base($"airportId: '{airportId}' is invalid.")
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Flight.Airports.Exceptions;
using BuildingBlocks.Exception;


public class InvalidCodeException : BadRequestException
{
public InvalidCodeException() : base("Code cannot be empty or whitespace.")
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Flight.Airports.Exceptions;
using BuildingBlocks.Exception;


public class InvalidNameException : BadRequestException
{
public InvalidNameException() : base("Name cannot be empty or whitespace.")
{
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
namespace Flight.Airports.Features;
namespace Flight.Airports.Features;

using CreatingAirport.V1;
using Models;
using Mapster;
using MassTransit;
using Models;

public class AirportMappings : IRegister
{
Expand All @@ -15,7 +15,7 @@ public void Register(TypeAdapterConfig config)

config.NewConfig<Airport, AirportReadModel>()
.Map(d => d.Id, s => NewId.NextGuid())
.Map(d => d.AirportId, s => s.Id);
.Map(d => d.AirportId, s => s.Id.Value);

config.NewConfig<CreateAirportRequestDto, CreateAirport>()
.ConstructUsing(x => new CreateAirport(x.Name, x.Address, x.Code));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ namespace Flight.Airports.Features.CreatingAirport.V1;
using BuildingBlocks.Core.CQRS;
using BuildingBlocks.Core.Event;
using BuildingBlocks.Web;
using Exceptions;
using Data;
using Duende.IdentityServer.EntityFramework.Entities;
using Exceptions;
using FluentValidation;
using MapsterMapper;
using MassTransit;
Expand All @@ -18,6 +18,7 @@ namespace Flight.Airports.Features.CreatingAirport.V1;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
using Microsoft.EntityFrameworkCore;
using ValueObjects;

public record CreateAirport(string Name, string Address, string Code) : ICommand<CreateAirportResult>, IInternalCommand
{
Expand Down Expand Up @@ -87,17 +88,17 @@ public async Task<CreateAirportResult> Handle(CreateAirport request, Cancellatio
Guard.Against.Null(request, nameof(request));

var airport =
await _flightDbContext.Airports.SingleOrDefaultAsync(x => x.Code == request.Code, cancellationToken);
await _flightDbContext.Airports.SingleOrDefaultAsync(x => x.Code.Value == request.Code, cancellationToken);

if (airport is not null)
{
throw new AirportAlreadyExistException();
}

var airportEntity = Models.Airport.Create(request.Id, request.Name, request.Code, request.Address);
var airportEntity = Models.Airport.Create(AirportId.Of(request.Id), Name.Of(request.Name), Address.Of(request.Address), Code.Of(request.Code));

var newAirport = (await _flightDbContext.Airports.AddAsync(airportEntity, cancellationToken))?.Entity;

return new CreateAirportResult(newAirport.Id);
return new CreateAirportResult(newAirport.Id.Value);
}
}
20 changes: 10 additions & 10 deletions src/Services/Flight/src/Flight/Airports/Models/Airport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@

namespace Flight.Airports.Models;

using System;
using Features.CreatingAirport.V1;
using Flight.Airports.ValueObjects;

public record Airport : Aggregate<Guid>
public record Airport : Aggregate<AirportId>
{
public string Name { get; private set; }
public string Address { get; private set; }
public string Code { get; private set; }
public Name Name { get; private set; } = default!;
public Address Address { get; private set; } = default!;
public Code Code { get; private set; }

public static Airport Create(Guid id, string name, string address, string code, bool isDeleted = false)
public static Airport Create(AirportId id, Name name, Address address, Code code, bool isDeleted = false)
{
var airport = new Airport
{
Expand All @@ -22,10 +22,10 @@ public static Airport Create(Guid id, string name, string address, string code,
};

var @event = new AirportCreatedDomainEvent(
airport.Id,
airport.Name,
airport.Address,
airport.Code,
airport.Id.Value,
airport.Name.Value,
airport.Address.Value,
airport.Code.Value,
isDeleted);

airport.AddDomainEvent(@event);
Expand Down
43 changes: 43 additions & 0 deletions src/Services/Flight/src/Flight/Airports/ValueObjects/Address.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
namespace Flight.Airports.ValueObjects;
using System.Linq;
using Exceptions;

public class Address
{
public string Street { get; }
public string City { get; }
public string State { get; }
public string ZipCode { get; }
public string Country { get; }
public string Value { get; }

public Address(string value)
{
if (string.IsNullOrWhiteSpace(value))
{
throw new InvalidAddressException();
}
Value = value;
}

public Address(string street, string city, string state, string zipCode, string country)
{
Street = street?.Trim();
City = city?.Trim();
State = state?.Trim();
ZipCode = zipCode?.Trim();
Country = country?.Trim();

Value = string.Join(", ", new[] { Street, City, State, ZipCode, Country }.Where(s => !string.IsNullOrWhiteSpace(s)));
}

public static Address Of(string value)
{
return new Address(value);
}

public static implicit operator string(Address address)
{
return address.Value;
}
}
28 changes: 28 additions & 0 deletions src/Services/Flight/src/Flight/Airports/ValueObjects/AirportId.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
namespace Flight.Airports.ValueObjects;
using System;
using Flight.Airports.Exceptions;

public record AirportId
{
public Guid Value { get; }

private AirportId(Guid value)
{
if (value == Guid.Empty)
{
throw new InvalidAirportIdExceptions(value);
}

Value = value;
}

public static AirportId Of(Guid value)
{
return new AirportId(value);
}

public static implicit operator Guid(AirportId airportId)
{
return airportId.Value;
}
}
25 changes: 25 additions & 0 deletions src/Services/Flight/src/Flight/Airports/ValueObjects/Code.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
namespace Flight.Airports.ValueObjects;

using Exceptions;

public record Code
{
public string Value { get; }
public Code(string value)
{
if (string.IsNullOrWhiteSpace(value))
{
throw new InvalidCodeException();
}
Value = value;
}
public static Code Of(string value)
{
return new Code(value);
}

public static implicit operator string(Code code)
{
return code.Value;
}
}
25 changes: 25 additions & 0 deletions src/Services/Flight/src/Flight/Airports/ValueObjects/Name.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
namespace Flight.Airports.ValueObjects;

using Flight.Airports.Exceptions;

public record Name
{
public string Value { get; }
public Name(string value)
{
if (string.IsNullOrWhiteSpace(value))
{
throw new InvalidNameException();
}
Value = value;
}
public static Name Of(string value)
{
return new Name(value);
}

public static implicit operator string(Name name)
{
return name.Value;
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,57 @@
using System;
using Flight.Airports.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace Flight.Data.Configurations;

public class AirportConfiguration: IEntityTypeConfiguration<Airport>
using Airports.ValueObjects;

public class AirportConfiguration : IEntityTypeConfiguration<Airport>
{
public void Configure(EntityTypeBuilder<Airport> builder)
{

builder.ToTable(nameof(Airport));

builder.HasKey(r => r.Id);
builder.Property(r => r.Id).ValueGeneratedNever();


builder.Property(r => r.Id).ValueGeneratedNever()
.HasConversion<Guid>(airportId => airportId.Value, dbId => AirportId.Of(dbId));
// // ref: https://learn.microsoft.com/en-us/ef/core/saving/concurrency?tabs=fluent-api
builder.Property(r => r.Version).IsConcurrencyToken();


builder.OwnsOne(
x => x.Name,
a =>
{
a.Property(p => p.Value)
.HasColumnName(nameof(Airport.Name))
.HasMaxLength(50)
.IsRequired();
}
);

builder.OwnsOne(
x => x.Address,
a =>
{
a.Property(p => p.Value)
.HasColumnName(nameof(Airport.Address))
.HasMaxLength(50)
.IsRequired();
}
);

builder.OwnsOne(
x => x.Code,
a =>
{
a.Property(p => p.Value)
.HasColumnName(nameof(Airport.Code))
.HasMaxLength(50)
.IsRequired();
}
);
}
}
Loading

0 comments on commit 7348c2c

Please sign in to comment.