Ejemplo n.º 1
0
        public async Task <ApiResult <GetAllBuildingOutput> > GetAll([FromUri] GetAllBuildingInput input, CancellationToken cancelToken)
        {
            if (Authorization == null)
            {
                return(new ApiResult <GetAllBuildingOutput>(APIResultCode.Unknown, new GetAllBuildingOutput {
                }, APIResultMessage.TokenNull));
            }
            var user = _tokenManager.GetUser(Authorization);

            if (user == null)
            {
                return(new ApiResult <GetAllBuildingOutput>(APIResultCode.Unknown, new GetAllBuildingOutput {
                }, APIResultMessage.TokenError));
            }
            if (input.PageIndex < 1)
            {
                input.PageIndex = 1;
            }
            if (input.PageSize < 1)
            {
                input.PageSize = 10;
            }
            int startRow = (input.PageIndex - 1) * input.PageSize;

            var data = await _buildingService.GetAllIncludeAsync(new BuildingDto
            {
                Name            = input?.Name,
                SmallDistrictId = input.SmallDistrictId
            }, cancelToken);

            var listCount = data.Count();
            var list      = data.Skip(startRow).Take(input.PageSize);

            return(new ApiResult <GetAllBuildingOutput>(APIResultCode.Success, new GetAllBuildingOutput
            {
                List = list.Select(x => new GetBuildingOutput
                {
                    Id = x.Id.ToString(),
                    Name = x.Name,
                    SmallDistrictId = x.SmallDistrictId.ToString(),
                    SmallDistrictName = x.SmallDistrict.Name,
                }).ToList(),
                TotalCount = listCount
            }));
        }