From 88002173a1155883d1fb46683a9a7ad1f521eb56 Mon Sep 17 00:00:00 2001 From: crupest Date: Fri, 30 Apr 2021 16:52:55 +0800 Subject: refactor: ... --- BackEnd/Timeline/Services/User/UserService.cs | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) (limited to 'BackEnd/Timeline/Services/User/UserService.cs') diff --git a/BackEnd/Timeline/Services/User/UserService.cs b/BackEnd/Timeline/Services/User/UserService.cs index 443afb90..a47bc860 100644 --- a/BackEnd/Timeline/Services/User/UserService.cs +++ b/BackEnd/Timeline/Services/User/UserService.cs @@ -54,9 +54,10 @@ namespace Timeline.Services.User } } - private static void ThrowUsernameConflict(object? user) + private static EntityAlreadyExistException CreateUsernameConflictException(string username) { - throw new UserAlreadyExistException(user); + throw new EntityAlreadyExistException(EntityTypes.User, + new Dictionary { ["username"] = username }); } public async Task GetUserAsync(long id) @@ -64,7 +65,7 @@ namespace Timeline.Services.User var user = await _databaseContext.Users.Where(u => u.Id == id).SingleOrDefaultAsync(); if (user is null) - throw new UserNotExistException(id); + throw CreateUserNotExistException(id); return user; } @@ -89,7 +90,7 @@ namespace Timeline.Services.User var conflict = await _databaseContext.Users.AnyAsync(u => u.Username == param.Username); if (conflict) - ThrowUsernameConflict(null); + throw CreateUsernameConflictException(param.Username); var newEntity = new UserEntity { @@ -120,8 +121,8 @@ namespace Timeline.Services.User } var entity = await _databaseContext.Users.Where(u => u.Id == id).SingleOrDefaultAsync(); - if (entity == null) - throw new UserNotExistException(id); + if (entity is null) + throw CreateUserNotExistException(id); if (param is not null) { @@ -133,7 +134,7 @@ namespace Timeline.Services.User { var conflict = await _databaseContext.Users.AnyAsync(u => u.Username == username); if (conflict) - ThrowUsernameConflict(null); + throw CreateUsernameConflictException(username); entity.Username = username; entity.UsernameChangeTime = now; @@ -180,7 +181,7 @@ namespace Timeline.Services.User if (entity is null) { _logger.LogInformation(Resource.LogVerifyCredentialsUsernameBad, username); - throw new UserNotExistException(username); + throw CreateUserNotExistException(username); } if (!_passwordService.VerifyPassword(entity.Password, password)) @@ -204,7 +205,7 @@ namespace Timeline.Services.User var entity = await _databaseContext.Users.Where(u => u.Id == id).SingleOrDefaultAsync(); if (entity is null) - throw new UserNotExistException(id); + throw CreateUserNotExistException(id); if (!_passwordService.VerifyPassword(entity.Password, oldPassword)) throw new BadPasswordException(oldPassword); -- cgit v1.2.3