mirror of
https://github.com/meysamhadeli/booking-microservices.git
synced 2026-04-15 05:25:36 +08:00
Merge pull request #269 from meysamhadeli/refactor/refactor-booking-value-objects-validation
refactor: Refactor booking value objects validation
This commit is contained in:
commit
0b47559441
@ -0,0 +1,11 @@
|
||||
namespace Booking.Booking.Exceptions;
|
||||
|
||||
using BuildingBlocks.Exception;
|
||||
|
||||
public class InvalidAircraftIdException : BadRequestException
|
||||
{
|
||||
public InvalidAircraftIdException(Guid aircraftId)
|
||||
: base($"aircraftId: '{aircraftId}' is invalid.")
|
||||
{
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,11 @@
|
||||
namespace Booking.Booking.Exceptions;
|
||||
|
||||
using BuildingBlocks.Exception;
|
||||
|
||||
public class InvalidArriveAirportIdException : BadRequestException
|
||||
{
|
||||
public InvalidArriveAirportIdException(Guid arriveAirportId)
|
||||
: base($"arriveAirportId: '{arriveAirportId}' is invalid.")
|
||||
{
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,11 @@
|
||||
namespace Booking.Booking.Exceptions;
|
||||
|
||||
using BuildingBlocks.Exception;
|
||||
|
||||
public class InvalidDepartureAirportIdException : BadRequestException
|
||||
{
|
||||
public InvalidDepartureAirportIdException(Guid departureAirportId)
|
||||
: base($"departureAirportId: '{departureAirportId}' is invalid.")
|
||||
{
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,11 @@
|
||||
namespace Booking.Booking.Exceptions;
|
||||
|
||||
using BuildingBlocks.Exception;
|
||||
|
||||
public class InvalidFlightDateException : BadRequestException
|
||||
{
|
||||
public InvalidFlightDateException(DateTime flightDate)
|
||||
: base($"Flight Date: '{flightDate}' is invalid.")
|
||||
{
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,10 @@
|
||||
namespace Booking.Booking.Exceptions;
|
||||
|
||||
using BuildingBlocks.Exception;
|
||||
public class InvalidFlightNumberException : BadRequestException
|
||||
{
|
||||
public InvalidFlightNumberException(string flightNumber)
|
||||
: base($"Flight Number: '{flightNumber}' is invalid.")
|
||||
{
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,11 @@
|
||||
namespace Booking.Booking.Exceptions;
|
||||
|
||||
using BuildingBlocks.Exception;
|
||||
|
||||
public class InvalidPassengerNameException : BadRequestException
|
||||
{
|
||||
public InvalidPassengerNameException(string passengerName)
|
||||
: base($"Passenger Name: '{passengerName}' is invalid.")
|
||||
{
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,12 @@
|
||||
namespace Booking.Booking.Exceptions;
|
||||
|
||||
using BuildingBlocks.Exception;
|
||||
|
||||
public class InvalidPriceException : BadRequestException
|
||||
{
|
||||
public InvalidPriceException(decimal price)
|
||||
: base($"Price: '{price}' must be grater than or equal 0.")
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,12 @@
|
||||
namespace Booking.Booking.Exceptions;
|
||||
|
||||
using BuildingBlocks.Exception;
|
||||
|
||||
public class SeatNumberException : BadRequestException
|
||||
{
|
||||
public SeatNumberException(string seatNumber)
|
||||
: base($"Seat Number: '{seatNumber}' is invalid.")
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
@ -18,8 +18,8 @@ using MediatR;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Routing;
|
||||
using Models.ValueObjects;
|
||||
using Passenger;
|
||||
using ValueObjects;
|
||||
|
||||
public record CreateBooking(Guid PassengerId, Guid FlightId, string Description) : ICommand<CreateBookingResult>,
|
||||
IInternalCommand
|
||||
@ -122,7 +122,7 @@ internal class CreateBookingCommandHandler : ICommandHandler<CreateBooking, Crea
|
||||
throw new BookingAlreadyExistException();
|
||||
}
|
||||
|
||||
var aggrigate = Models.Booking.Create(command.Id, new PassengerInfo(passenger.PassengerDto?.Name), new Trip(
|
||||
var aggrigate = Models.Booking.Create(command.Id, PassengerInfo.Of(passenger.PassengerDto?.Name), Trip.Of(
|
||||
flight.FlightDto.FlightNumber, new Guid(flight.FlightDto.AircraftId),
|
||||
new Guid(flight.FlightDto.DepartureAirportId),
|
||||
new Guid(flight.FlightDto.ArriveAirportId), flight.FlightDto.FlightDate.ToDateTime(),
|
||||
|
||||
@ -1,9 +1,9 @@
|
||||
using Booking.Booking.Models.ValueObjects;
|
||||
using BuildingBlocks.EventStoreDB.Events;
|
||||
|
||||
namespace Booking.Booking.Models;
|
||||
|
||||
using Features.CreatingBook.Commands.V1;
|
||||
using ValueObjects;
|
||||
|
||||
public record Booking : AggregateEventSourcing<Guid>
|
||||
{
|
||||
|
||||
@ -1,3 +0,0 @@
|
||||
namespace Booking.Booking.Models.ValueObjects;
|
||||
|
||||
public record PassengerInfo(string Name);
|
||||
@ -1,4 +0,0 @@
|
||||
namespace Booking.Booking.Models.ValueObjects;
|
||||
|
||||
public record Trip(string FlightNumber, Guid AircraftId, Guid DepartureAirportId, Guid ArriveAirportId,
|
||||
DateTime FlightDate, decimal Price, string Description, string SeatNumber);
|
||||
@ -0,0 +1,23 @@
|
||||
namespace Booking.Booking.ValueObjects;
|
||||
|
||||
using Exceptions;
|
||||
|
||||
public record PassengerInfo
|
||||
{
|
||||
public string Name { get; }
|
||||
|
||||
private PassengerInfo(string name)
|
||||
{
|
||||
Name = name;
|
||||
}
|
||||
|
||||
public static PassengerInfo Of(string name)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(name))
|
||||
{
|
||||
throw new InvalidPassengerNameException(name);
|
||||
}
|
||||
|
||||
return new PassengerInfo(name);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,68 @@
|
||||
namespace Booking.Booking.ValueObjects;
|
||||
|
||||
using Exceptions;
|
||||
|
||||
public record Trip
|
||||
{
|
||||
public string FlightNumber { get; }
|
||||
public Guid AircraftId { get; }
|
||||
public Guid DepartureAirportId { get; }
|
||||
public Guid ArriveAirportId { get; }
|
||||
public DateTime FlightDate { get; }
|
||||
public decimal Price { get; }
|
||||
public string Description { get; }
|
||||
public string SeatNumber { get; }
|
||||
|
||||
private Trip(string flightNumber, Guid aircraftId, Guid departureAirportId, Guid arriveAirportId,
|
||||
DateTime flightDate, decimal price, string description, string seatNumber)
|
||||
{
|
||||
FlightNumber = flightNumber;
|
||||
AircraftId = aircraftId;
|
||||
DepartureAirportId = departureAirportId;
|
||||
ArriveAirportId = arriveAirportId;
|
||||
FlightDate = flightDate;
|
||||
Price = price;
|
||||
Description = description;
|
||||
SeatNumber = seatNumber;
|
||||
}
|
||||
|
||||
public static Trip Of(string flightNumber, Guid aircraftId, Guid departureAirportId, Guid arriveAirportId,
|
||||
DateTime flightDate, decimal price, string description, string seatNumber)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(flightNumber))
|
||||
{
|
||||
throw new InvalidFlightNumberException(flightNumber);
|
||||
}
|
||||
|
||||
if (aircraftId == Guid.Empty)
|
||||
{
|
||||
throw new InvalidAircraftIdException(aircraftId);
|
||||
}
|
||||
|
||||
if (departureAirportId == Guid.Empty)
|
||||
{
|
||||
throw new InvalidDepartureAirportIdException(departureAirportId);
|
||||
}
|
||||
|
||||
if (arriveAirportId == Guid.Empty)
|
||||
{
|
||||
throw new InvalidArriveAirportIdException(departureAirportId);
|
||||
}
|
||||
|
||||
if (flightDate == default)
|
||||
{
|
||||
throw new InvalidFlightDateException(flightDate);
|
||||
}
|
||||
|
||||
if(price < 0){
|
||||
throw new InvalidPriceException(price);
|
||||
}
|
||||
|
||||
if (string.IsNullOrWhiteSpace(seatNumber))
|
||||
{
|
||||
throw new SeatNumberException(seatNumber);
|
||||
}
|
||||
|
||||
return new Trip(flightNumber, aircraftId, departureAirportId, arriveAirportId, flightDate, price, description, seatNumber);
|
||||
}
|
||||
}
|
||||
@ -3,9 +3,9 @@ using System;
|
||||
using BuildingBlocks.Exception;
|
||||
|
||||
|
||||
public class InvalidAircraftIdExceptions : BadRequestException
|
||||
public class InvalidAircraftIdException : BadRequestException
|
||||
{
|
||||
public InvalidAircraftIdExceptions(Guid aircraftId)
|
||||
public InvalidAircraftIdException(Guid aircraftId)
|
||||
: base($"AircraftId: '{aircraftId}' is invalid.")
|
||||
{
|
||||
}
|
||||
@ -16,9 +16,9 @@ public record AircraftId
|
||||
{
|
||||
if (value == Guid.Empty)
|
||||
{
|
||||
throw new InvalidAircraftIdExceptions(value);
|
||||
throw new InvalidAircraftIdException(value);
|
||||
}
|
||||
|
||||
|
||||
return new AircraftId(value);
|
||||
}
|
||||
|
||||
|
||||
@ -2,9 +2,9 @@ namespace Flight.Airports.Exceptions;
|
||||
using System;
|
||||
using BuildingBlocks.Exception;
|
||||
|
||||
public class InvalidAirportIdExceptions : BadRequestException
|
||||
public class InvalidAirportIdException : BadRequestException
|
||||
{
|
||||
public InvalidAirportIdExceptions(Guid airportId)
|
||||
public InvalidAirportIdException(Guid airportId)
|
||||
: base($"airportId: '{airportId}' is invalid.")
|
||||
{
|
||||
}
|
||||
@ -16,7 +16,7 @@ public record AirportId
|
||||
{
|
||||
if (value == Guid.Empty)
|
||||
{
|
||||
throw new InvalidAirportIdExceptions(value);
|
||||
throw new InvalidAirportIdException(value);
|
||||
}
|
||||
|
||||
return new AirportId(value);
|
||||
|
||||
@ -2,9 +2,9 @@ namespace Flight.Flights.Exceptions;
|
||||
using System;
|
||||
using BuildingBlocks.Exception;
|
||||
|
||||
public class InvalidArriveDateExceptions : BadRequestException
|
||||
public class InvalidArriveDateException : BadRequestException
|
||||
{
|
||||
public InvalidArriveDateExceptions(DateTime arriveDate)
|
||||
public InvalidArriveDateException(DateTime arriveDate)
|
||||
: base($"Arrive Date: '{arriveDate}' is invalid.")
|
||||
{
|
||||
}
|
||||
@ -2,9 +2,9 @@ namespace Flight.Flights.Exceptions;
|
||||
using System;
|
||||
using BuildingBlocks.Exception;
|
||||
|
||||
public class InvalidDepartureDateExceptions : BadRequestException
|
||||
public class InvalidDepartureDateException : BadRequestException
|
||||
{
|
||||
public InvalidDepartureDateExceptions(DateTime departureDate)
|
||||
public InvalidDepartureDateException(DateTime departureDate)
|
||||
: base($"Departure Date: '{departureDate}' is invalid.")
|
||||
{
|
||||
}
|
||||
@ -2,9 +2,9 @@ namespace Flight.Flights.Exceptions;
|
||||
using System;
|
||||
using BuildingBlocks.Exception;
|
||||
|
||||
public class InvalidFlightDateExceptions : BadRequestException
|
||||
public class InvalidFlightDateException : BadRequestException
|
||||
{
|
||||
public InvalidFlightDateExceptions(DateTime flightDate)
|
||||
public InvalidFlightDateException(DateTime flightDate)
|
||||
: base($"Flight Date: '{flightDate}' is invalid.")
|
||||
{
|
||||
}
|
||||
@ -2,9 +2,9 @@ namespace Flight.Flights.Exceptions;
|
||||
using System;
|
||||
using BuildingBlocks.Exception;
|
||||
|
||||
public class InvalidFlightIdExceptions : BadRequestException
|
||||
public class InvalidFlightIdException : BadRequestException
|
||||
{
|
||||
public InvalidFlightIdExceptions(Guid flightId)
|
||||
public InvalidFlightIdException(Guid flightId)
|
||||
: base($"flightId: '{flightId}' is invalid.")
|
||||
{
|
||||
}
|
||||
@ -15,7 +15,7 @@ public record ArriveDate
|
||||
{
|
||||
if (value == default)
|
||||
{
|
||||
throw new InvalidArriveDateExceptions(value);
|
||||
throw new InvalidArriveDateException(value);
|
||||
}
|
||||
|
||||
return new ArriveDate(value);
|
||||
|
||||
@ -16,9 +16,9 @@ public record DepartureDate
|
||||
{
|
||||
if (value == default)
|
||||
{
|
||||
throw new InvalidDepartureDateExceptions(value);
|
||||
throw new InvalidDepartureDateException(value);
|
||||
}
|
||||
|
||||
|
||||
return new DepartureDate(value);
|
||||
}
|
||||
|
||||
|
||||
@ -15,9 +15,9 @@ public record FlightDate
|
||||
{
|
||||
if (value == default)
|
||||
{
|
||||
throw new InvalidFlightDateExceptions(value);
|
||||
throw new InvalidFlightDateException(value);
|
||||
}
|
||||
|
||||
|
||||
return new FlightDate(value);
|
||||
}
|
||||
|
||||
|
||||
@ -16,7 +16,7 @@ public record FlightId
|
||||
{
|
||||
if (value == Guid.Empty)
|
||||
{
|
||||
throw new InvalidFlightIdExceptions(value);
|
||||
throw new InvalidFlightIdException(value);
|
||||
}
|
||||
|
||||
return new FlightId(value);
|
||||
|
||||
@ -3,9 +3,9 @@ using System;
|
||||
using BuildingBlocks.Exception;
|
||||
|
||||
|
||||
public class InvalidSeatIdExceptions : BadRequestException
|
||||
public class InvalidSeatIdException : BadRequestException
|
||||
{
|
||||
public InvalidSeatIdExceptions(Guid seatId)
|
||||
public InvalidSeatIdException(Guid seatId)
|
||||
: base($"seatId: '{seatId}' is invalid.")
|
||||
{
|
||||
}
|
||||
@ -16,7 +16,7 @@ public record SeatId
|
||||
{
|
||||
if (value == Guid.Empty)
|
||||
{
|
||||
throw new InvalidSeatIdExceptions(value);
|
||||
throw new InvalidSeatIdException(value);
|
||||
}
|
||||
|
||||
return new SeatId(value);
|
||||
|
||||
@ -3,9 +3,9 @@ using System;
|
||||
|
||||
using BuildingBlocks.Exception;
|
||||
|
||||
public class InvalidPassengerIdExceptions : BadRequestException
|
||||
public class InvalidPassengerIdException : BadRequestException
|
||||
{
|
||||
public InvalidPassengerIdExceptions(Guid passengerId)
|
||||
public InvalidPassengerIdException(Guid passengerId)
|
||||
: base($"PassengerId: '{passengerId}' is invalid.")
|
||||
{
|
||||
}
|
||||
@ -16,7 +16,7 @@ public record PassengerId
|
||||
{
|
||||
if (value == Guid.Empty)
|
||||
{
|
||||
throw new InvalidPassengerIdExceptions(value);
|
||||
throw new InvalidPassengerIdException(value);
|
||||
}
|
||||
|
||||
return new PassengerId(value);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user