//[Route("GetAllCategory")]

        public async Task <IActionResult> GetAll([FromBody] SearchDepartmentViewModel viewModel)
        {
            if (viewModel.PageIndex == -1 || viewModel.PageSize == -1)
            {
                return(this.ApiResponse <string>(null, $"{viewModel.PageIndex} or {viewModel.PageSize} can not be -1", ApiResponseCodes.INVALID_REQUEST));
            }

            var result = await _departmentService.GetAllDepartment(viewModel);

            if ((result.Code != ApiResponseCodes.OK))
            {
                return(base.ApiResponse <string>(null, result.Description,
                                                 ApiResponseCodes.EXCEPTION, 1));
            }
            if (result.Payload == null)
            {
                return(this.ApiResponse <List <DepartmentListViewModel> >(result.Payload, "Record not Found.", ApiResponseCodes.NOT_FOUND));
            }
            return(this.ApiResponse <List <DepartmentListViewModel> >(result.Payload, "successful.", ApiResponseCodes.OK));
        }
示例#2
0
        public async Task <ApiResponse <List <DepartmentListViewModel> > > GetAllDepartment(SearchDepartmentViewModel viewModel)
        {
            ApiResponse <List <DepartmentListViewModel> > response = new ApiResponse <List <DepartmentListViewModel> >();

            try
            {
                var count = UnitOfWork.Repository <Department>().Query().Count(x => !x.IsDeleted);
                Expression <Func <Department, bool> > queryPredicate = (x) => x.Name.Contains(viewModel.FilterBy) ||
                                                                       x.CreatedOn >= viewModel.ToDate || x.CreatedOn <= viewModel.FromDate;

                var result = (await this.GetAllAsync(viewModel.PageIndex, viewModel.PageSize, c => c.Id, queryPredicate, OrderBy.Ascending))
                             .OrderBy(b => b.Id).Select(source => new DepartmentListViewModel
                {
                    Name        = source.Name,
                    Description = source.Description,
                    TotalCount  = count,
                    Id          = source.Id.ToString(),
                }).ToList();
                response.Payload     = result;
                response.Code        = ApiResponseCodes.OK;
                response.Description = ApiResponseCodes.OK.ToString();
                return(response);
            }
            catch (Exception ex)
            {
                response.Payload     = null;
                response.Code        = ApiResponseCodes.ERROR;
                response.Description = ex.Message;
                return(response);
            }
        }