Exemplo n.º 1
0
        public async Task <OperationResultResponse <WorkspaceInfo> > ExecuteAsync(GetWorkspaceFilter filter)
        {
            DbWorkspace dbWorkspace = await _repository.GetAsync(filter);

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

            if (!dbWorkspace.Users.Select(u => u.UserId).Contains(_httpContextAccessor.HttpContext.GetUserId()))
            {
                return(_responseCreator.CreateFailureResponse <WorkspaceInfo>(HttpStatusCode.Forbidden));
            }

            OperationResultResponse <WorkspaceInfo> response = new();

            List <UserData> usersData = await GetUsersAsync(dbWorkspace.Users?.Select(u => u.UserId).ToList(), response.Errors);

            List <ImageInfo> imagesInfo = (await GetImagesAsync(
                                               usersData?.Where(u => u.ImageId.HasValue).Select(u => u.ImageId.Value).ToList(),
                                               response.Errors))
                                          ?.Select(_imageMapper.Map).ToList();

            response.Status = response.Errors.Any() ? OperationResultStatusType.PartialSuccess : OperationResultStatusType.FullSuccess;
            response.Body   = _workspaceInfoMapper.Map(
                dbWorkspace,
                usersData?.Select(u => _userMapper.Map(u, imagesInfo?.FirstOrDefault(i => i.Id == u.ImageId))).ToList());

            return(response);
        }
Exemplo n.º 2
0
        private async Task <UserInfo> GetUserAsync(List <string> errors)
        {
            Guid userId = _httpContextAccessor.HttpContext.GetUserId();

            if (_cache.TryGetValue(userId, out UserInfo userInfo))
            {
                return(userInfo);
            }

            UserData userData = (await GetUsersThroughBrokerAsync(new List <Guid>()
            {
                userId
            }, errors))
                                ?.FirstOrDefault();

            ImageData imageData = userData is not null && userData.ImageId.HasValue
        ? (await GetImagesThroughBrokerAsync(new List <Guid>()
            {
                userData.ImageId.Value
            }, errors))?.FirstOrDefault()
        : null;

            if (userData is not null)
            {
                _cache.Set(
                    userData.Id,
                    _userInfoMapper.Map(userData, _imageInfoMapper.Map(imageData)),
                    TimeSpan.FromMinutes(_cacheOptions.Value.CacheLiveInMinutes));
            }

            return(userInfo);
        }
Exemplo n.º 3
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);
        }
Exemplo n.º 4
0
        public async Task <OperationResultResponse <NewsResponse> > ExecuteAsync(Guid newsId)
        {
            OperationResultResponse <NewsResponse> response = new();

            DbNews dbNews = await _repository.GetAsync(newsId);

            if (dbNews == null)
            {
                _httpContextAccessor.HttpContext.Response.StatusCode = (int)HttpStatusCode.NotFound;

                response.Status = OperationResultStatusType.Failed;
                return(response);
            }

            List <DepartmentData> departmentsData = await GetDepartmentAsync(dbNews.Id, response.Errors);

            List <UserData> usersData =
                await GetUsersDataAsync(
                    new List <Guid>() { dbNews.AuthorId, dbNews.CreatedBy },
                    response.Errors);

            List <ImageData> avatarsImages =
                await GetUsersAvatarsAsync(
                    usersData?.Where(ud => ud.ImageId.HasValue).Select(ud => ud.ImageId.Value).ToList(),
                    response.Errors);

            List <UserInfo> usersInfo =
                usersData?
                .Select(ud => _userInfoMapper.Map(ud, avatarsImages?.FirstOrDefault(ai => ai.ImageId == ud.ImageId))).ToList();

            response.Body = _mapper
                            .Map(
                dbNews,
                _departmentInfoMapper.Map(departmentsData?.FirstOrDefault()),
                usersInfo?.FirstOrDefault(ui => ui.Id == dbNews.AuthorId),
                usersInfo?.FirstOrDefault(ui => ui.Id == dbNews.CreatedBy));

            response.Status = response.Errors.Any()
        ? OperationResultStatusType.PartialSuccess
        : OperationResultStatusType.FullSuccess;

            return(response);
        }
        public async Task <IEnumerable <UserInfoResponse> > GetLearnersAsync()
        {
            var results = await _repo.GetAsync(_learnerTypes);

            return(results.Select(x => _mapper.Map(x)).ToList());
        }
 public void ShouldReturnNullWhenUserDataIsNull()
 {
     Assert.IsNull(_mapper.Map(null));
 }
        public async Task <FindResultResponse <NewsInfo> > ExecuteAsync(FindNewsFilter findNewsFilter)
        {
            FindResultResponse <NewsInfo> response = new();

            if (!_baseFindValidator.ValidateCustom(findNewsFilter, out List <string> errors))
            {
                _httpContextAccessor.HttpContext.Response.StatusCode = (int)HttpStatusCode.BadRequest;

                response.Status = OperationResultStatusType.Failed;
                response.Errors = errors;
                return(response);
            }

            (List <DbNews> dbNewsList, int totalCount) =
                await _repository.FindAsync(findNewsFilter);

            if (dbNewsList == null)
            {
                return(response);
            }

            List <DepartmentData> departmentsData = await GetDepartmentsAsync(
                dbNewsList
                .Select(n => n.Id)
                .Distinct()
                .ToList(),
                response.Errors);

            List <UserData> usersData = await GetUsersDataAsync(
                dbNewsList
                .Select(n => n.AuthorId)
                .Concat(dbNewsList.Select(n => n.CreatedBy))
                .Distinct()
                .ToList(),
                response.Errors);

            List <ImageData> avatarImages = await GetImagesDataAsync(
                usersData?
                .Where(u => u.ImageId.HasValue)
                .Select(u => u.ImageId.Value)
                .ToList(),
                response.Errors);

            List <UserInfo> usersInfo = usersData?
                                        .Select(ud => _userInfoMapper.Map(ud, avatarImages?.FirstOrDefault(id => ud.ImageId == id.ImageId)))
                                        .ToList();

            response.Body = dbNewsList
                            .Select(dbNews => _mapper.Map(
                                        dbNews,
                                        _departmentInfoMapper.Map(departmentsData?.FirstOrDefault(di => di.NewsIds.Contains(dbNews.Id))),
                                        usersInfo?.FirstOrDefault(ud => dbNews.AuthorId == ud.Id),
                                        usersInfo?.FirstOrDefault(ud => dbNews.CreatedBy == ud.Id)))
                            .ToList();

            response.TotalCount = totalCount;

            response.Status = response.Errors.Any()
        ? OperationResultStatusType.PartialSuccess
        : OperationResultStatusType.FullSuccess;

            return(response);
        }