public async Task <IPagedResults <EvacueeListItem> > GetEvacueesAsync(SearchQueryParameters searchQuery)
        {
            var query = db.Evacuees
                        .Include(e => e.EvacueeRegistration)
                        .ThenInclude(r => r.IncidentTask)
                        .ThenInclude(i => i.Community)
                        .Include(e => e.EvacueeRegistration)
                        .ThenInclude(r => r.IncidentTask)
                        .ThenInclude(i => i.Region)
                        .Include(e => e.EvacueeRegistration)
                        .ThenInclude(r => r.HostCommunity)
                        .Include(e => e.Referrals)
                        .Where(e => e.EvacueeRegistration.Active == searchQuery.Active);

            if (searchQuery.HasQuery())
            {
                query = query.Where(e =>
                                    EF.Functions.Like(e.LastName, $"{searchQuery.Query}%") ||
                                    e.EvacueeRegistration.IncidentTask.TaskNumber == searchQuery.Query ||
                                    e.EvacueeRegistration.EssFileNumber.ToString() == searchQuery.Query ||
                                    EF.Functions.Like(e.EvacueeRegistration.HostCommunity.Name, $"%{searchQuery.Query}%") ||
                                    EF.Functions.Like(e.EvacueeRegistration.IncidentTask.Community.Name, $"%{searchQuery.Query}%")
                                    );
            }

            if (!searchQuery.HasSortBy())
            {
                searchQuery.SortBy = "-essFileNumber";
            }

            var results = await query.Sort(MapSortToFields(searchQuery.SortBy)).ToArrayAsync();

            return(new PaginatedList <EvacueeListItem>(results.Select(mapper.Map <EvacueeListItem>), searchQuery.Offset, searchQuery.Limit));
        }
        public async Task <IPagedResults <IncidentTask> > GetIncidentTasksAsync(SearchQueryParameters searchQuery)
        {
            var items = await IncidentTasks
                        .GroupJoin(db.EvacueeRegistrations.Select(e => new { e.IncidentTaskId, evacueeCount = e.Evacuees.Count() }),
                                   incident => incident.Id,
                                   summary => summary.IncidentTaskId,
                                   (incident, summary) => new { incident = incident, evacueeCount = summary.Sum(s => s.evacueeCount) }
                                   )
                        .Where(t => !searchQuery.HasQuery() || t.incident.Community.Id == Guid.Parse(searchQuery.Query))
                        .Where(t => searchQuery.Active == t.incident.Active)
                        .Sort(searchQuery.SortBy ?? "incident.id")
                        .ToArrayAsync();

            return(new PaginatedList <IncidentTask>(items.Select(t => t.incident.ToViewModel(t.evacueeCount)), searchQuery.Offset, searchQuery.Limit));
        }
Exemple #3
0
        public async Task <IPagedResults <Organization> > GetOrganizationsAsync(SearchQueryParameters searchQuery)
        {
            Guid?  communityId = null;
            string regionName  = null;

            if (searchQuery.HasQuery() && Guid.TryParse(searchQuery.Query, out Guid searchEntityId))
            {
                communityId = searchEntityId;
            }
            else if (searchQuery.HasQuery())
            {
                regionName = searchQuery.Query;
            }
            var items = await Organizations
                        .Where(o => (!communityId.HasValue || o.Community.Id == communityId) &&
                               (string.IsNullOrEmpty(regionName) || o.RegionName == regionName)
                               )
                        .Where(t => searchQuery.Active == t.Active)
                        .Sort(searchQuery.SortBy ?? "id")
                        .Join(Volunteers.Where(v => v.IsPrimaryContact ?? false), o => o.Id, pc => pc.Organization.Id, (org, pc) => new { org, pc }) //Assume a single primary contact
                        .ToArrayAsync();

            return(new PaginatedList <Organization>(items.Select(i => MapToViewModel(i.org, i.pc)), searchQuery.Offset, searchQuery.Limit));
        }