From e99de62498978c0923bfd06f6ae12553032281b7 Mon Sep 17 00:00:00 2001 From: Anton Todorov Date: Thu, 18 Jan 2024 14:48:26 +0200 Subject: [PATCH] Add category, breed, gender and city in all animals marketplace --- Application/Animal/AllAnimal.cs | 2 +- .../DTOs/Marketplace/AllAnimalForSaleDto.cs | 2 +- .../Marketplace/AllAnimalMarketplaceDto.cs | 15 ++++++++++ .../Marketplace/AllAnimalsForAdoption.cs | 18 ++++++----- Application/Marketplace/AllAnimalsForSale.cs | 6 +++- Application/User/GetAllTowns.cs | 1 + .../Marketplace/AllAnimalsForAdoptionTests.cs | 30 +++++++++++++++++-- Tests/Marketplace/AllAnimalsForSaleTests.cs | 26 ++++++++++++++++ 8 files changed, 88 insertions(+), 12 deletions(-) create mode 100644 Application/DTOs/Marketplace/AllAnimalMarketplaceDto.cs diff --git a/Application/Animal/AllAnimal.cs b/Application/Animal/AllAnimal.cs index c4a99db..79e79ca 100644 --- a/Application/Animal/AllAnimal.cs +++ b/Application/Animal/AllAnimal.cs @@ -7,12 +7,12 @@ using Microsoft.EntityFrameworkCore; using Domain; + using Domain.Enum; using Response; using Application.DTOs.Animal; using Persistence.Repositories; using static Common.ExceptionMessages.Animal; - using Domain.Enum; public class AllAnimal { diff --git a/Application/DTOs/Marketplace/AllAnimalForSaleDto.cs b/Application/DTOs/Marketplace/AllAnimalForSaleDto.cs index 621a26e..1e96309 100644 --- a/Application/DTOs/Marketplace/AllAnimalForSaleDto.cs +++ b/Application/DTOs/Marketplace/AllAnimalForSaleDto.cs @@ -2,7 +2,7 @@ { using Animal; - public class AllAnimalsForSaleDto : AllAnimalsDto + public class AllAnimalsForSaleDto : AllAnimalsMarketplaceDto { public decimal? Price { get; set; } } diff --git a/Application/DTOs/Marketplace/AllAnimalMarketplaceDto.cs b/Application/DTOs/Marketplace/AllAnimalMarketplaceDto.cs new file mode 100644 index 0000000..dab726c --- /dev/null +++ b/Application/DTOs/Marketplace/AllAnimalMarketplaceDto.cs @@ -0,0 +1,15 @@ +namespace Application.DTOs.Marketplace +{ + using Application.DTOs.Animal; + + public class AllAnimalsMarketplaceDto : AllAnimalsDto + { + public string Category { get; set; } = null!; + + public string Breed { get; set; } = null!; + + public string Gender { get; set; } = null!; + + public string City { get; set; } = null!; + } +} diff --git a/Application/Marketplace/AllAnimalsForAdoption.cs b/Application/Marketplace/AllAnimalsForAdoption.cs index feb058f..fe62bf0 100644 --- a/Application/Marketplace/AllAnimalsForAdoption.cs +++ b/Application/Marketplace/AllAnimalsForAdoption.cs @@ -8,20 +8,20 @@ using Domain; using Response; - using DTOs.Animal; using Domain.Enum; using Persistence.Repositories; + using Application.DTOs.Marketplace; using static Common.ExceptionMessages.Marketplace; public class AllAnimalsForAdoption { - public class AllAnimalsForAdoptionQuery : IRequest>> + public class AllAnimalsForAdoptionQuery : IRequest>> { public string UserId { get; set; } = null!; } - public class AllAnimalsForAdoptionQueryHandler : IRequestHandler>> + public class AllAnimalsForAdoptionQueryHandler : IRequestHandler>> { private readonly IRepository repository; @@ -30,25 +30,29 @@ public AllAnimalsForAdoptionQueryHandler(IRepository repository) this.repository = repository; } - public async Task>> Handle(AllAnimalsForAdoptionQuery request, CancellationToken cancellationToken) + public async Task>> Handle(AllAnimalsForAdoptionQuery request, CancellationToken cancellationToken) { string userId = request.UserId; var allAnimals = await repository. AllReadonly(a => a.OwnerId.ToString() != userId && a.AnimalStatus == AnimalStatus.ForAdoption). - Select(a => new AllAnimalsDto() + Select(a => new AllAnimalsMarketplaceDto() { Id = a.AnimalId.ToString(), MainPhoto = a.Photos.First(a => a.IsMain).Url, Name = a.Name, + Breed = a.Breed.Name, + Category = a.Breed.Category.Name, + City = a.Owner.City!, + Gender = a.Gender.ToString(), }).ToArrayAsync(); if (!allAnimals.Any()) { - return Result>.Failure(NoAnimalsForAdoption); + return Result>.Failure(NoAnimalsForAdoption); } - return Result>.Success(allAnimals); + return Result>.Success(allAnimals); } } } diff --git a/Application/Marketplace/AllAnimalsForSale.cs b/Application/Marketplace/AllAnimalsForSale.cs index fe77dd9..7225e96 100644 --- a/Application/Marketplace/AllAnimalsForSale.cs +++ b/Application/Marketplace/AllAnimalsForSale.cs @@ -42,7 +42,11 @@ public async Task>> Handle(AllAnimalsFo Id = a.AnimalId.ToString(), Name = a.Name, MainPhoto = a.Photos.First(p => p.IsMain).Url, - Price = a.Price + Price = a.Price, + Breed = a.Breed.Name, + Category = a.Breed.Category.Name, + City = a.Owner.City!, + Gender = a.Gender.ToString(), }).ToArrayAsync(); if (!allAnimals.Any()) diff --git a/Application/User/GetAllTowns.cs b/Application/User/GetAllTowns.cs index 7655702..534f438 100644 --- a/Application/User/GetAllTowns.cs +++ b/Application/User/GetAllTowns.cs @@ -29,6 +29,7 @@ public GetAllTownsQueryHandler(IRepository repository) public async Task>> Handle(GetAllTownsQuery request, CancellationToken cancellationToken) { var towns = await repository.AllReadonly(). + Where(u => u.City != null). Select(u => u.City).Distinct().ToArrayAsync(); return Result>.Success(towns); diff --git a/Tests/Marketplace/AllAnimalsForAdoptionTests.cs b/Tests/Marketplace/AllAnimalsForAdoptionTests.cs index 16aa586..8ed78eb 100644 --- a/Tests/Marketplace/AllAnimalsForAdoptionTests.cs +++ b/Tests/Marketplace/AllAnimalsForAdoptionTests.cs @@ -47,7 +47,20 @@ public async Task Handle_ShowAllAnimalForAdoption_ReturnsSuccessResult() Url = "URL" } }, + Breed = new Breed() + { + CategoryId = 2, + Name = "BreedTest", + Category = new AnimalCategory() + { + Name = "AnimalCategoryTest" + } + }, OwnerId = Guid.NewGuid(), + Owner = new User() + { + Name = "OwnerTest" + } }; var animalTwo = new Animal() { @@ -68,7 +81,20 @@ public async Task Handle_ShowAllAnimalForAdoption_ReturnsSuccessResult() Url = "URL" } }, + Breed = new Breed() + { + CategoryId = 2, + Name = "BreedTest", + Category = new AnimalCategory() + { + Name = "AnimalCategoryTest" + } + }, OwnerId = Guid.NewGuid(), + Owner = new User() + { + Name = "TestOwner" + } }; var queryable = @@ -87,8 +113,8 @@ public async Task Handle_ShowAllAnimalForAdoption_ReturnsSuccessResult() public async Task Handle_ShowAllAnimalForAdoption_WhenNoAnimalsFound_ReturnsFailureResult() { var queryable = - new List {}.AsQueryable(); - SetUpReturningAnimals(queryable, repositoryMock); + new List { }.AsQueryable(); + SetUpReturningAnimals(queryable, repositoryMock); var result = await handler.Handle(new AllAnimalsForAdoptionQuery(), CancellationToken.None); diff --git a/Tests/Marketplace/AllAnimalsForSaleTests.cs b/Tests/Marketplace/AllAnimalsForSaleTests.cs index a918beb..8679c14 100644 --- a/Tests/Marketplace/AllAnimalsForSaleTests.cs +++ b/Tests/Marketplace/AllAnimalsForSaleTests.cs @@ -47,7 +47,20 @@ public async Task Handle_ShowAllAnimalForSale_ReturnsSuccessResult() Url = "URL" } }, + Breed = new Breed() + { + CategoryId = 2, + Name = "BreedTest", + Category = new AnimalCategory() + { + Name = "AnimalCategoryTest" + } + }, OwnerId = Guid.NewGuid(), + Owner = new User() + { + Name = "TestOwner" + } }; var animalTwo = new Animal() { @@ -68,7 +81,20 @@ public async Task Handle_ShowAllAnimalForSale_ReturnsSuccessResult() Url = "URL" } }, + Breed = new Breed() + { + CategoryId = 2, + Name = "BreedTest", + Category = new AnimalCategory() + { + Name = "AnimalCategoryTest" + } + }, OwnerId = Guid.NewGuid(), + Owner = new User() + { + Name = "TestOwner" + } }; var queryable =