public async Task <IActionResult> Search(GridSearchModel searchModel) { if (!ModelState.IsValid) { return(BadRequest("Invalid data.")); } var result = await logService.SearchLogs(searchModel); return(Ok(result)); }
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); }