Created more proper NotFound exceptions
This commit is contained in:
		| @@ -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); | ||||
|     } | ||||
| } | ||||
| } | ||||
|   | ||||
| @@ -0,0 +1,10 @@ | ||||
| using Domains; | ||||
|  | ||||
| namespace Infrastructure.Database.GeneralExceptions | ||||
| { | ||||
|     /// Exception to throw when entity not found | ||||
|     /// <param name="id">Identifier of entity</param> | ||||
|     /// <typeparam name="T">Type of entity</typeparam> | ||||
|     public class EntityNotFoundByIdException<T>(Guid id) : EntityNotFoundException<T>($"Entity {typeof(T).Name} with id {id} not found.") | ||||
|         where T : class, IEntity; | ||||
| } | ||||
| @@ -2,8 +2,7 @@ | ||||
|  | ||||
| namespace Infrastructure.Database.GeneralExceptions; | ||||
|  | ||||
| /// Exception to throw when entity with specific id not found | ||||
| /// <param name="id">Identifier of entity</param> | ||||
| /// Exception to throw when entity not found | ||||
| /// <typeparam name="T">Not found entity type</typeparam> | ||||
| public class EntityNotFoundException<T>(Guid id) : Exception($"Entity {typeof(T).Name} with id '{id}' not found") | ||||
|     where T : class, IEntity; | ||||
| public class EntityNotFoundException<T>(string message) : Exception(message) | ||||
|     where T : class, IEntity; | ||||
|   | ||||
| @@ -21,7 +21,7 @@ public abstract class GenericRepository<T>(IGenericReader reader, IGenericWriter | ||||
|     public async Task<T> GetOneAsync(Guid id, CancellationToken cancellationToken) | ||||
|     { | ||||
|         var result = await LoadDomain().SingleOrDefaultAsync(a => a.Id == id, cancellationToken); | ||||
|         return result ?? throw new EntityNotFoundException<T>(id); | ||||
|         return result ?? throw new EntityNotFoundByIdException<T>(id); | ||||
|     } | ||||
|  | ||||
|     /// <inheritdoc cref="IGenericRepository{T}.AddAsync"/> | ||||
| @@ -50,4 +50,4 @@ public abstract class GenericRepository<T>(IGenericReader reader, IGenericWriter | ||||
|     { | ||||
|         return reader.GetAll<T>(); | ||||
|     } | ||||
| } | ||||
| } | ||||
|   | ||||
| @@ -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); | ||||
|     } | ||||
| } | ||||
|  | ||||
|     ///<inheritdoc cref="ICitiesRepository.GetByNameAsync"/> | ||||
|     /// <exception cref="CityNotFoundByNameException">city with provided name and country not found</exception> | ||||
|     public async Task<City> 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); | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -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 | ||||
|     /// <param name="name">Name of the city</param> | ||||
|     /// <param name="countryName">name of the city's country</param> | ||||
|     public class CityNotFoundByNameException(string name, string countryName) | ||||
|         : EntityNotFoundException<City>($"{name} with country {countryName} not found."); | ||||
| } | ||||
		Reference in New Issue
	
	Block a user