Beispiel #1
0
        public async Task <OperationResultResponse <ChannelInfo> > ExeсuteAsync(GetChannelFilter filter)
        {
            if (!_baseFindValidator.ValidateCustom(filter, out List <string> errors))
            {
                return(_responseCreator.CreateFailureResponse <ChannelInfo>(HttpStatusCode.BadRequest, errors));
            }

            DbChannel dbChannel = await _repository.GetAsync(filter);

            Guid requestUserId = _httpContextAccessor.HttpContext.GetUserId();

            if (dbChannel is null)
            {
                return(_responseCreator.CreateFailureResponse <ChannelInfo>(HttpStatusCode.NotFound));
            }

            if ((dbChannel.IsPrivate &&
                 !dbChannel.Users.Select(cu => cu.UserId).Contains(requestUserId)) ||
                !dbChannel.Workspace.Users.Select(wu => wu.UserId).Contains(requestUserId))
            {
                return(_responseCreator.CreateFailureResponse <ChannelInfo>(HttpStatusCode.Forbidden));
            }

            OperationResultResponse <ChannelInfo> response = new();

            List <Guid> usersIds = dbChannel.Users.Select(cu => cu.UserId).ToList();

            usersIds.AddRange(dbChannel.Messages.Select(m => m.CreatedBy).Distinct().ToList());

            List <UserData> usersData = await GetUsersAsync(usersIds, response.Errors);

            List <Guid> imagesIds = usersData?.Where(u => u.ImageId.HasValue)?.Select(u => u.ImageId.Value).ToList();

            //add messages immages to image service

            List <ImageInfo> imagesInfo = (await GetImagesAsync(imagesIds, response.Errors))
                                          ?.Select(_imageMapper.Map).ToList();

            List <UserInfo> usersInfo = usersData
                                        ?.Select(u =>
                                                 _userMapper.Map(u, imagesInfo?.FirstOrDefault(i => i.Id == u.ImageId))).ToList();

            response.Body = _channelMapper.Map(
                dbChannel,
                dbChannel.Messages.Select(
                    m => _messageMapper.Map(
                        m,
                        usersInfo?.FirstOrDefault(u => u.Id == m.CreatedBy),
                        imagesInfo?.Where(i => m.Images.Select(mi => mi.ImageId).Contains(i.Id)).ToList())).ToList(),
                usersInfo?.Where(u => dbChannel.Users.Select(u => u.UserId).Contains(u.Id)).ToList());

            response.Status = OperationResultStatusType.FullSuccess;

            return(response);
        }
        public async Task <DbChannel> GetAsync(GetChannelFilter filter)
        {
            IQueryable <DbChannel> dbChannel = _provider.Channels.AsQueryable()
                                               .Include(c => c.Workspace).ThenInclude(w => w.Users.Where(wu => wu.IsActive))
                                               .Include(c => c.Users.Where(cu => cu.IsActive));

            dbChannel = dbChannel
                        .Include(c => c.Messages
                                 .OrderByDescending(m => m.CreatedAtUtc)
                                 .Skip(filter.SkipCount)
                                 .Take(filter.TakeCount))
                        .ThenInclude(m => m.Images)
                        .Include(c => c.Messages)
                        .ThenInclude(m => m.Files);

            return(await dbChannel
                   .FirstOrDefaultAsync(c => c.Id == filter.ChannelId && c.IsActive));
        }
 public async Task <OperationResultResponse <ChannelInfo> > GetAsync(
     [FromServices] IGetChannelCommand command,
     [FromQuery] GetChannelFilter filter)
 {
     return(await command.ExeсuteAsync(filter));
 }