From df1ef1e21d8d889a2c9abd440039533c6a43818f Mon Sep 17 00:00:00 2001 From: crupest Date: Thu, 7 Jan 2021 16:23:20 +0800 Subject: 史诗级重构! MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BackEnd/Timeline/Services/UserService.cs | 56 ++++++++++---------------------- 1 file changed, 17 insertions(+), 39 deletions(-) (limited to 'BackEnd/Timeline/Services/UserService.cs') diff --git a/BackEnd/Timeline/Services/UserService.cs b/BackEnd/Timeline/Services/UserService.cs index c99e86b0..d341759c 100644 --- a/BackEnd/Timeline/Services/UserService.cs +++ b/BackEnd/Timeline/Services/UserService.cs @@ -7,7 +7,6 @@ using System.Linq; using System.Threading.Tasks; using Timeline.Entities; using Timeline.Helpers; -using Timeline.Models; using Timeline.Models.Validation; using Timeline.Services.Exceptions; using static Timeline.Resources.Services.UserService; @@ -32,13 +31,13 @@ namespace Timeline.Services /// The id of the user. /// The user info. /// Thrown when the user with given id does not exist. - Task GetUser(long id); + Task GetUser(long id); /// /// List all users. /// /// The user info of users. - Task> GetUsers(); + Task> GetUsers(); /// /// Create a user with given info. @@ -49,7 +48,7 @@ namespace Timeline.Services /// Thrown when or is null. /// Thrown when or is of bad format. /// Thrown when a user with given username already exists. - Task CreateUser(string username, string password); + Task CreateUser(string username, string password); /// /// Modify a user. @@ -62,7 +61,7 @@ namespace Timeline.Services /// /// Version will increase if password is changed. /// - Task ModifyUser(long id, ModifyUserParams? param); + Task ModifyUser(long id, ModifyUserParams? param); } public class UserService : BasicUserService, IUserService @@ -73,17 +72,15 @@ namespace Timeline.Services private readonly DatabaseContext _databaseContext; private readonly IPasswordService _passwordService; - private readonly IUserPermissionService _userPermissionService; private readonly UsernameValidator _usernameValidator = new UsernameValidator(); private readonly NicknameValidator _nicknameValidator = new NicknameValidator(); - public UserService(ILogger logger, DatabaseContext databaseContext, IPasswordService passwordService, IUserPermissionService userPermissionService, IClock clock) : base(databaseContext) + public UserService(ILogger logger, DatabaseContext databaseContext, IPasswordService passwordService, IClock clock) : base(databaseContext) { _logger = logger; _databaseContext = databaseContext; _passwordService = passwordService; - _userPermissionService = userPermissionService; _clock = clock; } @@ -116,43 +113,22 @@ namespace Timeline.Services throw new EntityAlreadyExistException(EntityNames.User, ExceptionUsernameConflict); } - private async Task CreateUserFromEntity(UserEntity entity) + public async Task GetUser(long id) { - var permission = await _userPermissionService.GetPermissionsOfUserAsync(entity.Id); - return new UserInfo( - entity.Id, - entity.UniqueId, - entity.Username, - string.IsNullOrEmpty(entity.Nickname) ? entity.Username : entity.Nickname, - permission, - entity.UsernameChangeTime, - entity.CreateTime, - entity.LastModified, - entity.Version - ); - } - - public async Task GetUser(long id) - { - var user = await _databaseContext.Users.Where(u => u.Id == id).SingleOrDefaultAsync(); + var user = await _databaseContext.Users.Where(u => u.Id == id).Include(u => u.Permissions).SingleOrDefaultAsync(); if (user == null) throw new UserNotExistException(id); - return await CreateUserFromEntity(user); + return user; } - public async Task> GetUsers() + public async Task> GetUsers() { - List result = new(); - foreach (var entity in await _databaseContext.Users.ToArrayAsync()) - { - result.Add(await CreateUserFromEntity(entity)); - } - return result; + return await _databaseContext.Users.Include(u => u.Permissions).ToListAsync(); } - public async Task CreateUser(string username, string password) + public async Task CreateUser(string username, string password) { if (username == null) throw new ArgumentNullException(nameof(username)); @@ -177,10 +153,12 @@ namespace Timeline.Services _logger.LogInformation(Log.Format(LogDatabaseCreate, ("Id", newEntity.Id), ("Username", username))); - return await CreateUserFromEntity(newEntity); + await _databaseContext.Entry(newEntity).Collection(e => e.Permissions).LoadAsync(); + + return newEntity; } - public async Task ModifyUser(long id, ModifyUserParams? param) + public async Task ModifyUser(long id, ModifyUserParams? param) { if (param != null) { @@ -194,7 +172,7 @@ namespace Timeline.Services CheckNicknameFormat(param.Nickname, nameof(param)); } - var entity = await _databaseContext.Users.Where(u => u.Id == id).SingleOrDefaultAsync(); + var entity = await _databaseContext.Users.Where(u => u.Id == id).Include(u => u.Permissions).SingleOrDefaultAsync(); if (entity == null) throw new UserNotExistException(id); @@ -238,7 +216,7 @@ namespace Timeline.Services _logger.LogInformation(LogDatabaseUpdate, ("Id", id)); } - return await CreateUserFromEntity(entity); + return entity; } } } -- cgit v1.2.3