Skip to content

Commit

Permalink
Update new CRUD for Game + User API (#21)
Browse files Browse the repository at this point in the history
* Add update user status

* - Add Mockdata for GameEntityUserEntity board.
- Add Get Users By GameID
- Update GET, POST & PUT API for Users

* Changes:
- Change MockData (GameUser)
- Get Levels By IdList (similar to Games)
- CD for GameUser (in User)

* Changes:
- Remove unuse functions and entities (Game, User)
- Rename funcs
  • Loading branch information
AnhQuoc2906 committed Nov 24, 2023
1 parent 9f2e9be commit 788332e
Show file tree
Hide file tree
Showing 38 changed files with 3,967 additions and 128 deletions.
2 changes: 1 addition & 1 deletion Api/Configurations/AppConfigurationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.Extensions.Options;
using DomainLayer.Constants;
using RepositoryLayer.Contexts;
using RepositoryLayer.Repositories;
using Serilog;
Expand Down Expand Up @@ -31,6 +30,7 @@ public static void AddAppServices(this IServiceCollection services)
services.AddScoped<ICharacterTypeServices, CharacterTypeServices>();
services.AddScoped<IGameServerServices, GameServerServices>();
services.AddScoped<IGameServices, GameServices>();
services.AddScoped<IGameUserServices, GameUserServices>();
services.AddScoped<ILevelProgressServices, LevelProgressServices>();
services.AddScoped<ILevelServices, LevelServices>();
services.AddScoped<IPaymentServices, PaymentServices>();
Expand Down
27 changes: 23 additions & 4 deletions Api/Configurations/DatabaseInitializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public static async Task InitializeAsync(ApplicationDbContext dbContext)
await dbContext.MockAttributeGroup();
await dbContext.MockUser();
await dbContext.MockGame();
await dbContext.MockGameUser();
await dbContext.MockGameServer();
await dbContext.MockAssetType();
await dbContext.MockAsset();
Expand Down Expand Up @@ -104,6 +105,25 @@ public static async Task MockGame(this ApplicationDbContext dbContext)
await dbContext.SaveChangesAsync();
}
}
public static async Task MockGameUser(this ApplicationDbContext dbContext)
{
if (dbContext.GamesUsers.Any()) return;

var users = dbContext.Users.ToList();
var games = dbContext.Games.ToList();
for (int i =0; i < 20; i++)
{
await dbContext.GamesUsers.AddAsync(
new GameUserEntity
{
GameId = games[_rand.Next(games.Count)].Id,
UserId = users[_rand.Next(users.Count)].Id,
CreatedAt = DateTime.Now,
ModifiedAt = DateTime.Now
});
}
await dbContext.SaveChangesAsync();
}
public static async Task MockGameServer(this ApplicationDbContext dbContext)
{
if (dbContext.GameServers.Any()) return;
Expand Down Expand Up @@ -311,10 +331,9 @@ public static async Task MockLevel(this ApplicationDbContext dbContext)
CreatedAt = DateTime.Now,
ModifiedAt = DateTime.Now
};
} while (await dbContext.Levels.FirstOrDefaultAsync(l => l.Name == newLevel.Name && l.GameId == newLevel.GameId) != null);

