Skip to content

Commit

Permalink
Change Error Code. 204 => 404 (#18)
Browse files Browse the repository at this point in the history
* Change Error Code. 204 => 404

* Create GetByGameID (Activity_Type, Asset_Type, Wallet_Category, Game_Server, Level, Character_Type)

* Add: GetUserByEmail

* Add 's' to the end of entity

* Update GetByEmail Section & MockData Running

* Update User.List()
  • Loading branch information
AnhQuoc2906 committed Nov 11, 2023
1 parent 39fbca9 commit cef7fef
Show file tree
Hide file tree
Showing 25 changed files with 121 additions and 48 deletions.
4 changes: 2 additions & 2 deletions Api/Configurations/DatabaseInitializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ public static async Task MockUser(this ApplicationDbContext dbContext)

dynamic user = SeedingServices.LoadJson("USER_MOCK_DATA.json");
int userLength = user.Count;
for (int i = 0; i < 20; i++)
for (int i = 0; i < user.Count; i++)
{
var mockUser = user[_rand.Next(userLength)];
var mockUser = user[i];
await dbContext.Users.AddAsync(
new UserEntity()
{
Expand Down
54 changes: 53 additions & 1 deletion Api/Controllers/GamesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,27 @@ namespace WebApiLayer.Controllers;
public class GamesController : BaseController
{
private readonly IGameServices _gameServices;
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 IGenericRepository<GameEntity> _gameRepo;
private readonly IGenericRepository<UserEntity> _userRepo;
public GamesController(IGameServices gameServices, IGenericRepository<GameEntity> gameRepo, IGenericRepository<UserEntity> userRepo)
public GamesController(IGameServices gameServices, IActivityTypeServices activityTypeServices
, IAssetTypeServices assetTypeServices
, ICharacterTypeServices characterTypeServices , IGameServerServices gameServerServices
, ILevelServices levelServices, IWalletCategoryServices walletCategoryServices
, IGenericRepository<GameEntity> gameRepo, IGenericRepository<UserEntity> userRepo)
{
_gameServices = gameServices;
_activityTypeServices = activityTypeServices;
_assetTypeServices = assetTypeServices;
_characterTypeServices = characterTypeServices;
_gameServerServices = gameServerServices;
_levelServices = levelServices;
_walletCategoryServices = walletCategoryServices;
_gameRepo = gameRepo;
_userRepo = userRepo;
}
Expand All @@ -36,6 +52,42 @@ public async Task<IActionResult> GetGame(Guid id)
return Ok(await _gameServices.GetById(id));
}

[HttpGet("{id}/activity-types")]
public async Task<IActionResult> GetActTypeByGameID(Guid id)
{
return Ok(await _activityTypeServices.GetByGameId(id));
}

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

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

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

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

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

[HttpPost]
public async Task<IActionResult> CreateGame([FromBody] CreateGameRequest newGame)
{
Expand Down
4 changes: 2 additions & 2 deletions Api/Controllers/UsersController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ public UsersController(IUserServices userServices, IGenericRepository<UserEntity
_userRepo = userRepo;
}
[HttpGet]
public async Task<IActionResult> GetUsers()
public async Task<IActionResult> GetUsers([FromQuery] string? email)
{
var users = await _userServices.List();
var users = await _userServices.List(email);
return Ok(users);
}

Expand Down
5 changes: 3 additions & 2 deletions Application/Business/Activity/ActivityServices.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using DomainLayer.Entities;
using DomainLayer.Constants;
using DomainLayer.Entities;
using RepositoryLayer.Repositories;

namespace ServiceLayer.Business;
Expand All @@ -16,7 +17,7 @@ public ActivityServices(IGenericRepository<ActivityEntity> activityRepo)
}
public async Task<ActivityEntity> Search(Guid activityId)
{
return await _activityRepo.FindByIdAsync(activityId);
return await _activityRepo.FoundOrThrowAsync(activityId, Constants.ENTITY.ACTIVITY + Constants.ERROR.NOT_EXIST_ERROR);
}
public async Task<ActivityEntity> Search(string Name) {
return await _activityRepo.FirstOrDefaultAsync( a => a.Name.Equals(Name));
Expand Down
5 changes: 3 additions & 2 deletions Application/Business/ActivityType/ActivityTypeServices.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using DomainLayer.Entities;
using DomainLayer.Constants;
using DomainLayer.Entities;
using RepositoryLayer.Repositories;

namespace ServiceLayer.Business;
Expand All @@ -17,7 +18,7 @@ public async Task<ICollection<ActivityTypeEntity>> List()
}
public async Task<ActivityTypeEntity> GetById(Guid activityTypeId)
{
return await _activityTypeRepo.FindByIdAsync(activityTypeId);
return await _activityTypeRepo.FoundOrThrowAsync(activityTypeId, Constants.ENTITY.ACTIVITY_TYPE + Constants.ERROR.NOT_EXIST_ERROR);
}
public async Task<ICollection<ActivityTypeEntity>> GetByGameId(Guid gameid)
{
Expand Down
5 changes: 3 additions & 2 deletions Application/Business/Asset/AssetServices.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using DomainLayer.Entities;
using DomainLayer.Constants;
using DomainLayer.Entities;
using RepositoryLayer.Repositories;

namespace ServiceLayer.Business;
Expand All @@ -17,7 +18,7 @@ public async Task<ICollection<AssetEntity>> List()
}
public async Task<AssetEntity> GetById(Guid assetId)
{
return await _assetRepo.FindByIdAsync(assetId);
return await _assetRepo.FoundOrThrowAsync(assetId, Constants.ENTITY.ASSET + Constants.ERROR.NOT_EXIST_ERROR);
}
public async Task<ICollection<AssetEntity>> GetByAssetTypeId(Guid assetTypeid)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public async Task<ICollection<AssetAttributeEntity>> List()
}
public async Task<AssetAttributeEntity> GetById(Guid assetAttributeId)
{
return await _assetAttributeRepo.FindByIdAsync(assetAttributeId);
return await _assetAttributeRepo.FoundOrThrowAsync(assetAttributeId, Constants.ENTITY.ASSET_ATTRIBUTE + Constants.ERROR.NOT_EXIST_ERROR);
}
public async Task<ICollection<AssetAttributeEntity>> GetByAssetId(Guid assetId)
{
Expand Down
2 changes: 1 addition & 1 deletion Application/Business/AssetType/AssetTypeServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public async Task<ICollection<AssetTypeEntity>> List()
}
public async Task<AssetTypeEntity> GetById(Guid assetTypeId)
{
return await _assetTypeRepo.FindByIdAsync(assetTypeId);
return await _assetTypeRepo.FoundOrThrowAsync(assetTypeId, Constants.ENTITY.ASSET_ATTRIBUTE + Constants.ERROR.NOT_EXIST_ERROR);
}
public async Task<ICollection<AssetTypeEntity>> GetByGameId(Guid gameId)
{
Expand Down
10 changes: 3 additions & 7 deletions Application/Business/AttributeGroup/AttributeGroupServices.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
using DomainLayer.Entities;
using DomainLayer.Exceptions;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore.ValueGeneration;
using DomainLayer.Constants;
using DomainLayer.Entities;
using RepositoryLayer.Repositories;
using System.Reflection;
using System.Security.Cryptography.X509Certificates;

namespace ServiceLayer.Business;

Expand All @@ -21,7 +17,7 @@ public async Task<ICollection<AttributeGroupEntity>> List()
}
public async Task<AttributeGroupEntity> GetById(Guid attributeGroupid)
{
return await _attributeRepo.FindByIdAsync(attributeGroupid);
return await _attributeRepo.FoundOrThrowAsync(attributeGroupid, Constants.ENTITY.ATTRIBUTE_GROUP + Constants.ERROR.NOT_EXIST_ERROR);
}
public async Task<int> Count()
{
Expand Down
3 changes: 2 additions & 1 deletion Application/Business/Character/CharacterServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ public async Task<ICollection<CharacterEntity>> List()
}
public async Task<CharacterEntity> GetById(Guid characterId)
{
return await _characterRepo.FindByIdAsync(characterId);
return await _characterRepo.FoundOrThrowAsync(characterId, Constants.ENTITY.CHARACTER + Constants.ERROR.NOT_EXIST_ERROR);

}
public async Task<ICollection<CharacterEntity>> GetByUserId(Guid id)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ public async Task<ICollection<CharacterAssetEntity>> List()
}
public async Task<CharacterAssetEntity> GetById(Guid characterAssetId)
{
return await _characterAssetRepo.FindByIdAsync(characterAssetId);
return await _characterAssetRepo.FoundOrThrowAsync(characterAssetId, Constants.ENTITY.CHARACTER_ASSET + Constants.ERROR.NOT_EXIST_ERROR);

}
public async Task<ICollection<CharacterAssetEntity>> GetByAssetId(Guid id)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using DomainLayer.Entities;
using DomainLayer.Constants;
using DomainLayer.Entities;
using DomainLayer.Exceptions;
using RepositoryLayer.Repositories;

Expand All @@ -18,7 +19,9 @@ public async Task<ICollection<CharacterAttributeEntity>> List()
}
public async Task<CharacterAttributeEntity> GetById(Guid characterAttributeid)
{
return await _characterAttributeRepo.FindByIdAsync(characterAttributeid);
return await _characterAttributeRepo.FoundOrThrowAsync(characterAttributeid,
Constants.ENTITY.CHARACTER_ATTRIBUTE + Constants.ERROR.NOT_EXIST_ERROR);

}
public async Task<ICollection<CharacterAttributeEntity>> GetByCharacterId(Guid id)
{
Expand Down
6 changes: 4 additions & 2 deletions Application/Business/CharacterType/CharacterTypeServices.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using DomainLayer.Entities;
using DomainLayer.Constants;
using DomainLayer.Entities;
using DomainLayer.Exceptions;
using RepositoryLayer.Repositories;

Expand All @@ -20,7 +21,8 @@ public async Task<ICollection<CharacterTypeEntity>> List()
}
public async Task<CharacterTypeEntity> GetById(Guid characterTypeId)
{
return await _characterTypeRepo.FindByIdAsync(characterTypeId);
return await _characterTypeRepo.FoundOrThrowAsync(characterTypeId,
Constants.ENTITY.CHARACTER_TYPE + Constants.ERROR.NOT_EXIST_ERROR);
}
public async Task<ICollection<CharacterTypeEntity>> GetByGameId(Guid gameId)
{
Expand Down
6 changes: 4 additions & 2 deletions Application/Business/GameServer/GameServerServices.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using DomainLayer.Entities;
using DomainLayer.Constants;
using DomainLayer.Entities;
using DomainLayer.Exceptions;
using RepositoryLayer.Repositories;

Expand All @@ -19,7 +20,8 @@ public async Task<ICollection<GameServerEntity>> List()
}
public async Task<GameServerEntity> GetById(Guid gameServerId)
{
return await _gameServerRepo.FindByIdAsync(gameServerId);
return await _gameServerRepo.FoundOrThrowAsync(gameServerId,
Constants.ENTITY.GAME_SERVER + Constants.ERROR.NOT_EXIST_ERROR);
}
public async Task<ICollection<GameServerEntity>> GetByGameId(Guid gameId)
{
Expand Down
3 changes: 2 additions & 1 deletion Application/Business/GameServices/GameServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ public async Task<ICollection<GameEntity>> List()
}
public async Task<GameEntity> GetById(Guid gameId)
{
return await _gameRepo.FindByIdAsync(gameId);
return await _gameRepo.FoundOrThrowAsync(gameId,
Constants.ENTITY.GAME + Constants.ERROR.NOT_EXIST_ERROR);
}
public async Task<ICollection<GameEntity>> List(Guid[] gameIds)
{
Expand Down
3 changes: 2 additions & 1 deletion Application/Business/LevelProgress/LevelProgressServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ public async Task<ICollection<LevelProgressEntity>> List()
}
public async Task<LevelProgressEntity> GetById(Guid levelProgressId)
{
return await _levelProgressRepo.FindByIdAsync(levelProgressId);
return await _levelProgressRepo.FoundOrThrowAsync(levelProgressId,
Constants.ENTITY.LEVEL_PROGRESS + Constants.ERROR.NOT_EXIST_ERROR);
}
public async Task<ICollection<LevelProgressEntity>> GetByCharacterId(Guid id)
{
Expand Down
3 changes: 2 additions & 1 deletion Application/Business/LevelServices/LevelServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ public async Task<ICollection<LevelEntity>> List()
}
public async Task<LevelEntity> GetById(Guid levelId)
{
return await _levelRepo.FindByIdAsync(levelId);
return await _levelRepo.FoundOrThrowAsync(levelId,
Constants.ENTITY.LEVEL + Constants.ERROR.NOT_EXIST_ERROR);
}
public async Task<ICollection<LevelEntity>> GetByGameId(Guid gameId)
{
Expand Down
6 changes: 4 additions & 2 deletions Application/Business/PaymentServices/PaymentServices.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using DomainLayer.Entities;
using DomainLayer.Constants;
using DomainLayer.Entities;
using DomainLayer.Exceptions;
using RepositoryLayer.Repositories;

Expand All @@ -18,7 +19,8 @@ public async Task<ICollection<PaymentEntity>> List()
}
public async Task<PaymentEntity> GetById(Guid paymentId)
{
return await _paymentRepo.FindByIdAsync(paymentId);
return await _paymentRepo.FoundOrThrowAsync(paymentId,
Constants.ENTITY.PAYMENT + Constants.ERROR.NOT_EXIST_ERROR);
}
public async Task<ICollection<PaymentEntity>> GetByCharacterId(Guid id)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using DomainLayer.Entities;
using DomainLayer.Constants;
using DomainLayer.Entities;
using RepositoryLayer.Repositories;

namespace ServiceLayer.Business;
Expand All @@ -16,7 +17,8 @@ public async Task<ICollection<TransactionEntity>> List()
}
public async Task<TransactionEntity> GetById(Guid transactionId)
{
return await _transactionRepo.FindByIdAsync(transactionId);
return await _transactionRepo.FoundOrThrowAsync(transactionId,
Constants.ENTITY.TRANSACTION + Constants.ERROR.NOT_EXIST_ERROR);
}
public async Task<ICollection<TransactionEntity>> GetByWalletId(Guid walletId)
{
Expand Down
2 changes: 1 addition & 1 deletion Application/Business/UserServices/IUserServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace ServiceLayer.Business;
public interface IUserServices
{
Task<ICollection<UserEntity>> List();
Task<ICollection<UserEntity>> List(string? email);
Task<UserEntity> GetById(Guid UserId);
Task<int> Count();
Task Create(UserEntity user);
Expand Down
16 changes: 10 additions & 6 deletions Application/Business/UserServices/UserServices.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
using AutoMapper;
using DomainLayer.Constants;
using DomainLayer.Entities;
using DomainLayer.Exceptions;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.AspNetCore.Mvc;
using RepositoryLayer.Repositories;
using ServiceLayer.Extensions;
using System.Data;

namespace ServiceLayer.Business;

Expand All @@ -15,13 +13,19 @@ public UserServices(IGenericRepository<UserEntity> userRepo)
{
_userRepo = userRepo;
}
public async Task<ICollection<UserEntity>> List()
public async Task<ICollection<UserEntity>> List(string? email)
{
if (!string.IsNullOrEmpty(email))
{
var user = await _userRepo.WhereAsync(u => u.Email.Equals(email));
return user;
}
return await _userRepo.ListAsync();
}
public async Task<UserEntity> GetById(Guid UserId)
{
return await _userRepo.FindByIdAsync(UserId);
return await _userRepo.FoundOrThrowAsync(UserId,
Constants.ENTITY.USER + Constants.ERROR.NOT_EXIST_ERROR);
}
public async Task<int> Count()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ public async Task<ICollection<WalletCategoryEntity>> List()
}
public async Task<WalletCategoryEntity> GetById(Guid categoryId)
{
return await _walletCategoryRepo.FindByIdAsync(categoryId);
return await _walletCategoryRepo.FoundOrThrowAsync(categoryId,
Constants.ENTITY.WALLET_CATEGORY + Constants.ERROR.NOT_EXIST_ERROR);
}
public async Task<ICollection<WalletCategoryEntity>> GetByGameId(Guid gameId)
{
Expand Down
3 changes: 2 additions & 1 deletion Application/Business/WalletServices/WalletServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ public async Task<ICollection<WalletEntity>> List()
}
public async Task<WalletEntity> GetById(Guid walletId)
{
return await _walletRepo.FindByIdAsync(walletId);
return await _walletRepo.FoundOrThrowAsync(walletId,
Constants.ENTITY.WALLET + Constants.ERROR.NOT_EXIST_ERROR);
}
public async Task<ICollection<WalletEntity>> GetByWalletCategoryId(Guid id)
{
Expand Down
4 changes: 2 additions & 2 deletions Domain/Entities/GameEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ public class GameEntity : BaseEntity
public string Name { get; set; }
public string Logo { get; set; }
public string Link { get; set; }
// M Game - M User
// M Users - M Games
public virtual ICollection<UserEntity>? Users { get; set; }
// 1 Game - M Activity Type
public virtual ICollection<ActivityTypeEntity>? ActivityTypes { get; set; }
// M Game - M Attribute Group
public virtual ICollection<AttributeGroupEntity>? AttributeGroups { get; set; }
}
}
Loading

0 comments on commit cef7fef

Please sign in to comment.