예제 #1
0
        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());
            }
        }
예제 #2
0
        public async Task <IActionResult> LogoutAsync()
        {
            SetUserOnlineDto onlineStatus = new SetUserOnlineDto
            {
                IsOnline = false,
                UserId   = User.GetUserId()
            };

            await _userService.SetUserOnlineStatusAsync(onlineStatus);

            return(NoContent());
        }
예제 #3
0
        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));
        }