diff options
author | crupest <crupest@outlook.com> | 2021-04-30 16:52:55 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2021-04-30 16:52:55 +0800 |
commit | 88002173a1155883d1fb46683a9a7ad1f521eb56 (patch) | |
tree | 742cce92b6e424d7bcd4a51a8da63dd10e18c4c7 /BackEnd/Timeline/Services/User | |
parent | bf4c48980f81e566065c07f3fe534ce7551ebdcc (diff) | |
download | timeline-88002173a1155883d1fb46683a9a7ad1f521eb56.tar.gz timeline-88002173a1155883d1fb46683a9a7ad1f521eb56.tar.bz2 timeline-88002173a1155883d1fb46683a9a7ad1f521eb56.zip |
refactor: ...
Diffstat (limited to 'BackEnd/Timeline/Services/User')
10 files changed, 59 insertions, 102 deletions
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 /// </summary>
/// <param name="userId">User id.</param>
/// <returns>The avatar digest.</returns>
- /// <exception cref="UserNotExistException">Thrown when user does not exist.</exception>
+ /// <exception cref="EntityNotExistException">Thrown when user does not exist.</exception>
Task<ICacheableDataDigest> GetAvatarDigestAsync(long userId);
/// <summary>
@@ -21,7 +21,7 @@ namespace Timeline.Services.User.Avatar /// </summary>
/// <param name="userId">User id.</param>
/// <returns>The avatar.</returns>
- /// <exception cref="UserNotExistException">Thrown when user does not exist.</exception>
+ /// <exception cref="EntityNotExistException">Thrown when user does not exist.</exception>
Task<ByteData> GetAvatarAsync(long userId);
/// <summary>
@@ -31,7 +31,7 @@ namespace Timeline.Services.User.Avatar /// <param name="avatar">The new avatar data.</param>
/// <returns>The digest of the avatar.</returns>
/// <exception cref="ArgumentNullException">Thrown if <paramref name="avatar"/> is null.</exception>
- /// <exception cref="UserNotExistException">Thrown when user does not exist.</exception>
+ /// <exception cref="EntityNotExistException">Thrown when user does not exist.</exception>
/// <exception cref="ImageException">Thrown if avatar is of bad format.</exception>
Task<ICacheableDataDigest> SetAvatarAsync(long userId, ByteData avatar);
@@ -39,7 +39,7 @@ namespace Timeline.Services.User.Avatar /// Remove avatar of a user.
/// </summary>
/// <param name="userId">User id.</param>
- /// <exception cref="UserNotExistException">Thrown when user does not exist.</exception>
+ /// <exception cref="EntityNotExistException">Thrown when user does not exist.</exception>
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<string, object> { ["username"] = username });
+ }
+
+ protected static EntityNotExistException CreateUserNotExistException(long id)
+ {
+ return new EntityNotExistException(EntityTypes.User,
+ new Dictionary<string, object> { ["id"] = id });
+ }
+
public async Task<bool> 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<string, object> { ["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 /// <returns>The id of the user.</returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="username"/> is null.</exception>
/// <exception cref="ArgumentException">Thrown when <paramref name="username"/> is of bad format.</exception>
- /// <exception cref="UserNotExistException">Thrown when the user with given username does not exist.</exception>
+ /// <exception cref="EntityNotExistException">Thrown when the user with given username does not exist.</exception>
Task<long> GetUserIdByUsernameAsync(string username);
/// <summary>
@@ -30,7 +30,7 @@ namespace Timeline.Services.User /// </summary>
/// <param name="userId">User id.</param>
/// <returns>The time.</returns>
- /// <exception cref="UserNotExistException">Thrown when user does not exist.</exception>
+ /// <exception cref="EntityNotExistException">Thrown when user does not exist.</exception>
Task<DateTime> 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 /// <param name="userId">The id of the user.</param>
/// <param name="checkUserExistence">Whether check the user's existence.</param>
/// <returns>The permission list.</returns>
- /// <exception cref="UserNotExistException">Thrown when <paramref name="checkUserExistence"/> is true and user does not exist.</exception>
+ /// <exception cref="EntityNotExistException">Thrown when <paramref name="checkUserExistence"/> is true and user does not exist.</exception>
Task<UserPermissions> GetPermissionsOfUserAsync(long userId, bool checkUserExistence = true);
/// <summary>
@@ -18,7 +18,7 @@ namespace Timeline.Services.User /// </summary>
/// <param name="userId">The id of the user.</param>
/// <param name="permission">The new permission.</param>
- /// <exception cref="UserNotExistException">Thrown when user does not exist.</exception>
+ /// <exception cref="EntityNotExistException">Thrown when user does not exist.</exception>
/// <exception cref="InvalidOperationOnRootUserException">Thrown when change root user's permission.</exception>
Task AddPermissionToUserAsync(long userId, UserPermission permission);
@@ -28,7 +28,7 @@ namespace Timeline.Services.User /// <param name="userId">The id of the user.</param>
/// <param name="permission">The permission.</param>
/// <param name="checkUserExistence">Whether check the user's existence.</param>
- /// <exception cref="UserNotExistException">Thrown when <paramref name="checkUserExistence"/> is true and user does not exist.</exception>
+ /// <exception cref="EntityNotExistException">Thrown when <paramref name="checkUserExistence"/> is true and user does not exist.</exception>
/// <exception cref="InvalidOperationOnRootUserException">Thrown when change root user's permission.</exception>
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 /// </summary>
/// <param name="id">The id of the user.</param>
/// <returns>The user info.</returns>
- /// <exception cref="UserNotExistException">Thrown when the user with given id does not exist.</exception>
+ /// <exception cref="EntityNotExistException">Thrown when the user with given id does not exist.</exception>
Task<UserEntity> GetUserAsync(long id);
/// <summary>
@@ -38,7 +38,7 @@ namespace Timeline.Services.User /// <param name="param">The new information.</param>
/// <returns>The new user info.</returns>
/// <exception cref="ArgumentException">Thrown when some fields in <paramref name="param"/> is bad.</exception>
- /// <exception cref="UserNotExistException">Thrown when user with given id does not exist.</exception>
+ /// <exception cref="EntityNotExistException">Thrown when user with given id does not exist.</exception>
/// <remarks>
/// Version will increase if password is changed.
/// </remarks>
@@ -52,7 +52,7 @@ namespace Timeline.Services.User /// <returns>User id.</returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="username"/> or <paramref name="password"/> is null.</exception>
/// <exception cref="ArgumentException">Thrown when <paramref name="username"/> is of bad format or <paramref name="password"/> is empty.</exception>
- /// <exception cref="UserNotExistException">Thrown when the user with given username does not exist.</exception>
+ /// <exception cref="EntityNotExistException">Thrown when the user with given username does not exist.</exception>
/// <exception cref="BadPasswordException">Thrown when password is wrong.</exception>
Task<long> VerifyCredential(string username, string password);
@@ -64,7 +64,7 @@ namespace Timeline.Services.User /// <param name="newPassword">New password.</param>
/// <exception cref="ArgumentNullException">Thrown if <paramref name="oldPassword"/> or <paramref name="newPassword"/> is null.</exception>
/// <exception cref="ArgumentException">Thrown if <paramref name="oldPassword"/> or <paramref name="newPassword"/> is empty.</exception>
- /// <exception cref="UserNotExistException">Thrown if the user with given username does not exist.</exception>
+ /// <exception cref="EntityNotExistException">Thrown if the user with given username does not exist.</exception>
/// <exception cref="BadPasswordException">Thrown if the old password is wrong.</exception>
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
-{
- /// <summary>
- /// The user requested does not exist.
- /// </summary>
- [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
-{
- /// <summary>
- /// The user requested does not exist.
- /// </summary>
- [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) { }
-
- /// <summary>
- /// The username of the user that does not exist.
- /// </summary>
- public string? Username { get; set; }
-
- /// <summary>
- /// The id of the user that does not exist.
- /// </summary>
- 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<string, object> { ["username"] = username });
}
public async Task<UserEntity> 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);
|