Text search for the change log

This commit is contained in:
Noah Petherbridge 2024-04-04 23:05:16 -07:00
parent 268a177412
commit 763b9e4404
3 changed files with 38 additions and 11 deletions

View File

@ -31,6 +31,8 @@ func ChangeLog() http.HandlerFunc {
adminUser = r.FormValue("admin_user_id")
event = r.FormValue("event")
sort = r.FormValue("sort")
searchQuery = r.FormValue("search")
search = models.ParseSearchString(searchQuery)
sortOK bool
)
@ -80,7 +82,7 @@ func ChangeLog() http.HandlerFunc {
}
pager.ParsePage(r)
cl, err := models.PaginateChangeLog(tableName, tableID, aboutUserID, adminUserID, event, pager)
cl, err := models.PaginateChangeLog(tableName, tableID, aboutUserID, adminUserID, event, search, pager)
if err != nil {
session.FlashError(w, r, "Error paginating the change log: %s", err)
}
@ -115,6 +117,7 @@ func ChangeLog() http.HandlerFunc {
"AboutUserID": aboutUser,
"AdminUserID": adminUser,
"Event": event,
"SearchQuery": searchQuery,
"Sort": sort,
}
if err := tmpl.Execute(w, r, vars); err != nil {

View File

@ -52,7 +52,7 @@ var ChangeLogEventTypes = []string{
}
// PaginateChangeLog lists the change logs.
func PaginateChangeLog(tableName string, tableID, aboutUserID, adminUserID uint64, event string, pager *Pagination) ([]*ChangeLog, error) {
func PaginateChangeLog(tableName string, tableID, aboutUserID, adminUserID uint64, event string, search *Search, pager *Pagination) ([]*ChangeLog, error) {
var (
cl = []*ChangeLog{}
where = []string{}
@ -84,6 +84,18 @@ func PaginateChangeLog(tableName string, tableID, aboutUserID, adminUserID uint6
placeholders = append(placeholders, event)
}
// Text search terms
for _, term := range search.Includes {
var ilike = "%" + strings.ToLower(term) + "%"
where = append(where, "change_logs.message ILIKE ?")
placeholders = append(placeholders, ilike)
}
for _, term := range search.Excludes {
var ilike = "%" + strings.ToLower(term) + "%"
where = append(where, "change_logs.message NOT ILIKE ?")
placeholders = append(placeholders, ilike)
}
query := DB.Model(&ChangeLog{}).Where(
strings.Join(where, " AND "),
placeholders...,

View File

@ -75,7 +75,7 @@
</div>
</div>
<div class="column px-1">
<div class="column pl-1">
<div class="field">
<label class="label">Admin User:</label>
<input type="text" class="input"
@ -87,7 +87,9 @@
</p>
</div>
</div>
</div>
<div class="columns">
<div class="column px-1">
<div class="field">
<label class="label">Event Type:</label>
@ -102,6 +104,16 @@
</div>
</div>
<div class="column is-narrow">
<label class="label">Search query:</label>
<input type="text" class="input" name="search" value="{{$Root.SearchQuery}}">
<p class="help">
Tip: you can <span class="has-text-success">"quote exact phrases"</span> and
<span class="has-text-success">-exclude</span> words (or
<span class="has-text-success">-"exclude phrases"</span>) from your search.
</p>
</div>
<div class="column px-1">
<div class="field">
<label class="label">Sort:</label>
@ -113,15 +125,15 @@
</div>
</div>
</div>
</div>
<div class="has-text-centered">
<a href="{{.Request.URL.Path}}" class="button">Reset</a>
<button type="submit" class="button is-success">
<span>Search</span>
<span class="icon"><i class="fa fa-search"></i></span>
</button>
<div class="column has-text-right">
<br>
<a href="{{.Request.URL.Path}}" class="button">Reset</a>
<button type="submit" class="button is-success">
<span>Search</span>
<span class="icon"><i class="fa fa-search"></i></span>
</button>
</div>
</div>
</div>
</div>