From b892622e7ffdf4220f6631ec58f7a6692881dd35 Mon Sep 17 00:00:00 2001 From: crupest Date: Fri, 31 Jan 2020 21:57:09 +0800 Subject: Fix test bugs in user info mapper. Make create user action return created user info. --- Timeline/Controllers/UserController.cs | 6 +++--- Timeline/Models/Http/UserInfo.cs | 14 +++----------- Timeline/Services/UserService.cs | 8 ++++---- 3 files changed, 10 insertions(+), 18 deletions(-) (limited to 'Timeline') diff --git a/Timeline/Controllers/UserController.cs b/Timeline/Controllers/UserController.cs index 4572296b..26e63f63 100644 --- a/Timeline/Controllers/UserController.cs +++ b/Timeline/Controllers/UserController.cs @@ -108,12 +108,12 @@ namespace Timeline.Controllers } [HttpPost("userop/createuser"), AdminAuthorize] - public async Task CreateUser([FromBody] CreateUserRequest body) + public async Task> CreateUser([FromBody] CreateUserRequest body) { try { - await _userService.CreateUser(_mapper.Map(body)); - return Ok(); + var user = await _userService.CreateUser(_mapper.Map(body)); + return Ok(ConvertToUserInfo(user)); } catch (ConflictException) { diff --git a/Timeline/Models/Http/UserInfo.cs b/Timeline/Models/Http/UserInfo.cs index fee53ade..0d1d702b 100644 --- a/Timeline/Models/Http/UserInfo.cs +++ b/Timeline/Models/Http/UserInfo.cs @@ -25,14 +25,8 @@ namespace Timeline.Models.Http public class UserInfoLinksValueResolver : IValueResolver { - private readonly IActionContextAccessor? _actionContextAccessor; - private readonly IUrlHelperFactory? _urlHelperFactory; - - public UserInfoLinksValueResolver() - { - _actionContextAccessor = null; - _urlHelperFactory = null; - } + private readonly IActionContextAccessor _actionContextAccessor; + private readonly IUrlHelperFactory _urlHelperFactory; public UserInfoLinksValueResolver(IActionContextAccessor actionContextAccessor, IUrlHelperFactory urlHelperFactory) { @@ -42,10 +36,8 @@ namespace Timeline.Models.Http public UserInfoLinks? Resolve(User source, UserInfo destination, UserInfoLinks? destMember, ResolutionContext context) { - if (_actionContextAccessor == null || _urlHelperFactory == null) - { + if (_actionContextAccessor.ActionContext == null) return null; - } var urlHelper = _urlHelperFactory.GetUrlHelper(_actionContextAccessor.ActionContext); var result = new UserInfoLinks diff --git a/Timeline/Services/UserService.cs b/Timeline/Services/UserService.cs index d2dc969e..93d92740 100644 --- a/Timeline/Services/UserService.cs +++ b/Timeline/Services/UserService.cs @@ -64,7 +64,7 @@ namespace Timeline.Services /// /// The info of new user. /// The password, can't be null or empty. - /// The id of the new user. + /// The the new user. /// Thrown when is null. /// Thrown when some fields in is bad. /// Thrown when a user with given username already exists. @@ -75,7 +75,7 @@ namespace Timeline.Services /// must be a valid nickname if set. It is empty by default. /// Other fields are ignored. /// - Task CreateUser(User info); + Task CreateUser(User info); /// /// Modify a user's info. @@ -276,7 +276,7 @@ namespace Timeline.Services return entities.Select(user => CreateUserFromEntity(user)).ToArray(); } - public async Task CreateUser(User info) + public async Task CreateUser(User info) { if (info == null) throw new ArgumentNullException(nameof(info)); @@ -316,7 +316,7 @@ namespace Timeline.Services _logger.LogInformation(Log.Format(LogDatabaseCreate, ("Id", newEntity.Id), ("Username", username), ("Administrator", administrator))); - return newEntity.Id; + return CreateUserFromEntity(newEntity); } private void ValidateModifyUserInfo(User? info) -- cgit v1.2.3