public HttpResponseMessage Get(DispensaryDataTableParams filter) { IQueryable <Dispensary> query = null; if (User.IsInRole("GlobalAdmin")) { query = from d in HGContext.Dispensaries.Include(d => d.Address) where !d.IsDeleted select d; } else { query = from d in HGContext.Dispensaries.Include(d => d.Address) where !d.IsHidden && !d.IsDeleted select d; } if (!String.IsNullOrEmpty(filter.name)) { query = query.Where(i => i.Name.Contains(filter.name)); } if (!String.IsNullOrEmpty(filter.state)) { query = query.Where(i => i != null && i.Address.State != null && i.Address.State.Contains(filter.state)); } if (filter.iSortCol_0 == 0) { query = filter.sSortDir_0 == "desc" ? query.OrderByDescending(d => d.Name) : query.OrderBy(d => d.Name); } if (filter.iSortCol_0 == 1) { query = filter.sSortDir_0 == "desc" ? query.OrderByDescending(d => d.Address.State) : query.OrderBy(d => d.Address.State); } if (filter.iSortCol_0 == 2) { query = filter.sSortDir_0 == "desc" ? query.OrderByDescending(d => d.Type) : query.OrderBy(d => d.Type); } if (filter.iSortCol_0 == 3) { query = filter.sSortDir_0 == "desc" ? query.OrderByDescending(d => d.EmailAddress) : query.OrderBy(d => d.EmailAddress); } if (filter.iSortCol_0 == 4) { query = filter.sSortDir_0 == "desc" ? query.OrderByDescending(d => d.PhoneNumber) : query.OrderBy(d => d.PhoneNumber); } var dispensaries = query.AsNoTracking().Skip(filter.iDisplayStart).Take(filter.iDisplayLength).Select(d => new DispensaryModel() { Id = d.Id, Address = new AddressModel() { Name = d.Address.Name, Address1 = d.Address.Address1, Address2 = d.Address.Address2, Country = d.Address.Country, City = d.Address.City, FormattedAddress = d.Address.FormattedAddress, Id = d.Address.Id, Latitude = d.Address.Latitude, Longitude = d.Address.Longitude, PhoneNumber = d.Address.PhoneNumber, State = d.Address.State, Zip = d.Address.Zip, }, Description = d.Description, EmailAddress = d.EmailAddress, PhoneNumber = d.PhoneNumber, HasDelivery = d.HasDelivery, HasPickup = d.HasPickup, HasStorefront = d.HasStorefront, HasScheduledDelivery = d.HasScheduledDelivery, HoursAndInfo = d.HoursAndInfo, IsHidden = d.IsHidden, IsPrivate = d.IsPrivate, Name = d.Name, Slug = d.Slug, Type = d.Type, HoursOfOperationString = d.HoursOfOperation, ApprovalZipCodesCollection = d.ApprovalZipCodes.Select(z => z.Code), DeliveryZipCodesCollection = d.DeliveryZipCodes.Select(z => z.Code) }).ToList(); foreach (DispensaryModel dispensary in dispensaries) { if (dispensary.ApprovalZipCodesCollection != null) { dispensary.ApprovalZipCodes = String.Join(", ", dispensary.ApprovalZipCodesCollection); } if (dispensary.DeliveryZipCodesCollection != null) { dispensary.DeliveryZipCodes = String.Join(", ", dispensary.DeliveryZipCodesCollection); } dispensary.HoursOfOperation = Mapper.Map <List <HoursOfOperation> >(dispensary.HoursOfOperationString); } var count = query.AsNoTracking().Count(); GridModel <DispensaryModel> gritItems = new GridModel <DispensaryModel>() { aaData = dispensaries, iTotalDisplayRecords = count, iTotalRecords = count, sEcho = filter.sEcho }; var response = Request.CreateResponse(HttpStatusCode.OK, gritItems); return(response); }
public HttpResponseMessage Get(DispensaryDataTableParams filter) { IQueryable<Dispensary> query = null; if (User.IsInRole("GlobalAdmin")) { query = from d in HGContext.Dispensaries.Include(d => d.Address) where !d.IsDeleted select d; } else { query = from d in HGContext.Dispensaries.Include(d => d.Address) where !d.IsHidden && !d.IsDeleted select d; } if (!String.IsNullOrEmpty(filter.name)) query = query.Where(i => i.Name.Contains(filter.name)); if (!String.IsNullOrEmpty(filter.state)) query = query.Where(i => i != null && i.Address.State != null && i.Address.State.Contains(filter.state)); if (filter.iSortCol_0 == 0) query = filter.sSortDir_0 == "desc" ? query.OrderByDescending(d => d.Name) : query.OrderBy(d => d.Name); if (filter.iSortCol_0 == 1) query = filter.sSortDir_0 == "desc" ? query.OrderByDescending(d => d.Address.State) : query.OrderBy(d => d.Address.State); if (filter.iSortCol_0 == 2) query = filter.sSortDir_0 == "desc" ? query.OrderByDescending(d => d.Type) : query.OrderBy(d => d.Type); if (filter.iSortCol_0 == 3) query = filter.sSortDir_0 == "desc" ? query.OrderByDescending(d => d.EmailAddress) : query.OrderBy(d => d.EmailAddress); if (filter.iSortCol_0 == 4) query = filter.sSortDir_0 == "desc" ? query.OrderByDescending(d => d.PhoneNumber) : query.OrderBy(d => d.PhoneNumber); var dispensaries = query.AsNoTracking().Skip(filter.iDisplayStart).Take(filter.iDisplayLength).Select(d => new DispensaryModel() { Id = d.Id, Address = new AddressModel() { Name = d.Address.Name, Address1 = d.Address.Address1, Address2 = d.Address.Address2, Country = d.Address.Country, City = d.Address.City, FormattedAddress = d.Address.FormattedAddress, Id = d.Address.Id, Latitude = d.Address.Latitude, Longitude = d.Address.Longitude, PhoneNumber = d.Address.PhoneNumber, State = d.Address.State, Zip = d.Address.Zip, }, Description = d.Description, EmailAddress = d.EmailAddress, PhoneNumber = d.PhoneNumber, HasDelivery = d.HasDelivery, HasPickup = d.HasPickup, HasStorefront = d.HasStorefront, HasScheduledDelivery = d.HasScheduledDelivery, HoursAndInfo = d.HoursAndInfo, IsHidden = d.IsHidden, IsPrivate = d.IsPrivate, Name = d.Name, Slug = d.Slug, Type = d.Type, HoursOfOperationString = d.HoursOfOperation, ApprovalZipCodesCollection = d.ApprovalZipCodes.Select(z => z.Code), DeliveryZipCodesCollection = d.DeliveryZipCodes.Select(z => z.Code) }).ToList(); foreach (DispensaryModel dispensary in dispensaries) { if (dispensary.ApprovalZipCodesCollection != null) dispensary.ApprovalZipCodes = String.Join(", ", dispensary.ApprovalZipCodesCollection); if (dispensary.DeliveryZipCodesCollection != null) dispensary.DeliveryZipCodes = String.Join(", ", dispensary.DeliveryZipCodesCollection); dispensary.HoursOfOperation = Mapper.Map<List<HoursOfOperation>>(dispensary.HoursOfOperationString); } var count = query.AsNoTracking().Count(); GridModel<DispensaryModel> gritItems = new GridModel<DispensaryModel>() { aaData = dispensaries, iTotalDisplayRecords = count, iTotalRecords = count, sEcho = filter.sEcho }; var response = Request.CreateResponse(HttpStatusCode.OK, gritItems); return response; }