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); }
public async Task <IActionResult> GetAllMachineries(AgriculturalMachineryListingRequestModel model) { if (ModelState.IsValid) { try { return(Ok(this.agriculturalMachineryService.GetAll(model))); } catch (Exception e) { return(BadRequest(e.Message)); } } else { var msg = ModelState.GetErrors(); var error = StatusCode(500, msg); return(error); } }