public async Task <IActionResult> LoginAsync([FromBody] UserLoginDto loginDto) { try { var result = await _authorizationService.LoginUserAsync(loginDto); if (result == null) { return(Unauthorized()); } SetUserOnlineDto onlineStatus = new SetUserOnlineDto { IsOnline = true, UserId = result.User.Id }; await _userService.SetUserOnlineStatusAsync(onlineStatus); return(Ok(result)); } catch (UserNotFoundException) { return(NotFound()); } catch (UserLoginFailedException) { return(Unauthorized()); } }
public async Task <IActionResult> LogoutAsync() { SetUserOnlineDto onlineStatus = new SetUserOnlineDto { IsOnline = false, UserId = User.GetUserId() }; await _userService.SetUserOnlineStatusAsync(onlineStatus); return(NoContent()); }
public async Task <GetUserDto> SetUserOnlineStatusAsync(SetUserOnlineDto userOnlineDto, CancellationToken ct = default) { _logger.LogInformation( "Set online status to {OnlineStatus} for user {UserId}", userOnlineDto.IsOnline, userOnlineDto.UserId); User user = await _unitOfWork.UserRepository.GetAsync(userOnlineDto.UserId, ct); user.IsOnline = userOnlineDto.IsOnline; if (!user.IsOnline) { user.LastSeenAt = DateTime.UtcNow; } _unitOfWork.UserRepository.Update(user); await _unitOfWork.CommitAsync(ct); return(_mapper.Map <GetUserDto>(user)); }