Search, Paging, and "As of" Date #10

Merged
danieljsummers merged 15 commits from search into master 2019-03-21 00:19:02 +00:00
3 changed files with 31 additions and 30 deletions
Showing only changes of commit 8b46a670fa - Show all commits

View File

@ -8,6 +8,22 @@ open PrayerTracker.Entities
open System.Collections.Generic open System.Collections.Generic
open System.Linq open System.Linq
[<AutoOpen>]
module private Helpers =
/// Central place to append sort criteria for prayer request queries
let reqSort sort (query : IQueryable<PrayerRequest>) =
match sort with
| "D" ->
query.OrderByDescending(fun pr -> pr.updatedDate)
.ThenByDescending(fun pr -> pr.enteredDate)
.ThenBy(fun pr -> pr.requestor)
| _ ->
query.OrderBy(fun pr -> pr.requestor)
.ThenByDescending(fun pr -> pr.updatedDate)
.ThenByDescending(fun pr -> pr.enteredDate)
type AppDbContext with type AppDbContext with
(*-- DISCONNECTED DATA EXTENSIONS --*) (*-- DISCONNECTED DATA EXTENSIONS --*)
@ -79,7 +95,6 @@ type AppDbContext with
upcast ( upcast (
this.PrayerRequests.AsNoTracking().Where(fun pr -> pr.smallGroupId = grp.smallGroupId) this.PrayerRequests.AsNoTracking().Where(fun pr -> pr.smallGroupId = grp.smallGroupId)
|> function |> function
// Filter
| query when activeOnly -> | query when activeOnly ->
let asOf = theDate.AddDays(-(float grp.preferences.daysToExpire)).Date let asOf = theDate.AddDays(-(float grp.preferences.daysToExpire)).Date
query.Where(fun pr -> query.Where(fun pr ->
@ -89,16 +104,7 @@ type AppDbContext with
|| RequestType.Expecting = pr.requestType) || RequestType.Expecting = pr.requestType)
&& not pr.isManuallyExpired) && not pr.isManuallyExpired)
| query -> query | query -> query
|> function |> reqSort grp.preferences.requestSort)
// Sort
| query when grp.preferences.requestSort = "D" ->
query.OrderByDescending(fun pr -> pr.updatedDate)
.ThenByDescending(fun pr -> pr.enteredDate)
.ThenBy(fun pr -> pr.requestor)
| query ->
query.OrderBy(fun pr -> pr.requestor)
.ThenByDescending(fun pr -> pr.updatedDate)
.ThenByDescending(fun pr -> pr.enteredDate))
/// Count prayer requests for the given small group Id /// Count prayer requests for the given small group Id
member this.CountRequestsBySmallGroup gId = member this.CountRequestsBySmallGroup gId =
@ -110,24 +116,14 @@ type AppDbContext with
/// Get all (or active) requests for a small group as of now or the specified date /// Get all (or active) requests for a small group as of now or the specified date
member this.SearchRequestsForSmallGroup (grp : SmallGroup) (searchTerm : string) pageNbr : PrayerRequest seq = member this.SearchRequestsForSmallGroup (grp : SmallGroup) (searchTerm : string) pageNbr : PrayerRequest seq =
let skip = (pageNbr - 1) * 100 let pgSz = grp.preferences.pageSize
let skip = (pageNbr - 1) * pgSz
let sql = RawSqlString """SELECT * FROM pt."PrayerRequest" WHERE "SmallGroupId" = {0} AND "Text" ILIKE {1}"""
let like = sprintf "%%%s%%"
upcast ( upcast (
this.PrayerRequests this.PrayerRequests.FromSql(sql, grp.smallGroupId, like searchTerm).AsNoTracking ()
.AsNoTracking() |> reqSort grp.preferences.requestSort
.Where(fun pr -> pr.smallGroupId = grp.smallGroupId && pr.text.Contains(searchTerm.ToLowerInvariant())) |> function query -> (query.Skip skip).Take pgSz)
|> function
// Sort
| query when grp.preferences.requestSort = "D" ->
query.OrderByDescending(fun pr -> pr.updatedDate)
.ThenByDescending(fun pr -> pr.enteredDate)
.ThenBy(fun pr -> pr.requestor)
| query ->
query.OrderBy(fun pr -> pr.requestor)
.ThenByDescending(fun pr -> pr.updatedDate)
.ThenByDescending(fun pr -> pr.enteredDate)
|> function
// Pagination
| query -> query.Skip(skip).Take(100))
(*-- SMALL GROUP EXTENSIONS --*) (*-- SMALL GROUP EXTENSIONS --*)

View File

@ -819,4 +819,10 @@
<data name="Click for Help on This Page" xml:space="preserve"> <data name="Click for Help on This Page" xml:space="preserve">
<value>Haga Clic para Obtener Ayuda en Esta Página</value> <value>Haga Clic para Obtener Ayuda en Esta Página</value>
</data> </data>
<data name="Search" xml:space="preserve">
<value>Buscar</value>
</data>
<data name="Search requests..." xml:space="preserve">
<value>Busca las peticiones...</value>
</data>
</root> </root>

View File

@ -201,8 +201,7 @@ let maintain onlyActive : HttpHandler =
task { task {
let reqs = let reqs =
match ctx.GetQueryStringValue "search" with match ctx.GetQueryStringValue "search" with
| Ok srch -> | Ok srch -> db.SearchRequestsForSmallGroup grp srch 1
Seq.empty
| Error _ -> db.AllRequestsForSmallGroup grp (ctx.GetService<IClock> ()) None onlyActive | Error _ -> db.AllRequestsForSmallGroup grp (ctx.GetService<IClock> ()) None onlyActive
return! return!
{ viewInfo ctx startTicks with helpLink = Some Help.maintainRequests } { viewInfo ctx startTicks with helpLink = Some Help.maintainRequests }