diff --git a/SchengenVisaApi/Infrastructure/Database/Applicants/Repositories/ApplicantsRepository.cs b/SchengenVisaApi/Infrastructure/Database/Applicants/Repositories/ApplicantsRepository.cs index d0c4763..51d0da2 100644 --- a/SchengenVisaApi/Infrastructure/Database/Applicants/Repositories/ApplicantsRepository.cs +++ b/SchengenVisaApi/Infrastructure/Database/Applicants/Repositories/ApplicantsRepository.cs @@ -1,5 +1,4 @@ using ApplicationLayer.Applicants; -using ApplicationLayer.Common; using Domains.ApplicantDomain; using Infrastructure.Database.Generic; using Microsoft.EntityFrameworkCore; @@ -20,4 +19,4 @@ public sealed class ApplicantsRepository(IGenericReader reader, IGenericWriter w .Include(a => a.CityOfBirth) .Include(a => a.PlaceOfWork); } -} \ No newline at end of file +} diff --git a/SchengenVisaApi/Infrastructure/Database/GeneralExceptions/EntityNotFoundByIdException.cs b/SchengenVisaApi/Infrastructure/Database/GeneralExceptions/EntityNotFoundByIdException.cs new file mode 100644 index 0000000..b494c87 --- /dev/null +++ b/SchengenVisaApi/Infrastructure/Database/GeneralExceptions/EntityNotFoundByIdException.cs @@ -0,0 +1,10 @@ +using Domains; + +namespace Infrastructure.Database.GeneralExceptions +{ + /// Exception to throw when entity not found + /// Identifier of entity + /// Type of entity + public class EntityNotFoundByIdException(Guid id) : EntityNotFoundException($"Entity {typeof(T).Name} with id {id} not found.") + where T : class, IEntity; +} diff --git a/SchengenVisaApi/Infrastructure/Database/GeneralExceptions/EntityNotFoundException.cs b/SchengenVisaApi/Infrastructure/Database/GeneralExceptions/EntityNotFoundException.cs index 2d6801c..3d6ce3d 100644 --- a/SchengenVisaApi/Infrastructure/Database/GeneralExceptions/EntityNotFoundException.cs +++ b/SchengenVisaApi/Infrastructure/Database/GeneralExceptions/EntityNotFoundException.cs @@ -2,8 +2,7 @@ namespace Infrastructure.Database.GeneralExceptions; -/// Exception to throw when entity with specific id not found -/// Identifier of entity +/// Exception to throw when entity not found /// Not found entity type -public class EntityNotFoundException(Guid id) : Exception($"Entity {typeof(T).Name} with id '{id}' not found") - where T : class, IEntity; \ No newline at end of file +public class EntityNotFoundException(string message) : Exception(message) + where T : class, IEntity; diff --git a/SchengenVisaApi/Infrastructure/Database/Generic/GenericRepository.cs b/SchengenVisaApi/Infrastructure/Database/Generic/GenericRepository.cs index 7541a9c..836494d 100644 --- a/SchengenVisaApi/Infrastructure/Database/Generic/GenericRepository.cs +++ b/SchengenVisaApi/Infrastructure/Database/Generic/GenericRepository.cs @@ -21,7 +21,7 @@ public abstract class GenericRepository(IGenericReader reader, IGenericWriter public async Task GetOneAsync(Guid id, CancellationToken cancellationToken) { var result = await LoadDomain().SingleOrDefaultAsync(a => a.Id == id, cancellationToken); - return result ?? throw new EntityNotFoundException(id); + return result ?? throw new EntityNotFoundByIdException(id); } /// @@ -50,4 +50,4 @@ public abstract class GenericRepository(IGenericReader reader, IGenericWriter { return reader.GetAll(); } -} \ No newline at end of file +} diff --git a/SchengenVisaApi/Infrastructure/Database/Locations/Repositories/Cities/CitiesRepository.cs b/SchengenVisaApi/Infrastructure/Database/Locations/Repositories/Cities/CitiesRepository.cs index 583764b..7849fae 100644 --- a/SchengenVisaApi/Infrastructure/Database/Locations/Repositories/Cities/CitiesRepository.cs +++ b/SchengenVisaApi/Infrastructure/Database/Locations/Repositories/Cities/CitiesRepository.cs @@ -1,6 +1,7 @@ using ApplicationLayer.Locations; using Domains.LocationDomain; using Infrastructure.Database.Generic; +using Infrastructure.Database.Locations.Repositories.Cities.Exceptions; using Microsoft.EntityFrameworkCore; namespace Infrastructure.Database.Locations.Repositories.Cities; @@ -12,4 +13,12 @@ public sealed class CitiesRepository(IGenericReader reader, IGenericWriter write { return base.LoadDomain().Include(c => c.Country); } -} \ No newline at end of file + + /// + /// city with provided name and country not found + public async Task GetByNameAsync(string name, string countryName, CancellationToken cancellationToken) + { + var result = await LoadDomain().Where(c => c.Name == name && c.Country.Name == countryName).SingleOrDefaultAsync(cancellationToken); + return result ?? throw new CityNotFoundByNameException(name, countryName); + } +} diff --git a/SchengenVisaApi/Infrastructure/Database/Locations/Repositories/Cities/Exceptions/CityNotFoundByNameException.cs b/SchengenVisaApi/Infrastructure/Database/Locations/Repositories/Cities/Exceptions/CityNotFoundByNameException.cs new file mode 100644 index 0000000..55ad91d --- /dev/null +++ b/SchengenVisaApi/Infrastructure/Database/Locations/Repositories/Cities/Exceptions/CityNotFoundByNameException.cs @@ -0,0 +1,11 @@ +using Domains.LocationDomain; +using Infrastructure.Database.GeneralExceptions; + +namespace Infrastructure.Database.Locations.Repositories.Cities.Exceptions +{ + /// Exception to throw when city cannot be found by its name and its country name + /// Name of the city + /// name of the city's country + public class CityNotFoundByNameException(string name, string countryName) + : EntityNotFoundException($"{name} with country {countryName} not found."); +} diff --git a/SchengenVisaApi/Infrastructure/DependencyInjection.cs b/SchengenVisaApi/Infrastructure/DependencyInjection.cs index b3febe4..da92abd 100644 --- a/SchengenVisaApi/Infrastructure/DependencyInjection.cs +++ b/SchengenVisaApi/Infrastructure/DependencyInjection.cs @@ -1,5 +1,4 @@ using ApplicationLayer.Applicants; -using ApplicationLayer.Common; using ApplicationLayer.Locations; using ApplicationLayer.VisaApplications; using Infrastructure.Database;