From 52acf41e331ddbd66befed4692c804b754ba7d5c Mon Sep 17 00:00:00 2001 From: crupest Date: Thu, 30 Jan 2020 20:26:52 +0800 Subject: ... --- Timeline/Services/UserService.cs | 36 ++++++++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-) (limited to 'Timeline/Services/UserService.cs') diff --git a/Timeline/Services/UserService.cs b/Timeline/Services/UserService.cs index ff2306c5..1197bb73 100644 --- a/Timeline/Services/UserService.cs +++ b/Timeline/Services/UserService.cs @@ -43,6 +43,16 @@ namespace Timeline.Services /// Thrown when the user with given username does not exist. Task GetUserByUsername(string username); + /// + /// Get the user id of given username. + /// + /// Username of the 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. + Task GetUserIdByUsername(string username); + /// /// List all users. /// @@ -57,7 +67,7 @@ namespace Timeline.Services /// The id of the new user. /// Thrown when is null. /// Thrown when some fields in is bad. - /// Thrown when a user with given username already exists. + /// Thrown when a user with given username already exists. /// /// must not be null and must be a valid username. /// must not be null or empty. @@ -78,13 +88,12 @@ namespace Timeline.Services /// Only , , and will be used. /// If null, then not change. /// Other fields are ignored. - /// After modified, even if nothing is changed, version will increase. + /// Version will increase if password is changed. /// /// must be a valid username if set. /// can't be empty if set. /// must be a valid nickname if set. /// - /// Note: Whether is set or not, version will increase and not set to the specified value if there is one. /// /// Task ModifyUser(long id, User? info); @@ -97,6 +106,7 @@ namespace Timeline.Services /// Thrown when is null. /// Thrown when is of bad format or some fields in is bad. /// Thrown when user with given id does not exist. + /// Thrown when user with the newusername already exist. /// /// Only , and will be used. /// If null, then not change. @@ -184,7 +194,7 @@ namespace Timeline.Services private static void ThrowUsernameConflict() { - throw new ConfictException(ExceptionUsernameConflict); + throw new ConflictException(ExceptionUsernameConflict); } private static User CreateUserFromEntity(UserEntity entity) @@ -245,6 +255,21 @@ namespace Timeline.Services return CreateUserFromEntity(entity); } + public async Task GetUserIdByUsername(string username) + { + if (username == null) + throw new ArgumentNullException(nameof(username)); + + CheckUsernameFormat(username, nameof(username)); + + var entity = await _databaseContext.Users.Where(user => user.Username == username).Select(u => new { u.Id }).SingleOrDefaultAsync(); + + if (entity == null) + throw new UserNotExistException(username); + + return entity.Id; + } + public async Task GetUsers() { var entities = await _databaseContext.Users.ToArrayAsync(); @@ -325,6 +350,7 @@ namespace Timeline.Services if (password != null) { entity.Password = _passwordService.HashPassword(password); + entity.Version += 1; } var administrator = info.Administrator; @@ -339,8 +365,6 @@ namespace Timeline.Services entity.Nickname = nickname; } } - - entity.Version += 1; } -- cgit v1.2.3