Skip to content

Commit

Permalink
Add find all games within IdList (#16)
Browse files Browse the repository at this point in the history
* Add find all games within IdList

* Merge 2  "GetGames" function
  • Loading branch information
AnhQuoc2906 committed Nov 7, 2023
1 parent 5893ae0 commit a7fd973
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
9 changes: 6 additions & 3 deletions Api/Controllers/GamesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,19 @@ public GamesController(IGameServices gameServices, IGenericRepository<GameEntity
}

[HttpGet]
public async Task<IActionResult> GetGames()
public async Task<IActionResult> GetGames([FromQuery] Guid[]? idList)
{
if (idList != null && idList.Count() > 0)
{
return Ok(await _gameServices.List(idList));
}
return Ok(await _gameServices.List());
}

[HttpGet("{id}")]
public async Task<IActionResult> GetGame(Guid id)
{
var user = await _gameServices.GetById(id);
return Ok(user);
return Ok(await _gameServices.GetById(id));
}

[HttpPost]
Expand Down
21 changes: 20 additions & 1 deletion Application/Business/GameServices/GameServices.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using DomainLayer.Entities;
using DomainLayer.Constants;
using DomainLayer.Entities;
using DomainLayer.Exceptions;
using RepositoryLayer.Repositories;
using System.Reflection.Metadata;

namespace ServiceLayer.Business;

Expand All @@ -20,6 +22,23 @@ public async Task<GameEntity> GetById(Guid gameId)
{
return await _gameRepo.FindByIdAsync(gameId);
}
public async Task<ICollection<GameEntity>> List(Guid[] gameIds)
{
List<GameEntity> result = new List<GameEntity>();
foreach(var gameId in gameIds.Distinct())
{
var game = await _gameRepo.FindByIdAsync(gameId);
if (game is not null)
{
result.Add(game);
}
else
{
throw new NotFoundException("Game with ID " + gameId + " " + Constants.ERROR.NOT_EXIST_ERROR);
}
}
return result;
}
public async Task<ICollection<GameEntity>> GetByUserId(Guid userId)
{
return await _gameRepo.WhereAsync(g => g.UserId.Equals(userId));
Expand Down
1 change: 1 addition & 0 deletions Application/Business/GameServices/IGameServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ public interface IGameServices
{
Task<ICollection<GameEntity>> List();
Task<GameEntity> GetById(Guid gameId);
Task<ICollection<GameEntity>> List(Guid[] gameIds);
Task<ICollection<GameEntity>> GetByUserId(Guid userId);
Task<int> Count();
Task Create(GameEntity game);
Expand Down

0 comments on commit a7fd973

Please sign in to comment.