예제 #1
0
        public async Task <ActionResult> GetByIdAsync(
            int id,
            [FromQuery] bool includeUsers = false)
        {
            try
            {
                var chatroom = await _chatroomManagerService.GetChatroomAsync(id);

                var chatroomUsers = includeUsers
                    ? (await _chatroomManagerService.GetChatroomUsersAsync(id)).Select(user => new BasicUserResponse
                {
                    Id       = user.Id,
                    Username = user.Username,
                }).ToList()
                    : Enumerable.Empty <BasicUserResponse>().ToList();
                return(new JsonResult(new ChatroomResponse
                {
                    Id = chatroom.Id,
                    Name = chatroom.Name,
                    IsShared = chatroom.IsShared,
                    Users = chatroomUsers,
                }));
            }
            catch (InvalidOperationException)
            {
                return(NotFound());
            }
            catch (Exception ex)
            {
                _logger.LogWarning(ex, $"Could not retrieve chatroom with id ({id}).");
                return(BadRequest());
            }
        }
예제 #2
0
        public async Task <IActionResult> GetChatroomsAsync(
            int id,
            [FromServices] IChatroomsManager chatroomsManager)
        {
            try
            {
                var user = await _userManager.GetUserAsync(id);

                if (user is null)
                {
                    return(NotFound());
                }

                var chatrooms = await chatroomsManager.GetUserChatroomsAsync(user);

                var responseChatrooms = new List <ChatroomResponse>();
                foreach (var chatroom in chatrooms)
                {
                    var chatroomUsers = (await chatroomsManager.GetChatroomUsersAsync(chatroom.Id))
                                        .Select(user => new BasicUserResponse {
                        Id = user.Id, Username = user.Username
                    })
                                        .ToList();
                    responseChatrooms.Add(new ChatroomResponse
                    {
                        Id    = chatroom.Id,
                        Name  = chatroom.Name,
                        Users = chatroomUsers,
                    });
                }

                return(new JsonResult(new UserChatroomsResponse {
                    Chatrooms = responseChatrooms
                }));
            }
            catch (Exception ex)
            {
                _logger.LogWarning(ex, $"Could not retrieve the chatrooms of user with id ({id}).");
                return(BadRequest());
            }
        }