await dbContext.Levels.AddAsync(newLevel);
await dbContext.SaveChangesAsync(); // Await this operation
} while (dbContext.Levels.FirstOrDefault(l => l.Name == newLevel.Name && l.GameId == newLevel.GameId) != null);
dbContext.Levels.Add(newLevel);
dbContext.SaveChanges(); // Await this operation
}
}
public static async Task MockLevelProgress(this ApplicationDbContext dbContext)
Expand Down
29 changes: 19 additions & 10 deletions Api/Controllers/GamesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,31 @@ namespace WebApiLayer.Controllers;
public class GamesController : BaseController
{
private readonly IGameServices _gameServices;
private readonly IGameUserServices _gameUserServices;
private readonly IActivityTypeServices _activityTypeServices;
private readonly IAssetTypeServices _assetTypeServices;
private readonly ICharacterTypeServices _characterTypeServices;
private readonly IGameServerServices _gameServerServices;
private readonly ILevelServices _levelServices;
private readonly IWalletCategoryServices _walletCategoryServices;
private readonly IUserServices _userServices;
private readonly IGenericRepository<GameEntity> _gameRepo;
private readonly IGenericRepository<UserEntity> _userRepo;
public GamesController(IGameServices gameServices, IActivityTypeServices activityTypeServices
, IAssetTypeServices assetTypeServices
, ICharacterTypeServices characterTypeServices , IGameServerServices gameServerServices
, ILevelServices levelServices, IWalletCategoryServices walletCategoryServices
public GamesController(IGameServices gameServices, IActivityTypeServices activityTypeServices, IGameUserServices gameUserServices
, IAssetTypeServices assetTypeServices, ICharacterTypeServices characterTypeServices
, IGameServerServices gameServerServices, ILevelServices levelServices
, IWalletCategoryServices walletCategoryServices, IUserServices userServices
, IGenericRepository<GameEntity> gameRepo, IGenericRepository<UserEntity> userRepo)
{
_gameServices = gameServices;
_activityTypeServices = activityTypeServices;
_gameUserServices = gameUserServices;
_assetTypeServices = assetTypeServices;
_characterTypeServices = characterTypeServices;
_gameServerServices = gameServerServices;
_levelServices = levelServices;
_walletCategoryServices = walletCategoryServices;
_userServices = userServices;
_gameRepo = gameRepo;
_userRepo = userRepo;
}
Expand All @@ -55,39 +59,44 @@ public async Task<IActionResult> GetGame(Guid id)
[HttpGet("{id}/activity-types")]
public async Task<IActionResult> GetActTypeByGameID(Guid id)
{
return Ok(await _activityTypeServices.GetByGameId(id));
return Ok(await _activityTypeServices.ListActTypesByGameId(id));
}

[HttpGet("{id}/asset-types")]
public async Task<IActionResult> GetAssTypeByGameID(Guid id)
{
return Ok(await _assetTypeServices.GetByGameId(id));
return Ok(await _assetTypeServices.ListAssTypesByGameId(id));
}

[HttpGet("{id}/character-types")]
public async Task<IActionResult> GetCharTypeByGameID(Guid id)
{
return Ok(await _characterTypeServices.GetByGameId(id));
return Ok(await _characterTypeServices.ListCharTypesByGameId(id));
}

[HttpGet("{id}/game-servers")]
public async Task<IActionResult> GetGameServerByGameID(Guid id)
{
return Ok(await _gameServerServices.GetByGameId(id));
return Ok(await _gameServerServices.ListServersByGameId(id));
}

[HttpGet("{id}/levels")]
public async Task<IActionResult> GetLevelByGameID(Guid id)
{
return Ok(await _levelServices.GetByGameId(id));
return Ok(await _levelServices.ListLevelsByGameId(id));
}

[HttpGet("{id}/wallet-category")]
public async Task<IActionResult> GetWalCatByGameID(Guid id)
{
return Ok(await _walletCategoryServices.GetByGameId(id));
return Ok(await _walletCategoryServices.ListWalCatsByGameId(id));
}

[HttpGet("{id}/users")]
public async Task<IActionResult> GetUsersByGameID(Guid id)
{
return Ok(await _gameUserServices.ListUsersByGameId(id));
}
[HttpPost]
public async Task<IActionResult> CreateGame([FromBody] CreateGameRequest newGame)
{
Expand Down
29 changes: 19 additions & 10 deletions Api/Controllers/LevelsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,13 @@ public class LevelsController : BaseController
}

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

[HttpGet("{id}")]
Expand All @@ -36,17 +39,23 @@ public async Task<IActionResult> GetLevel(Guid id)
}

[HttpPost]
public async Task<IActionResult> CreateLevel([FromBody] CreateLevelsController level)
public async Task<IActionResult> CreateLevel([FromBody] CreateLevelsRequest[] level)
{
await _gameRepo.FoundOrThrowAsync(level.GameId, Constants.ENTITY.GAME + Constants.ERROR.NOT_EXIST_ERROR);
var newLevel = new LevelEntity();
Mapper.Map(level, newLevel);
await _levelServices.Create(newLevel);
return CreatedAtAction(nameof(GetLevel), new { id = newLevel.Id }, newLevel);
List<LevelEntity> levelList = new List<LevelEntity>();
foreach (var singleLevel in level)
{
await _gameRepo.FoundOrThrowAsync(singleLevel.GameId, Constants.ENTITY.GAME +"id " + singleLevel.GameId + " " + Constants.ERROR.NOT_EXIST_ERROR);
await _levelServices.CheckForDuplicateLevel(singleLevel.Name, singleLevel.GameId);
LevelEntity newLevel = new LevelEntity();
Mapper.Map(singleLevel, newLevel);
levelList.Add(newLevel);
}
await _levelServices.Create(levelList);
return CreatedAtAction(nameof(GetLevels), new { ids = levelList.Select(l => l.Id).ToList() }, level);
}

[HttpPut("{id}")]
public async Task<IActionResult> UpdateLevel(Guid id, [FromBody] UpdateLevelsController level)
public async Task<IActionResult> UpdateLevel(Guid id, [FromBody] UpdateLevelsRequest level)
{
var updateLevel = await _levelRepo.FoundOrThrowAsync(id, Constants.ENTITY.LEVEL + Constants.ERROR.NOT_EXIST_ERROR);
Mapper.Map(level, updateLevel);
Expand Down
34 changes: 28 additions & 6 deletions Api/Controllers/UsersController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Microsoft.AspNetCore.Mvc;
using RepositoryLayer.Repositories;
using ServiceLayer.Business;
using System.Runtime.ConstrainedExecution;
using WebApiLayer.UserFeatures.Requests;

namespace WebApiLayer.Controllers;
Expand All @@ -11,24 +12,26 @@ namespace WebApiLayer.Controllers;
public class UsersController : BaseController
{
private readonly IUserServices _userServices;
private readonly IGameUserServices _gameUserServices;
private readonly IGenericRepository<UserEntity> _userRepo;
public UsersController(IUserServices userServices, IGenericRepository<UserEntity> userRepo)
private readonly IGenericRepository<GameUserEntity> _gameUserRepo;
public UsersController(IUserServices userServices, IGenericRepository<UserEntity> userRepo, IGameUserServices gameUserServices, IGenericRepository<GameUserEntity> gameUserRepo)
{
_userServices = userServices;
_userRepo = userRepo;
_gameUserServices = gameUserServices;
_gameUserRepo = gameUserRepo;
}
[HttpGet]
public async Task<IActionResult> GetUsers([FromQuery] string? email)
{
var users = await _userServices.List(email);
return Ok(users);
{
return Ok(await _userServices.List(email));
}

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

[HttpPost]
Expand All @@ -40,6 +43,17 @@ public async Task<IActionResult> CreateUser([FromBody] CreateUserRequest cUser)
return CreatedAtAction(nameof(GetUser), new { id = user.Id }, user);
}

[HttpPost("{id}/add-game")]
public async Task<IActionResult> CreateGameUser(Guid id, [FromQuery] Guid GameId)
{
var gameUser = new GameUserEntity {
UserId = id,
GameId = GameId
};
await _gameUserServices.Create(gameUser);
return CreatedAtAction(nameof(GetUser), new {id = id}, gameUser);
}

[HttpPut("{id}")]
public async Task<IActionResult> UpdateUser(Guid id, [FromBody] UpdateUserRequest user)
{
Expand All @@ -56,4 +70,12 @@ public async Task<IActionResult> DeleteUser(Guid id)
await _userServices.Delete(id);
return NoContent();
}

[HttpDelete("{id}/delete-game")]
public async Task<IActionResult> DeleteGameUser(Guid id)
{
await _gameUserRepo.FoundOrThrowAsync(id, Constants.ERROR.NOT_EXIST_ERROR);
await _gameUserServices.Delete(id);
return NoContent();
}
}
Loading

0 comments on commit 788332e

Please sign in to comment.