public async Task <ChannelPreviewModel> Handle(GetUserChannelQuery request, CancellationToken cancellationToken) { var currentUserId = _currentUserService.GetUserIdOrThrow(); if (!await UserChannelExists(request.UserId, currentUserId, cancellationToken)) { var createChannelResult = await _mediator.Send(new CreateUserChannelCommand { UserId = request.UserId }, cancellationToken); createChannelResult.ThrowIfFailed(); } var channel = await UserChannelQuery(request.UserId, currentUserId) .Select(c => new ChannelPreviewModel { Id = c.Id, InterlocutorUserId = c.Interlocutors.Single(i => i.UserId != currentUserId).UserId, Name = c.Interlocutors.Single(i => i.UserId != currentUserId).User.Name.FullName, Gender = c.Interlocutors.Single(i => i.UserId != currentUserId).User.Gender.ToString(), LastMessage = c.Messages.OrderBy(m => m.CreatedDate).Last().Content, LastMessageDate = c.Messages.OrderBy(m => m.CreatedDate).Last().CreatedDate, Seen = c.Messages.OrderBy(m => m.CreatedDate).Last().Seen != null, CreatedDate = c.CreatedDate }) .AsNoTracking() .SingleAsync(cancellationToken); channel.IsOnline = _connectedUsersService.ConnectedUsers .Any(x => x.UserId == channel.InterlocutorUserId); return(channel); }
public async Task <IList <UserPreviewModel> > Handle(SearchUsersQuery request, CancellationToken cancellationToken) { var currentUserId = _currentUserService.GetUserIdOrThrow(); var usersQuery = _dbContext.Users .Where(x => x.Name.FirstName.Contains(request.SearchStr) || x.Name.LastName.Contains(request.SearchStr)) .Where(x => x.Id != currentUserId); if (request.ExcludeFriends) { usersQuery = usersQuery .Where(x => x.ReceivedFriendshipsRequests.All(f => f.RequestedById != currentUserId)) .Where(x => x.SentFriendshipRequests.All(f => f.RequestedToId != currentUserId)); } var users = await usersQuery .Take(20) .Select(x => new UserPreviewModel { Id = x.Id, Name = x.Name.FullName, Gender = x.Gender.ToString(), IsFriend = false // TODO }) .AsNoTracking() // https://stackoverflow.com/questions/41577376/how-to-read-values-from-the-querystring-with-asp-net-core .ToListAsync(cancellationToken); return(users); }
public async Task <UserModel> Handle(GetUserQuery request, CancellationToken cancellationToken) { var currentUserId = _currentUserService.GetUserIdOrThrow(); var user = await _dbContext.Users .Where(x => x.Id == request.Id) .Select(x => new UserModel { Id = x.Id, Firstname = x.Name.FirstName, Lastname = x.Name.LastName, FullName = x.Name.FullName, Gender = x.Gender.ToString(), About = x.About, Friends = x.ReceivedFriendshipsRequests.Count() + x.SentFriendshipRequests.Count(), IsFriend = x.ReceivedFriendshipsRequests.Any(f => f.RequestedById == currentUserId) || x.SentFriendshipRequests.Any(f => f.RequestedToId == currentUserId), LastActive = x.LastActive }) .AsNoTracking() .SingleAsync(cancellationToken); user.IsOnline = _connectedUsersService.ConnectedUsers .Any(x => x.UserId == user.Id); return(user); }
public async Task <IList <ChannelPreviewModel> > Handle(GetChannelsQuery request, CancellationToken cancellationToken) { var currentUserId = _currentUserService.GetUserIdOrThrow(); var channels = await _dbContext.Channels .Where(c => c.Interlocutors.Any(i => i.UserId == currentUserId)) .Select(c => new ChannelPreviewModel { Id = c.Id, InterlocutorUserId = c.Interlocutors.Single(i => i.UserId != currentUserId).UserId, Name = c.Interlocutors.Single(i => i.UserId != currentUserId).User.Name.FullName, Gender = c.Interlocutors.Single(i => i.UserId != currentUserId).User.Gender.ToString(), LastMessage = c.Messages.OrderBy(m => m.CreatedDate).Last().Content, LastMessageDate = c.Messages.OrderBy(m => m.CreatedDate).Last().CreatedDate, Seen = c.Messages.OrderBy(m => m.CreatedDate).Last().Seen != null, Own = c.Messages.OrderBy(m => m.CreatedDate).Last().SenderId == currentUserId, CreatedDate = c.CreatedDate }) .OrderByDescending(c => c.LastMessageDate) .ThenByDescending(c => c.CreatedDate) .AsNoTracking() .ToListAsync(cancellationToken); foreach (var channel in channels) { channel.IsOnline = _connectedUsersService.ConnectedUsers .Any(x => x.UserId == channel.InterlocutorUserId); } return(channels); }
public async Task <AccountProfileModel> Handle(GetAccountProfileQuery request, CancellationToken cancellationToken) { var currentUserId = _currentUserService.GetUserIdOrThrow(); var user = await _dbContext.Users.FindAsync(currentUserId); return(new AccountProfileModel { Firstname = user.Name.FirstName, Lastname = user.Name.LastName, Email = user.Email, Gender = user.Gender?.ToString(), About = user.About, CreatedDate = user.CreatedDate }); }
public async Task <ChannelModel> Handle(GetChannelQuery request, CancellationToken cancellationToken) { var currentUserId = _currentUserService.GetUserIdOrThrow(); if (!await HasAccess(request.Id, currentUserId, cancellationToken)) { throw new PowerChatApplicationException(ChannelErrorCodes.NoAccess, "You don't have access to this channel.'"); } var channel = await _dbContext.Channels .Where(c => c.Id == request.Id) .Select(x => new ChannelModel { Id = x.Id, Name = x.Interlocutors.Single(i => i.UserId != currentUserId).User.Name.FullName, Interlocutor = x.Interlocutors.Select(i => new InterlocutorModel { Id = i.User.Id, Name = i.User.Name.FullName, Gender = i.User.Gender.ToString() }).Single(i => i.Id != currentUserId), Messages = x.Messages .OrderBy(m => m.CreatedDate) .Select(m => new MessageModel { Id = m.Id, AuthorId = m.SenderId, Content = m.Content, SentDate = m.CreatedDate, Seen = m.Seen, Own = m.SenderId == currentUserId }), LastActive = x.Interlocutors.Single(i => i.UserId != currentUserId).User.LastActive }) .AsNoTracking() .SingleAsync(cancellationToken); channel.IsOnline = _connectedUsersService.ConnectedUsers .Any(x => x.UserId == channel.Interlocutor.Id); return(channel); }
public async Task <IList <FriendModel> > Handle(GetFriendsQuery request, CancellationToken cancellationToken) { var currentUserId = _currentUserService.GetUserIdOrThrow(); var requestedBy = await _dbContext.Friendships .Where(x => x.RequestedById == currentUserId) .Select(x => new FriendModel { Id = x.RequestedTo.Id, Name = x.RequestedTo.Name.FullName, Gender = x.RequestedTo.Gender.ToString(), Approved = x.Approved }) .AsNoTracking() .ToListAsync(cancellationToken); var requestedTo = await _dbContext.Friendships .Where(x => x.RequestedToId == currentUserId) .Select(x => new FriendModel { Id = x.RequestedBy.Id, Name = x.RequestedBy.Name.FullName, Gender = x.RequestedBy.Gender.ToString(), Approved = x.Approved }) .AsNoTracking() .ToListAsync(cancellationToken); var friends = requestedBy.Union(requestedTo).ToList(); foreach (var friend in friends) { friend.IsOnline = _connectedUsersService.ConnectedUsers .Any(x => x.UserId == friend.Id); } return(friends); }