-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat/available car
- Loading branch information
Showing
10 changed files
with
221 additions
and
46 deletions.
There are no files selected for viewing
9 changes: 9 additions & 0 deletions
9
car-rental/backend/CarRentalAPI/CarRentalAPI/Abstractions/Repositories/ICarRepository.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using CarRentalAPI.Models; | ||
|
||
namespace CarRentalAPI.Abstractions.Repositories | ||
{ | ||
public interface ICarRepository | ||
{ | ||
public Task<List<Car>> GetCarsByIdAsync(List<int> ids, string? brand, string? model, string? location); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
car-rental/backend/CarRentalAPI/CarRentalAPI/Abstractions/Repositories/IRentRepository.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using CarRentalAPI.DTOs.Combinations; | ||
|
||
namespace CarRentalAPI.Abstractions.Repositories | ||
{ | ||
public interface IRentRepository | ||
{ | ||
public Task<List<CarIdRentDatesDto>> GetChosenCarActiveRentDatesAsync(string? brand, string? model, string? location); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
car-rental/backend/CarRentalAPI/CarRentalAPI/DTOs/CarSearch/OfferForCarSearchDto.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
namespace CarRentalAPI.DTOs.CarSearch | ||
{ | ||
public class OfferForCarSearchDto | ||
{ | ||
public int CarId { get; set; } | ||
public string Brand { get; set; } | ||
public string Model { get; set; } | ||
public decimal Price { get; set; } | ||
public string Conditions { get; set; } | ||
public string CompanyName { get; set; } | ||
public string Location { get; set; } | ||
public DateTime StartDate { get; set; } | ||
public DateTime EndDate { get; set; } | ||
public string Email { get; set; } | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
car-rental/backend/CarRentalAPI/CarRentalAPI/DTOs/Combinations/CarIdRentDatesDto.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
namespace CarRentalAPI.DTOs.Combinations | ||
{ | ||
public class CarIdRentDatesDto | ||
{ | ||
public int CarId { get; set; } | ||
public DateTime StartDate { get; set; } | ||
public DateTime EndDate { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
car-rental/backend/CarRentalAPI/CarRentalAPI/Repositories/CarRepository.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using CarRentalAPI.Abstractions.Repositories; | ||
using CarRentalAPI.Models; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.IdentityModel.Tokens; | ||
|
||
namespace CarRentalAPI.Repositories | ||
{ | ||
public class CarRepository : ICarRepository | ||
{ | ||
private readonly CarRentalDbContext _context; | ||
|
||
public CarRepository(CarRentalDbContext context) | ||
{ | ||
_context = context; | ||
} | ||
|
||
public async Task<List<Car>> GetCarsByIdAsync(List<int> ids, string? brand, string? model, string? location) | ||
{ | ||
var cars = await _context.Cars | ||
.Where(c => ((!ids.Contains(c.CarId))) && | ||
(brand.IsNullOrEmpty() || c.Model.Brand.Name == brand) && | ||
(model.IsNullOrEmpty() || c.Model.Name == model) && | ||
(location.IsNullOrEmpty() || c.Location == location)) | ||
.Include(c => c.Model) | ||
.ThenInclude(m => m.Brand).ToListAsync(); | ||
return cars; | ||
} | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
car-rental/backend/CarRentalAPI/CarRentalAPI/Repositories/RentRepository.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using CarRentalAPI.Abstractions.Repositories; | ||
using CarRentalAPI.DTOs.Combinations; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.IdentityModel.Tokens; | ||
|
||
namespace CarRentalAPI.Repositories | ||
{ | ||
public class RentRepository : IRentRepository | ||
{ | ||
private readonly CarRentalDbContext _context; | ||
|
||
public RentRepository(CarRentalDbContext context) | ||
{ | ||
_context = context; | ||
} | ||
|
||
public async Task<List<CarIdRentDatesDto>> GetChosenCarActiveRentDatesAsync(string? brand, string? model, string? location) | ||
{ | ||
try | ||
{ | ||
var query = from r in _context.Rents | ||
join c in _context.Cars on r.CarId equals c.CarId | ||
where | ||
(brand.IsNullOrEmpty() || c.Model.Brand.Name == brand) && | ||
(model.IsNullOrEmpty() || c.Model.Name == model) && | ||
(location.IsNullOrEmpty() || c.Location == location) && | ||
(r.RentEnd > DateTime.Today) | ||
select new CarIdRentDatesDto { CarId = c.CarId, StartDate = r.RentStart, EndDate = r.RentEnd.Value }; | ||
return await query.ToListAsync(); | ||
} | ||
catch (InvalidOperationException ex) | ||
{ | ||
Console.WriteLine("With high probability rent without RentEnd appeared in the database for some reason\n"); | ||
throw new InvalidOperationException("Error while fetching rent data.", ex); | ||
|
||
} | ||
|
||
} | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
car-rental/backend/CarRentalAPI/CarRentalAPI/Services/AvailabilityChecker.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using CarRentalAPI.DTOs.Combinations; | ||
|
||
namespace CarRentalAPI.Services | ||
{ | ||
public class AvailabilityChecker | ||
{ | ||
public List<int> CheckForNotAvailableCars(List<CarIdRentDatesDto> pairs, DateTime startDate, DateTime endDate) | ||
{ | ||
List<int> notAvailableCarIds = new List<int>(); | ||
foreach (var pair in pairs) | ||
{ | ||
if (DoIntervalsCollide(pair.StartDate, pair.EndDate, startDate, endDate)) notAvailableCarIds.Add(pair.CarId); | ||
} | ||
return notAvailableCarIds; | ||
} | ||
|
||
private bool DoIntervalsCollide(DateTime start1, DateTime end1, DateTime start2, DateTime end2) | ||
{ | ||
// Check if interval 2 collides with interval 1 | ||
if ((end2 <= end1) && (end2 >= start1)) return true; | ||
if ((start2 >= start1) && (start2 <= end1)) return true; | ||
return false; | ||
} | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
car-rental/backend/CarRentalAPI/CarRentalAPI/Services/OffersService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
using CarRentalAPI.Abstractions; | ||
using CarRentalAPI.Abstractions.Repositories; | ||
using CarRentalAPI.DTOs.CarSearch; | ||
using CarRentalAPI.DTOs.Combinations; | ||
using CarRentalAPI.Models; | ||
|
||
namespace CarRentalAPI.Services | ||
{ | ||
public class OffersService | ||
{ | ||
private readonly IRentRepository _rentRepository; | ||
private readonly ICarRepository _carRepository; | ||
private readonly AvailabilityChecker _availabilityChecker; | ||
private readonly IPriceGenerator _priceGenerator; | ||
|
||
public OffersService(IRentRepository rentRepository, ICarRepository carRepository, AvailabilityChecker availabilityChecker, IPriceGenerator priceGenerator) | ||
{ | ||
_rentRepository = rentRepository; | ||
_carRepository = carRepository; | ||
_availabilityChecker = availabilityChecker; | ||
_priceGenerator = priceGenerator; | ||
} | ||
|
||
public async Task<List<OfferForCarSearchDto>> GetNewOffers(string? brand, string? model, DateTime startDate, DateTime endDate, string? location, string email, string conditions, string companyName) | ||
{ | ||
List<CarIdRentDatesDto> pairs = await _rentRepository.GetChosenCarActiveRentDatesAsync(brand, model, location); | ||
List<int> notAvailableCarIds = _availabilityChecker.CheckForNotAvailableCars(pairs, startDate, endDate); | ||
List<Car> availableCars = await _carRepository.GetCarsByIdAsync(notAvailableCarIds, brand, model, location); | ||
|
||
List<OfferForCarSearchDto> newOffers = new List<OfferForCarSearchDto>(); | ||
foreach (var car in availableCars) | ||
{ | ||
newOffers.Add(new OfferForCarSearchDto | ||
{ | ||
CarId = car.CarId, | ||
Brand = car.Model.Brand.Name, | ||
Model = car.Model.Year == null ? car.Model.Name : car.Model.Name + " " + car.Model.Year, | ||
Price = _priceGenerator.GeneratePrice(car.Model.BasePrice, startDate, endDate), | ||
Conditions = conditions, | ||
CompanyName = companyName, | ||
Location = car.Location, | ||
StartDate = startDate, | ||
EndDate = endDate, | ||
Email = email | ||
}); | ||
} | ||
|
||
return newOffers; | ||
} | ||
} | ||
} |