From b6fe2f43a60aab148ffa0e7f5a00c7c12da27854 Mon Sep 17 00:00:00 2001 From: crupest Date: Tue, 11 Aug 2020 00:39:10 +0800 Subject: User service update time fields of user. --- Timeline/Services/UserService.cs | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) (limited to 'Timeline/Services/UserService.cs') diff --git a/Timeline/Services/UserService.cs b/Timeline/Services/UserService.cs index 91d27965..4e56c86a 100644 --- a/Timeline/Services/UserService.cs +++ b/Timeline/Services/UserService.cs @@ -158,6 +158,7 @@ namespace Timeline.Services public class UserService : IUserService { private readonly ILogger _logger; + private readonly IClock _clock; private readonly DatabaseContext _databaseContext; @@ -165,9 +166,10 @@ namespace Timeline.Services private readonly UsernameValidator _usernameValidator = new UsernameValidator(); private readonly NicknameValidator _nicknameValidator = new NicknameValidator(); - public UserService(ILogger logger, DatabaseContext databaseContext, IPasswordService passwordService) + public UserService(ILogger logger, DatabaseContext databaseContext, IPasswordService passwordService, IClock clock) { _logger = logger; + _clock = clock; _databaseContext = databaseContext; _passwordService = passwordService; } @@ -210,7 +212,10 @@ namespace Timeline.Services Administrator = UserRoleConvert.ToBool(entity.Roles), Nickname = string.IsNullOrEmpty(entity.Nickname) ? entity.Username : entity.Nickname, Id = entity.Id, - Version = entity.Version + Version = entity.Version, + CreateTime = entity.CreateTime, + UsernameChangeTime = entity.UsernameChangeTime, + LastModified = entity.LastModified }; } @@ -343,14 +348,19 @@ namespace Timeline.Services { if (info != null) { + DateTimeOffset now = _clock.GetCurrentTime(); + bool updateLastModified = false; + var username = info.Username; - if (username != null) + if (username != null && username != entity.Username) { var conflict = await _databaseContext.Users.AnyAsync(u => u.Username == username); if (conflict) ThrowUsernameConflict(); entity.Username = username; + entity.UsernameChangeTime = now; + updateLastModified = true; } var password = info.Password; @@ -361,15 +371,22 @@ namespace Timeline.Services } var administrator = info.Administrator; - if (administrator.HasValue) + if (administrator.HasValue && UserRoleConvert.ToBool(entity.Roles) != administrator) { entity.Roles = UserRoleConvert.ToString(administrator.Value); + updateLastModified = true; } var nickname = info.Nickname; - if (nickname != null) + if (nickname != null && nickname != entity.Nickname) { entity.Nickname = nickname; + updateLastModified = true; + } + + if (updateLastModified) + { + entity.LastModified = now; } } } -- cgit v1.2.3