public async Task <GridResponseModel> SearchLogs(GridSearchModel searchModel)
        {
            GridResponseModel result = new GridResponseModel();

            IQueryable <UserLogs> userLogs = _context.UserLogs.OrderByDescending(x => x.Id).AsQueryable();

            string searchToLower = searchModel.Search.ToLower();

            if (searchModel.StartDate.HasValue && searchModel.EndDate.HasValue)
            {
                if (searchModel.StartDate > searchModel.EndDate)
                {
                    DateTime?temp = searchModel.StartDate;
                    searchModel.StartDate = searchModel.EndDate;
                    searchModel.EndDate   = temp;
                }

                userLogs = userLogs.Where(
                    ul => ul.ServerTimeUtc.Date >= searchModel.StartDate &&
                    ul.ServerTimeUtc.Date <= searchModel.EndDate);
            }
            if (!String.IsNullOrEmpty(searchToLower))
            {
                userLogs = userLogs.Where(ul => ul.ActionName.ToLower().Contains(searchToLower) ||
                                          ul.Controller.ToLower().Contains(searchToLower) ||
                                          ul.Data.ToLower().Contains(searchToLower) ||
                                          ul.Id.ToString().ToLower().Contains(searchToLower) ||
                                          ul.Message.ToLower().Contains(searchToLower) ||
                                          ul.RequestMethod.ToLower().Contains(searchToLower) ||
                                          ul.Result.ToLower().Contains(searchToLower) ||
                                          ul.UserId.ToLower().Contains(searchToLower) ||
                                          ul.Ip.ToLower().Contains(searchToLower) ||
                                          ul.Duration.ToString().ToLower().Contains(searchToLower) ||
                                          ul.ResponseStatusCode.ToString().ToLower().Contains(searchToLower));
            }

            result.Total = userLogs.Count();

            if (!String.IsNullOrEmpty(searchModel.SortBy))
            {
                userLogs = OrderByStringWithReflection.OrderBy(userLogs, searchModel.SortBy, searchModel.SortDesc);
            }

            userLogs = userLogs.Skip((searchModel.Page - 1) * searchModel.ItemsPerPage)
                       .Take(searchModel.ItemsPerPage);

            result.Items    = userLogs;
            result.Page     = searchModel.Page;
            result.PageSize = searchModel.ItemsPerPage;

            return(result);
        }
Beispiel #2
0
        public AgriculturalMachineryResponseModel GetAll(AgriculturalMachineryListingRequestModel model)
        {
            AgriculturalMachineryResponseModel result = new AgriculturalMachineryResponseModel();

            var agMachineries = _context.AgriculturalMachinery
                                .Include(x => x.Company)
                                .Include(x => x.Owner)
                                .Where(x => x.Deleted == false)
                                .Select(am => new AgriculturalMachineryWithOwnerNameModel
            {
                Id = am.Id,
                RegistrationNumber = am.RegistrationNumber,
                FrameNumber        = am.FrameNumber,
                Type  = am.Type,
                Owner = am.CompanyId != null
                ?
                        am.Company.Name
                :
                        am.Owner.FirstName + " " + am.Owner.LastName
            })
                                .OrderByDescending(x => x.Id)
                                .AsQueryable();

            var searchString = model.SearchString == null || model.SearchString == "" ? model.SearchString : model.SearchString.ToLower();

            if (!String.IsNullOrEmpty(searchString))
            {
                agMachineries = agMachineries
                                .Where(m =>
                                       m.RegistrationNumber.ToLower().Contains(searchString) ||
                                       m.FrameNumber.ToLower().Contains(searchString) ||
                                       m.Type.ToLower().Contains(searchString) ||
                                       m.Owner.ToLower().Contains(searchString));
            }

            result.Total = agMachineries.Count();

            if (!String.IsNullOrEmpty(model.SortBy))
            {
                agMachineries = OrderByStringWithReflection.OrderBy(agMachineries, model.SortBy, model.SortDesc);
            }

            agMachineries = agMachineries.Skip((model.Page - 1) * model.ItemsPerPage)
                            .Take(model.ItemsPerPage);

            result.Items = agMachineries;

            return(result);
        }