public async Task <FindResultResponse <ShortWorkspaceInfo> > ExecuteAsync(FindWorkspaceFilter filter) { FindResultResponse <ShortWorkspaceInfo> response = new(); if (!_baseFindValidator.ValidateCustom(filter, out List <string> errors)) { return(_responseCreator.CreateFailureFindResponse <ShortWorkspaceInfo>( HttpStatusCode.BadRequest, errors)); } (List <DbWorkspace> dbWorkspases, int totalCount) = await _repository.FindAsync(filter); response.Body = dbWorkspases .Select(_mapper.Map) .ToList(); response.TotalCount = totalCount; response.Status = OperationResultStatusType.FullSuccess; if (response.Errors.Any()) { response = _responseCreator.CreateFailureFindResponse <ShortWorkspaceInfo>( HttpStatusCode.BadRequest); } return(response); }
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 <FindResultResponse <RoleInfo> > ExecuteAsync(FindRolesFilter filter) { if (!_findFilterValidator.ValidateCustom(filter, out List <string> errors)) { _httpContextAccessor.HttpContext.Response.StatusCode = (int)HttpStatusCode.BadRequest; return(new FindResultResponse <RoleInfo> { Status = OperationResultStatusType.Failed, Errors = errors }); } FindResultResponse <RoleInfo> result = new(); (List <(DbRole role, List <DbRightsLocalization> rights)> roles, int totalCount) = filter.IncludeDeactivated ? await _roleRepository.FindAllAsync(filter) : await _roleRepository.FindActiveAsync(filter); result.TotalCount = totalCount; List <Guid> usersIds = new(); foreach ((DbRole role, List <DbRightsLocalization> rights) in roles) { usersIds.Add(role.CreatedBy); if (role.ModifiedBy.HasValue) { usersIds.Add(role.ModifiedBy.Value); } } List <UserInfo> usersInfos = (await GetUsersAsync(usersIds.Distinct().ToList(), errors))? .Select(_userInfoMapper.Map) .ToList(); result.Body = roles.Select( pair => _roleInfoMapper.Map(pair.role, pair.rights.Select(_rightMapper.Map).ToList(), usersInfos)).ToList(); result.Errors = errors; result.Status = errors.Any() ? OperationResultStatusType.PartialSuccess : OperationResultStatusType.FullSuccess; return(result); }
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); }