public async Task <ActionResult <UserLarge> > DeleteUser(string username) { _logger.LogInformation(nameof(EditUserInfo) + ": " + username); var uname = User.FindFirst(ClaimTypes.Name)?.Value; var uid = User.FindFirst(ClaimTypes.NameIdentifier)?.Value; if (uname != username && uid != username) { return(Unauthorized()); } var user = await _repo.GetUser(uid); if (user == null) { return(NotFound()); } var postsCount = await _repo.GetUserPostsCount(user.Username, DateTime.UtcNow); if (postsCount != 0) { return(BadRequest(new { error = $"User has {postsCount} posts." })); } var collectionCount = await _repo.GetUserCollectionCount(user.Username, true, DateTime.UtcNow); if (collectionCount != 0) { return(BadRequest(new { error = $"User has {collectionCount} collection." })); } return(UserLarge.FromUser(await _repo.DeleteUser(user.Username), _imageServer)); }
public async Task <ActionResult <UserLarge> > GetUserInfo(string username) { _logger.LogInformation(nameof(GetUserInfo) + ": " + username); var user = await _repo.GetUser(username); if (user == null) { return(NotFound()); } return(UserLarge.FromUser(user, _imageServer)); }
public async Task <ActionResult <UserLarge> > EditUserInfo([FromBody] UserLarge newUserInfo) { _logger.LogInformation(nameof(EditUserInfo) + ": " + newUserInfo); var username = User.FindFirst(ClaimTypes.Name)?.Value; var user = await _repo.UpdateUser(username, newUserInfo); if (user == null) { return(BadRequest(new { error = "Update user info failed." })); } return(UserLarge.FromUser(user, _imageServer)); }
public async Task <User> UpdateUser(string username, UserLarge user) { var item = await GetUser(username); if (item == null) { return(null); } item.Nickname = user.Nickname; item.Phone = user.Phone; item.BirthDate = user.BirthDate; item.Sex = user.Sex; item.Address = user.Address; item.ImageUrl = user.ImageUrl.RemoveServerAddress(_imageServer); await _context.SaveChangesAsync(); return(item); }
public async Task <ActionResult> SignUp([FromBody] UserSmall user) { _logger.LogInformation(nameof(SignUp) + ": " + user); // 检查是否有相同用户名 if (await _repo.UserExists(user.Username)) { return(BadRequest(new { error = "Username already exists." })); } var registeredUser = await _repo.Register(user); if (registeredUser == null) { return(BadRequest(new { error = "User register fail!" })); } return(new CreatedResult(nameof(SignUp), UserLarge.FromUser(registeredUser, _imageServer))); }