Skip to content

Commit

Permalink
Add category, breed, gender and city in all animals marketplace
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonTodorov321 committed Jan 18, 2024
1 parent de90df3 commit e99de62
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Application/Animal/AllAnimal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
2 changes: 1 addition & 1 deletion Application/DTOs/Marketplace/AllAnimalForSaleDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{
using Animal;

public class AllAnimalsForSaleDto : AllAnimalsDto
public class AllAnimalsForSaleDto : AllAnimalsMarketplaceDto
{
public decimal? Price { get; set; }
}
Expand Down
15 changes: 15 additions & 0 deletions Application/DTOs/Marketplace/AllAnimalMarketplaceDto.cs
Original file line number Diff line number Diff line change
@@ -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!;
}
}
18 changes: 11 additions & 7 deletions Application/Marketplace/AllAnimalsForAdoption.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Result<IEnumerable<AllAnimalsDto>>>
public class AllAnimalsForAdoptionQuery : IRequest<Result<IEnumerable<AllAnimalsMarketplaceDto>>>
{
public string UserId { get; set; } = null!;
}

public class AllAnimalsForAdoptionQueryHandler : IRequestHandler<AllAnimalsForAdoptionQuery, Result<IEnumerable<AllAnimalsDto>>>
public class AllAnimalsForAdoptionQueryHandler : IRequestHandler<AllAnimalsForAdoptionQuery, Result<IEnumerable<AllAnimalsMarketplaceDto>>>
{
private readonly IRepository repository;

Expand All @@ -30,25 +30,29 @@ public AllAnimalsForAdoptionQueryHandler(IRepository repository)
this.repository = repository;
}

public async Task<Result<IEnumerable<AllAnimalsDto>>> Handle(AllAnimalsForAdoptionQuery request, CancellationToken cancellationToken)
public async Task<Result<IEnumerable<AllAnimalsMarketplaceDto>>> Handle(AllAnimalsForAdoptionQuery request, CancellationToken cancellationToken)
{
string userId = request.UserId;

var allAnimals = await repository.
AllReadonly<Animal>(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<IEnumerable<AllAnimalsDto>>.Failure(NoAnimalsForAdoption);
return Result<IEnumerable<AllAnimalsMarketplaceDto>>.Failure(NoAnimalsForAdoption);
}

return Result<IEnumerable<AllAnimalsDto>>.Success(allAnimals);
return Result<IEnumerable<AllAnimalsMarketplaceDto>>.Success(allAnimals);
}
}
}
Expand Down
6 changes: 5 additions & 1 deletion Application/Marketplace/AllAnimalsForSale.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ public async Task<Result<IEnumerable<AllAnimalsForSaleDto>>> 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())
Expand Down
1 change: 1 addition & 0 deletions Application/User/GetAllTowns.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public GetAllTownsQueryHandler(IRepository repository)
public async Task<Result<IEnumerable<string>>> Handle(GetAllTownsQuery request, CancellationToken cancellationToken)
{
var towns = await repository.AllReadonly<User>().
Where(u => u.City != null).
Select(u => u.City).Distinct().ToArrayAsync();

return Result<IEnumerable<string>>.Success(towns);
Expand Down
30 changes: 28 additions & 2 deletions Tests/Marketplace/AllAnimalsForAdoptionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand All @@ -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 =
Expand All @@ -87,8 +113,8 @@ public async Task Handle_ShowAllAnimalForAdoption_ReturnsSuccessResult()
public async Task Handle_ShowAllAnimalForAdoption_WhenNoAnimalsFound_ReturnsFailureResult()
{
var queryable =
new List<Animal> {}.AsQueryable();
SetUpReturningAnimals(queryable, repositoryMock);
new List<Animal> { }.AsQueryable();
SetUpReturningAnimals(queryable, repositoryMock);

var result = await handler.Handle(new AllAnimalsForAdoptionQuery(), CancellationToken.None);

Expand Down
26 changes: 26 additions & 0 deletions Tests/Marketplace/AllAnimalsForSaleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand All @@ -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 =
Expand Down

0 comments on commit e99de62

Please sign in to comment.