From 88002173a1155883d1fb46683a9a7ad1f521eb56 Mon Sep 17 00:00:00 2001 From: crupest Date: Fri, 30 Apr 2021 16:52:55 +0800 Subject: refactor: ... --- .../Services/User/Avatar/IUserAvatarService.cs | 8 ++--- BackEnd/Timeline/Services/User/BasicUserService.cs | 28 ++++++++-------- .../Services/User/BasicUserServiceExtensions.cs | 17 ++++++++++ .../Timeline/Services/User/IBasicUserService.cs | 4 +-- .../Services/User/IUserPermissionService.cs | 6 ++-- BackEnd/Timeline/Services/User/IUserService.cs | 8 ++--- .../Services/User/UserAlreadyExistException.cs | 24 -------------- .../Services/User/UserNotExistException.cs | 37 ---------------------- .../Services/User/UserPermissionService.cs | 10 +++--- BackEnd/Timeline/Services/User/UserService.cs | 19 +++++------ 10 files changed, 59 insertions(+), 102 deletions(-) create mode 100644 BackEnd/Timeline/Services/User/BasicUserServiceExtensions.cs delete mode 100644 BackEnd/Timeline/Services/User/UserAlreadyExistException.cs delete mode 100644 BackEnd/Timeline/Services/User/UserNotExistException.cs (limited to 'BackEnd/Timeline/Services/User') diff --git a/BackEnd/Timeline/Services/User/Avatar/IUserAvatarService.cs b/BackEnd/Timeline/Services/User/Avatar/IUserAvatarService.cs index 7ec855aa..92f2429c 100644 --- a/BackEnd/Timeline/Services/User/Avatar/IUserAvatarService.cs +++ b/BackEnd/Timeline/Services/User/Avatar/IUserAvatarService.cs @@ -13,7 +13,7 @@ namespace Timeline.Services.User.Avatar /// /// User id. /// The avatar digest. - /// Thrown when user does not exist. + /// Thrown when user does not exist. Task GetAvatarDigestAsync(long userId); /// @@ -21,7 +21,7 @@ namespace Timeline.Services.User.Avatar /// /// User id. /// The avatar. - /// Thrown when user does not exist. + /// Thrown when user does not exist. Task GetAvatarAsync(long userId); /// @@ -31,7 +31,7 @@ namespace Timeline.Services.User.Avatar /// The new avatar data. /// The digest of the avatar. /// Thrown if is null. - /// Thrown when user does not exist. + /// Thrown when user does not exist. /// Thrown if avatar is of bad format. Task SetAvatarAsync(long userId, ByteData avatar); @@ -39,7 +39,7 @@ namespace Timeline.Services.User.Avatar /// Remove avatar of a user. /// /// User id. - /// Thrown when user does not exist. + /// Thrown when user does not exist. Task DeleteAvatarAsync(long userId); } } diff --git a/BackEnd/Timeline/Services/User/BasicUserService.cs b/BackEnd/Timeline/Services/User/BasicUserService.cs index 1f1b25f5..0ee8dabd 100644 --- a/BackEnd/Timeline/Services/User/BasicUserService.cs +++ b/BackEnd/Timeline/Services/User/BasicUserService.cs @@ -1,5 +1,6 @@ using Microsoft.EntityFrameworkCore; using System; +using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Timeline.Entities; @@ -18,6 +19,18 @@ namespace Timeline.Services.User _database = database; } + protected static EntityNotExistException CreateUserNotExistException(string username) + { + return new EntityNotExistException(EntityTypes.User, + new Dictionary { ["username"] = username }); + } + + protected static EntityNotExistException CreateUserNotExistException(long id) + { + return new EntityNotExistException(EntityTypes.User, + new Dictionary { ["id"] = id }); + } + public async Task CheckUserExistenceAsync(long id) { return await _database.Users.AnyAsync(u => u.Id == id); @@ -34,7 +47,7 @@ namespace Timeline.Services.User var entity = await _database.Users.Where(user => user.Username == username).Select(u => new { u.Id }).SingleOrDefaultAsync(); if (entity == null) - throw new UserNotExistException(username); + throw CreateUserNotExistException(username); return entity.Id; } @@ -44,20 +57,9 @@ namespace Timeline.Services.User var entity = await _database.Users.Where(u => u.Id == userId).Select(u => new { u.UsernameChangeTime }).SingleOrDefaultAsync(); if (entity is null) - throw new UserNotExistException(userId); + throw CreateUserNotExistException(userId); return entity.UsernameChangeTime; } } - - public static class BasicUserServiceExtensions - { - public static async Task ThrowIfUserNotExist(this IBasicUserService service, long userId) - { - if (!await service.CheckUserExistenceAsync(userId)) - { - throw new UserNotExistException(userId); - } - } - } } diff --git a/BackEnd/Timeline/Services/User/BasicUserServiceExtensions.cs b/BackEnd/Timeline/Services/User/BasicUserServiceExtensions.cs new file mode 100644 index 00000000..8a05f452 --- /dev/null +++ b/BackEnd/Timeline/Services/User/BasicUserServiceExtensions.cs @@ -0,0 +1,17 @@ +using System.Collections.Generic; +using System.Threading.Tasks; + +namespace Timeline.Services.User +{ + public static class BasicUserServiceExtensions + { + public static async Task ThrowIfUserNotExist(this IBasicUserService service, long userId) + { + if (!await service.CheckUserExistenceAsync(userId)) + { + throw new EntityNotExistException(EntityTypes.User, + new Dictionary { ["id"] = userId }); + } + } + } +} diff --git a/BackEnd/Timeline/Services/User/IBasicUserService.cs b/BackEnd/Timeline/Services/User/IBasicUserService.cs index 0e30d733..0ae3fdff 100644 --- a/BackEnd/Timeline/Services/User/IBasicUserService.cs +++ b/BackEnd/Timeline/Services/User/IBasicUserService.cs @@ -22,7 +22,7 @@ namespace Timeline.Services.User /// The id of the user. /// Thrown when is null. /// Thrown when is of bad format. - /// Thrown when the user with given username does not exist. + /// Thrown when the user with given username does not exist. Task GetUserIdByUsernameAsync(string username); /// @@ -30,7 +30,7 @@ namespace Timeline.Services.User /// /// User id. /// The time. - /// Thrown when user does not exist. + /// Thrown when user does not exist. Task GetUsernameLastModifiedTimeAsync(long userId); } } diff --git a/BackEnd/Timeline/Services/User/IUserPermissionService.cs b/BackEnd/Timeline/Services/User/IUserPermissionService.cs index 7ff1275b..985a67f5 100644 --- a/BackEnd/Timeline/Services/User/IUserPermissionService.cs +++ b/BackEnd/Timeline/Services/User/IUserPermissionService.cs @@ -10,7 +10,7 @@ namespace Timeline.Services.User /// The id of the user. /// Whether check the user's existence. /// The permission list. - /// Thrown when is true and user does not exist. + /// Thrown when is true and user does not exist. Task GetPermissionsOfUserAsync(long userId, bool checkUserExistence = true); /// @@ -18,7 +18,7 @@ namespace Timeline.Services.User /// /// The id of the user. /// The new permission. - /// Thrown when user does not exist. + /// Thrown when user does not exist. /// Thrown when change root user's permission. Task AddPermissionToUserAsync(long userId, UserPermission permission); @@ -28,7 +28,7 @@ namespace Timeline.Services.User /// The id of the user. /// The permission. /// Whether check the user's existence. - /// Thrown when is true and user does not exist. + /// Thrown when is true and user does not exist. /// Thrown when change root user's permission. Task RemovePermissionFromUserAsync(long userId, UserPermission permission, bool checkUserExistence = true); } diff --git a/BackEnd/Timeline/Services/User/IUserService.cs b/BackEnd/Timeline/Services/User/IUserService.cs index 06155c55..745bd524 100644 --- a/BackEnd/Timeline/Services/User/IUserService.cs +++ b/BackEnd/Timeline/Services/User/IUserService.cs @@ -12,7 +12,7 @@ namespace Timeline.Services.User /// /// The id of the user. /// The user info. - /// Thrown when the user with given id does not exist. + /// Thrown when the user with given id does not exist. Task GetUserAsync(long id); /// @@ -38,7 +38,7 @@ namespace Timeline.Services.User /// The new information. /// The new user info. /// Thrown when some fields in is bad. - /// Thrown when user with given id does not exist. + /// Thrown when user with given id does not exist. /// /// Version will increase if password is changed. /// @@ -52,7 +52,7 @@ namespace Timeline.Services.User /// User id. /// Thrown when or is null. /// Thrown when is of bad format or is empty. - /// Thrown when the user with given username does not exist. + /// Thrown when the user with given username does not exist. /// Thrown when password is wrong. Task VerifyCredential(string username, string password); @@ -64,7 +64,7 @@ namespace Timeline.Services.User /// New password. /// Thrown if or is null. /// Thrown if or is empty. - /// Thrown if the user with given username does not exist. + /// Thrown if the user with given username does not exist. /// Thrown if the old password is wrong. Task ChangePassword(long id, string oldPassword, string newPassword); } diff --git a/BackEnd/Timeline/Services/User/UserAlreadyExistException.cs b/BackEnd/Timeline/Services/User/UserAlreadyExistException.cs deleted file mode 100644 index e257af74..00000000 --- a/BackEnd/Timeline/Services/User/UserAlreadyExistException.cs +++ /dev/null @@ -1,24 +0,0 @@ -using System; - -namespace Timeline.Services.User -{ - /// - /// The user requested does not exist. - /// - [Serializable] - public class UserAlreadyExistException : EntityAlreadyExistException - { - public UserAlreadyExistException() : this(null, null, null) { } - public UserAlreadyExistException(object? entity) : this(entity, null, null) { } - public UserAlreadyExistException(object? entity, Exception? inner) : this(entity, null, inner) { } - public UserAlreadyExistException(object? entity, string? message, Exception? inner) - : base(EntityNames.User, entity, message ?? Resource.ExceptionUserAlreadyExist, inner) - { - - } - - protected UserAlreadyExistException( - System.Runtime.Serialization.SerializationInfo info, - System.Runtime.Serialization.StreamingContext context) : base(info, context) { } - } -} diff --git a/BackEnd/Timeline/Services/User/UserNotExistException.cs b/BackEnd/Timeline/Services/User/UserNotExistException.cs deleted file mode 100644 index bc5d8d9e..00000000 --- a/BackEnd/Timeline/Services/User/UserNotExistException.cs +++ /dev/null @@ -1,37 +0,0 @@ -using System; - -namespace Timeline.Services.User -{ - /// - /// The user requested does not exist. - /// - [Serializable] - public class UserNotExistException : EntityNotExistException - { - public UserNotExistException() : this(null, null, null, null) { } - public UserNotExistException(string? username) : this(username, null, null, null) { } - public UserNotExistException(string? username, Exception? inner) : this(username, null, null, inner) { } - public UserNotExistException(long id) : this(null, id, null, null) { } - public UserNotExistException(long id, Exception? inner) : this(null, id, null, inner) { } - public UserNotExistException(string? username, long? id, string? message, Exception? inner) - : base(EntityNames.User, message ?? Resource.ExceptionUserNotExist, inner) - { - Username = username; - Id = id; - } - - protected UserNotExistException( - System.Runtime.Serialization.SerializationInfo info, - System.Runtime.Serialization.StreamingContext context) : base(info, context) { } - - /// - /// The username of the user that does not exist. - /// - public string? Username { get; set; } - - /// - /// The id of the user that does not exist. - /// - public long? Id { get; set; } - } -} diff --git a/BackEnd/Timeline/Services/User/UserPermissionService.cs b/BackEnd/Timeline/Services/User/UserPermissionService.cs index f9911c7f..f6f11c61 100644 --- a/BackEnd/Timeline/Services/User/UserPermissionService.cs +++ b/BackEnd/Timeline/Services/User/UserPermissionService.cs @@ -8,21 +8,19 @@ namespace Timeline.Services.User public class UserPermissionService : IUserPermissionService { private readonly DatabaseContext _database; + private readonly IBasicUserService _basicUserService; - public UserPermissionService(DatabaseContext database) + public UserPermissionService(DatabaseContext database, IBasicUserService basicUserService) { _database = database; + _basicUserService = basicUserService; } private async Task CheckUserExistence(long userId, bool checkUserExistence) { if (checkUserExistence) { - var existence = await _database.Users.AnyAsync(u => u.Id == userId); - if (!existence) - { - throw new UserNotExistException(userId); - } + await _basicUserService.ThrowIfUserNotExist(userId); } } 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