Skip to content

Commit

Permalink
Implement async crud operations in repositories all the way up till p…
Browse files Browse the repository at this point in the history
…resentation
  • Loading branch information
izzat5233 committed Apr 1, 2024
1 parent cb37321 commit c04c3cb
Show file tree
Hide file tree
Showing 30 changed files with 87 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ namespace AirportTicketBookingSystem.Test.Infrastructure.Service;
public class AirportServiceTests
{
[Theory, AutoMoqData]
public void Add_ShouldCallRepository(
public async Task AddAsync_ShouldCallRepository(
Airport entity,
[Frozen] Mock<IAirportRepository> repositoryMock,
AirportService service)
{
service.Add(entity);
repositoryMock.Verify(r => r.Add(entity), Times.Once);
await service.AddAsync(entity);
repositoryMock.Verify(r => r.AddAsync(entity), Times.Once);
}

[Theory, AutoMoqData]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,33 @@ namespace AirportTicketBookingSystem.Test.Infrastructure.Service;
public class BookingServiceTests
{
[Theory, AutoMoqData]
public void Add_ShouldCallRepository(
public async Task AddAsync_ShouldCallRepository(
Booking entity,
[Frozen] Mock<IBookingRepository> repositoryMock,
BookingService service)
{
service.Add(entity);
repositoryMock.Verify(r => r.Add(entity), Times.Once);
await service.AddAsync(entity);
repositoryMock.Verify(r => r.AddAsync(entity), Times.Once);
}

[Theory, AutoMoqData]
public void Update_ShouldCallRepository(
public async Task UpdateAsync_ShouldCallRepository(
Booking entity,
[Frozen] Mock<IBookingRepository> repositoryMock,
BookingService service)
{
service.Update(entity);
repositoryMock.Verify(r => r.Update(entity), Times.Once);
await service.UpdateAsync(entity);
repositoryMock.Verify(r => r.UpdateAsync(entity), Times.Once);
}

[Theory, AutoMoqData]
public void Delete_ShouldCallRepository(
public async Task DeleteAsync_ShouldCallRepository(
Booking entity,
[Frozen] Mock<IBookingRepository> repositoryMock,
BookingService service)
{
service.Delete(entity);
repositoryMock.Verify(r => r.Delete(entity), Times.Once);
await service.DeleteAsync(entity);
repositoryMock.Verify(r => r.DeleteAsync(entity), Times.Once);
}

[Theory, AutoMoqData]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ namespace AirportTicketBookingSystem.Test.Infrastructure.Service;
public class FlightServiceTests
{
[Theory, AutoMoqData]
public void Add_ShouldCallRepository(
public async Task AddAsync_ShouldCallRepository(
Flight entity,
[Frozen] Mock<IFlightRepository> repositoryMock,
FlightService service)
{
service.Add(entity);
repositoryMock.Verify(r => r.Add(entity), Times.Once);
await service.AddAsync(entity);
repositoryMock.Verify(r => r.AddAsync(entity), Times.Once);
}

[Theory, AutoMoqData]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ namespace AirportTicketBookingSystem.Test.Infrastructure.Service;
public class PassengerServiceTests
{
[Theory, AutoMoqData]
public void Add_ShouldCallRepository(
public async Task AddAsync_ShouldCallRepository(
Passenger entity,
[Frozen] Mock<IPassengerRepository> repositoryMock,
PassengerService service)
{
service.Add(entity);
repositoryMock.Verify(r => r.Add(entity), Times.Once);
await service.AddAsync(entity);
repositoryMock.Verify(r => r.AddAsync(entity), Times.Once);
}

[Theory, AutoMoqData]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ public interface IBookingManagementService
/// <returns>A SearchResult containing a collection of bookings for the specified passenger.</returns>
SearchResult<Booking> GetAllBookings(int passengerId);

OperationResult<Booking> AddBooking(Booking booking);
Task<OperationResult<Booking>> AddBookingAsync(Booking booking);

OperationResult<Booking> UpdateBooking(Booking updatedBooking);
Task<OperationResult<Booking>> UpdateBookingAsync(Booking updatedBooking);

OperationResult<Booking> CancelBooking(Booking cancelledBooking);
Task<OperationResult<Booking>> CancelBookingAsync(Booking cancelledBooking);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ namespace AirportTicketBookingSystem.Application.Interfaces.Service;

public interface IFlightManagementService
{
OperationResult<Flight> AddFlight(Flight flight);
Task<OperationResult<Flight>> AddFlightAsync(Flight flight);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ public interface IClientRequestService
{
SearchResult<Booking> GetAllBookings(int passengerId);

OperationResult<Booking> AddBooking(Booking booking);
Task<OperationResult<Booking>> AddBookingAsync(Booking booking);

OperationResult<Booking> UpdateBooking(Booking updatedBooking);
Task<OperationResult<Booking>> UpdateBookingAsync(Booking updatedBooking);

OperationResult<Booking> CancelBooking(Booking cancelledBooking);
Task<OperationResult<Booking>> CancelBookingAsync(Booking cancelledBooking);

bool IsPassengerRegistered(int passengerId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public interface IManagerRequestService
{
SearchResult<Booking> SearchBookings(BookingSearchCriteria criteria);

OperationResult<Flight> AddFlight(Flight flight);
Task<OperationResult<Flight>> AddFlightAsync(Flight flight);

IEnumerable<OperationResult<Flight>> BatchUploadFlights(string filepath);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ public SearchResult<Booking> GetAllBookings(int passengerId)
Items: bookings);
}

public OperationResult<Booking> AddBooking(Booking booking)
public async Task<OperationResult<Booking>> AddBookingAsync(Booking booking)
{
try
{
_bookingService.Add(booking);
await _bookingService.AddAsync(booking);
return new OperationResult<Booking>(
Success: true,
Message: "Booking creation completed successfully",
Expand All @@ -40,11 +40,11 @@ public OperationResult<Booking> AddBooking(Booking booking)
}
}

public OperationResult<Booking> UpdateBooking(Booking updatedBooking)
public async Task<OperationResult<Booking>> UpdateBookingAsync(Booking updatedBooking)
{
try
{
_bookingService.Update(updatedBooking);
await _bookingService.UpdateAsync(updatedBooking);
return new OperationResult<Booking>(
Success: true,
Message: "Booking update completed successfully",
Expand All @@ -59,11 +59,11 @@ public OperationResult<Booking> UpdateBooking(Booking updatedBooking)
}
}

public OperationResult<Booking> CancelBooking(Booking cancelledBooking)
public async Task<OperationResult<Booking>> CancelBookingAsync(Booking cancelledBooking)
{
try
{
_bookingService.Delete(cancelledBooking);
await _bookingService.DeleteAsync(cancelledBooking);
return new OperationResult<Booking>(
Success: true,
Message: "Booking delete completed successfully",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ public ClientRequestService(
public SearchResult<Booking> GetAllBookings(int passengerId) =>
_bookingManagementService.GetAllBookings(passengerId);

public OperationResult<Booking> AddBooking(Booking booking) =>
_bookingManagementService.AddBooking(booking);
public async Task<OperationResult<Booking>> AddBookingAsync(Booking booking) =>
await _bookingManagementService.AddBookingAsync(booking);

public OperationResult<Booking> UpdateBooking(Booking updatedBooking) =>
_bookingManagementService.UpdateBooking(updatedBooking);
public async Task<OperationResult<Booking>> UpdateBookingAsync(Booking updatedBooking) =>
await _bookingManagementService.UpdateBookingAsync(updatedBooking);

public OperationResult<Booking> CancelBooking(Booking cancelledBooking) =>
_bookingManagementService.CancelBooking(cancelledBooking);
public async Task<OperationResult<Booking>> CancelBookingAsync(Booking cancelledBooking) =>
await _bookingManagementService.CancelBookingAsync(cancelledBooking);

public bool IsPassengerRegistered(int passengerId) =>
_passengerRegistrationService.IsPassengerRegistered(passengerId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ public class FlightManagementService : IFlightManagementService
private readonly IFlightService _flightService;
public FlightManagementService(IFlightService flightService) => _flightService = flightService;

public OperationResult<Flight> AddFlight(Flight flight)
public async Task<OperationResult<Flight>> AddFlightAsync(Flight flight)
{
try
{
_flightService.Add(flight);
await _flightService.AddAsync(flight);
return new OperationResult<Flight>(
Success: true,
Message: "Flight creation completed successfully",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ public ManagerRequestService(
public SearchResult<Booking> SearchBookings(BookingSearchCriteria criteria) =>
_searchService.SearchBookings(criteria);

public OperationResult<Flight> AddFlight(Flight flight) =>
_flightManagementService.AddFlight(flight);
public async Task<OperationResult<Flight>> AddFlightAsync(Flight flight) =>
await _flightManagementService.AddFlightAsync(flight);

public IEnumerable<OperationResult<Flight>> BatchUploadFlights(string filepath) =>
_flightUploadService.BatchUpload(filepath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace AirportTicketBookingSystem.Domain.Interfaces.Repository;
public interface IAirportRepository
{
/// <exception cref="DatabaseOperationException">Thrown when an airport with the same identifier already exists in the repository.</exception>
public void Add(Airport airport);
public Task AddAsync(Airport airport);

public IEnumerable<Airport> GetAll();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ namespace AirportTicketBookingSystem.Domain.Interfaces.Repository;
public interface IBookingRepository
{
/// <exception cref="DatabaseOperationException">Thrown when an identical booking already exists in the repository.</exception>
public void Add(Booking booking);
public Task AddAsync(Booking booking);

/// <exception cref="DatabaseOperationException">Thrown when the booking to update does not exist in the repository.</exception>
public void Update(Booking booking);
public Task UpdateAsync(Booking booking);

/// <exception cref="DatabaseOperationException">Thrown when the booking to delete does not exist in the repository.</exception>
public void Delete(Booking booking);
public Task DeleteAsync(Booking booking);

public IEnumerable<Booking> GetAll();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace AirportTicketBookingSystem.Domain.Interfaces.Repository;
public interface IFlightRepository
{
/// <exception cref="DatabaseOperationException">Thrown when a flight with the same identifier already exists in the repository.</exception>
public void Add(Flight flight);
public Task AddAsync(Flight flight);

public IEnumerable<Flight> GetAll();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace AirportTicketBookingSystem.Domain.Interfaces.Repository;
public interface IPassengerRepository
{
/// <exception cref="DatabaseOperationException">Thrown when a passenger with the same identifier already exists in the repository.</exception>
public void Add(Passenger passenger);
public Task AddAsync(Passenger passenger);

public Passenger? GetById(int id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public interface IAirportService
/// </summary>
/// <param name="airport">The airport to add.</param>
/// <exception cref="DatabaseOperationException">Thrown when an airport with the same identifier already exists in the repository.</exception>
void Add(Airport airport);
Task AddAsync(Airport airport);

/// <summary>
/// Retrieves an airport by its identifier.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,22 @@ public interface IBookingService
/// <param name="booking">The booking to add.</param>
/// <exception cref="DatabaseOperationException">Thrown when an identical booking already exists in the repository.</exception>
/// <exception cref="DatabaseRelationalException">Thrown when the <c>flightId</c> or <c>passengerId</c> in the booking do not exist in the repository.</exception>
public void Add(Booking booking);
public Task AddAsync(Booking booking);

/// <summary>
/// Updates an existing booking in the repository.
/// </summary>
/// <param name="booking">The booking with updated information.</param>
/// <exception cref="DatabaseOperationException">Thrown when the booking to update does not exist in the repository.</exception>
/// <exception cref="DatabaseRelationalException">Thrown when the <c>flightId</c> or <c>passengerId</c> in the booking do not exist in the repository.</exception>
public void Update(Booking booking);
public Task UpdateAsync(Booking booking);

/// <summary>
/// Deletes a booking from the repository based on flight and passenger identifiers.
/// </summary>
/// <param name="booking">The booking to delete.</param>
/// <exception cref="DatabaseOperationException">Thrown when the booking to delete does not exist in the repository.</exception>
public void Delete(Booking booking);
public Task DeleteAsync(Booking booking);

/// <summary>
/// Retrieves a booking by flight and passenger identifiers.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,7 @@ public interface IFlightService
/// <param name="flight">The flight to add.</param>
/// <exception cref="DatabaseOperationException">Thrown when a flight with the same identifier already exists in the repository.</exception>
/// <exception cref="DatabaseRelationalException">Thrown when <c>DepartureAirportId</c> or <c>ArrivalAirportId</c> do not exist in the repository.</exception>
public void Add(Flight flight);

/// <summary>
/// Asynchronously adds multiple flights to the repository.
/// </summary>
/// <param name="flights">The collection of flights to add.</param>
/// <returns>An empty collection of tasks.</returns>
[Obsolete("Method not implemented yet")]
public IEnumerable<Task> AddAllAsync(IEnumerable<Flight> flights) => Enumerable.Empty<Task>();
public Task AddAsync(Flight flight);

/// <summary>
/// Retrieves a flight by its identifier.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public interface IPassengerService
/// </summary>
/// <param name="passenger">The passenger to add.</param>
/// <exception cref="DatabaseOperationException">Thrown when a passenger with the same identifier already exists in the repository.</exception>
public void Add(Passenger passenger);
public Task AddAsync(Passenger passenger);

/// <summary>
/// Retrieves a passenger by their unique identifier.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public AirportRepository(
_crudDatabaseService = crudDatabaseService;
}

public void Add(Airport airport) => _crudDatabaseService.AddAsync(airport);
public async Task AddAsync(Airport airport) => await _crudDatabaseService.AddAsync(airport);

public IEnumerable<Airport> GetAll() => _queryDatabaseService.GetAll();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ public BookingRepository(
_crudDatabaseService = crudDatabaseService;
}

public void Add(Booking booking) => _crudDatabaseService.AddAsync(booking);
public async Task AddAsync(Booking booking) => await _crudDatabaseService.AddAsync(booking);

public void Update(Booking booking) => _crudDatabaseService.UpdateAsync(booking);
public async Task UpdateAsync(Booking booking) => await _crudDatabaseService.UpdateAsync(booking);

public void Delete(Booking booking) => _crudDatabaseService.DeleteAsync(booking);
public async Task DeleteAsync(Booking booking) => await _crudDatabaseService.DeleteAsync(booking);

public IEnumerable<Booking> GetAll() => _queryDatabaseService.GetAll();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public FlightRepository(
_crudDatabaseService = crudDatabaseService;
}

public void Add(Flight flight) => _crudDatabaseService.AddAsync(flight);
public async Task AddAsync(Flight flight) => await _crudDatabaseService.AddAsync(flight);

public IEnumerable<Flight> GetAll() => _queryDatabaseService.GetAll();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public PassengerRepository(
_crudDatabaseService = crudDatabaseService;
}

public void Add(Passenger passenger) => _crudDatabaseService.AddAsync(passenger);
public async Task AddAsync(Passenger passenger) => await _crudDatabaseService.AddAsync(passenger);

public Passenger? GetById(int id) => _queryDatabaseService
.GetAll()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public AirportService(
_filteringService = filteringService;
}

public void Add(Airport airport) => _repository.Add(airport);
public async Task AddAsync(Airport airport) => await _repository.AddAsync(airport);

public Airport? GetById(string id) => _repository.GetById(id);

Expand Down
Loading

0 comments on commit c04c3cb

Please sign in to comment.