From 8b46a670fa6ae60e4b39de3e15859c7e84397265 Mon Sep 17 00:00:00 2001 From: "Daniel J. Summers" Date: Fri, 15 Mar 2019 22:22:32 -0500 Subject: [PATCH] Search works on request text --- src/PrayerTracker.Data/DataAccess.fs | 52 +++++++++---------- src/PrayerTracker.UI/Resources/Common.es.resx | 6 +++ src/PrayerTracker/PrayerRequest.fs | 3 +- 3 files changed, 31 insertions(+), 30 deletions(-) diff --git a/src/PrayerTracker.Data/DataAccess.fs b/src/PrayerTracker.Data/DataAccess.fs index d67d039..e9516c2 100644 --- a/src/PrayerTracker.Data/DataAccess.fs +++ b/src/PrayerTracker.Data/DataAccess.fs @@ -8,6 +8,22 @@ open PrayerTracker.Entities open System.Collections.Generic open System.Linq +[] +module private Helpers = + + /// Central place to append sort criteria for prayer request queries + let reqSort sort (query : IQueryable) = + 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 (*-- DISCONNECTED DATA EXTENSIONS --*) @@ -79,7 +95,6 @@ type AppDbContext with upcast ( this.PrayerRequests.AsNoTracking().Where(fun pr -> pr.smallGroupId = grp.smallGroupId) |> function - // Filter | query when activeOnly -> let asOf = theDate.AddDays(-(float grp.preferences.daysToExpire)).Date query.Where(fun pr -> @@ -89,16 +104,7 @@ type AppDbContext with || RequestType.Expecting = pr.requestType) && not pr.isManuallyExpired) | query -> query - |> 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)) + |> reqSort grp.preferences.requestSort) /// Count prayer requests for the given small group Id 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 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 ( - this.PrayerRequests - .AsNoTracking() - .Where(fun pr -> pr.smallGroupId = grp.smallGroupId && pr.text.Contains(searchTerm.ToLowerInvariant())) - |> 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)) + this.PrayerRequests.FromSql(sql, grp.smallGroupId, like searchTerm).AsNoTracking () + |> reqSort grp.preferences.requestSort + |> function query -> (query.Skip skip).Take pgSz) (*-- SMALL GROUP EXTENSIONS --*) diff --git a/src/PrayerTracker.UI/Resources/Common.es.resx b/src/PrayerTracker.UI/Resources/Common.es.resx index 80f9af9..978b6cf 100644 --- a/src/PrayerTracker.UI/Resources/Common.es.resx +++ b/src/PrayerTracker.UI/Resources/Common.es.resx @@ -819,4 +819,10 @@ Haga Clic para Obtener Ayuda en Esta Página + + Buscar + + + Busca las peticiones... + \ No newline at end of file diff --git a/src/PrayerTracker/PrayerRequest.fs b/src/PrayerTracker/PrayerRequest.fs index fea0722..19d49aa 100644 --- a/src/PrayerTracker/PrayerRequest.fs +++ b/src/PrayerTracker/PrayerRequest.fs @@ -201,8 +201,7 @@ let maintain onlyActive : HttpHandler = task { let reqs = match ctx.GetQueryStringValue "search" with - | Ok srch -> - Seq.empty + | Ok srch -> db.SearchRequestsForSmallGroup grp srch 1 | Error _ -> db.AllRequestsForSmallGroup grp (ctx.GetService ()) None onlyActive return! { viewInfo ctx startTicks with helpLink = Some Help.maintainRequests }