aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJordan <me@jordan.im>2021-12-26 20:55:20 -0700
committerJordan <me@jordan.im>2021-12-26 20:55:20 -0700
commit69bc97bdbe330c084d9937360dd11c04b617033d (patch)
tree4bf808c75435b93552a4ed2ebd5b90cfc2fb0376
parent9b28b7df9897353b5921dd13e2d6e07b016ac70a (diff)
downloadkeep-69bc97bdbe330c084d9937360dd11c04b617033d.tar.gz
keep-69bc97bdbe330c084d9937360dd11c04b617033d.zip
web, db: add wildcard URL search
-rw-r--r--db.go16
-rw-r--r--db_test.go22
-rw-r--r--web.go20
3 files changed, 42 insertions, 16 deletions
diff --git a/db.go b/db.go
index 6aa53a5..7521666 100644
--- a/db.go
+++ b/db.go
@@ -156,7 +156,7 @@ func (db *SqliteDB) Stats() (*Stats, error) {
}
func (db *SqliteDB) ListEntries(limit int, offset int, user string,
- guild string, channel string) (*[]Entry, error) {
+ guild string, channel string, search string) (*[]Entry, error) {
query := `
SELECT urls.id, urls.url, users.user_id, guilds.guild_id, channels.channel_id, status_code
@@ -202,6 +202,20 @@ func (db *SqliteDB) ListEntries(limit int, offset int, user string,
args = append(args, channel)
}
}
+ if search != "" {
+ if !hasWhere {
+ query = fmt.Sprintf(`%s
+ WHERE urls.url LIKE '%%' || ? || '%%'
+ `, query)
+ args = append(args, search)
+ hasWhere = true
+ } else {
+ query = fmt.Sprintf(`%s
+ AND urls.url LIKE '%%' || ? || '%%'
+ `, query)
+ args = append(args, search)
+ }
+ }
query = fmt.Sprintf(`%s
ORDER BY urls.id DESC
LIMIT ? OFFSET ?;`, query)
diff --git a/db_test.go b/db_test.go
index d2a1ee1..4ec5a01 100644
--- a/db_test.go
+++ b/db_test.go
@@ -51,37 +51,45 @@ func TestDB(t *testing.T) {
}
// ListEntries()
- e, err := db.ListEntries(10, 0, "", "", "")
+ e, err := db.ListEntries(10, 0, "", "", "", "")
if err != nil {
t.Error(err)
}
if len(*e) != 2 {
t.Errorf("ListEntries(): Recieved length %d; wanted %d", len(*e), 2)
}
- e, err = db.ListEntries(10, 0, "000000000000000000", "", "")
+ e, err = db.ListEntries(10, 0, "000000000000000000", "", "", "")
if len(*e) != 1 {
t.Errorf("ListEntries(): Recieved length %d; wanted %d", len(*e), 2)
}
- e, err = db.ListEntries(10, 0, "", "222222222222222222", "")
+ e, err = db.ListEntries(10, 0, "", "222222222222222222", "", "")
if len(*e) != 2 {
t.Errorf("ListEntries(): Recieved length %d; wanted %d", len(*e), 2)
}
- e, err = db.ListEntries(10, 0, "", "", "333333333333333333")
+ e, err = db.ListEntries(10, 0, "", "", "333333333333333333", "")
if len(*e) != 1 {
t.Errorf("ListEntries(): Recieved length %d; wanted %d", len(*e), 2)
}
- e, err = db.ListEntries(10, 0, "111111111111111111", "222222222222222222", "")
+ e, err = db.ListEntries(10, 0, "111111111111111111", "222222222222222222", "", "")
if len(*e) != 1 {
t.Errorf("ListEntries(): Recieved length %d; wanted %d", len(*e), 2)
}
- e, err = db.ListEntries(10, 0, "111111111111111111", "", "333333333333333333")
+ e, err = db.ListEntries(10, 0, "111111111111111111", "", "333333333333333333", "")
if len(*e) != 1 {
t.Errorf("ListEntries(): Recieved length %d; wanted %d", len(*e), 2)
}
if len(*e) != 1 {
t.Errorf("ListEntries(): Recieved length %d; wanted %d", len(*e), 2)
}
- e, err = db.ListEntries(10, 0, "111111111111111111", "222222222222222222", "333333333333333333")
+ e, err = db.ListEntries(10, 0, "111111111111111111", "222222222222222222", "333333333333333333", "")
+ if len(*e) != 1 {
+ t.Errorf("ListEntries(): Recieved length %d; wanted %d", len(*e), 2)
+ }
+ e, err = db.ListEntries(10, 0, "", "", "", "example")
+ if len(*e) != 2 {
+ t.Errorf("ListEntries(): Recieved length %d; wanted %d", len(*e), 2)
+ }
+ e, err = db.ListEntries(10, 0, "", "", "333333333333333333", "example")
if len(*e) != 1 {
t.Errorf("ListEntries(): Recieved length %d; wanted %d", len(*e), 2)
}
diff --git a/web.go b/web.go
index 3f5776b..40a8a87 100644
--- a/web.go
+++ b/web.go
@@ -18,6 +18,7 @@ type Resp struct {
User string
Guild string
Channel string
+ Search string
}
var funcMap = template.FuncMap{
@@ -70,6 +71,9 @@ const index = `
display: flex;
justify-content: space-between;
}
+ input {
+ max-width: 12em;
+ }
</style>
</head>
<body>
@@ -84,22 +88,21 @@ const index = `
</p>
<div style="padding-top:5px; padding-bottom:5px;">
<form action="" method="get">
- <label for="user">User:</label>
- <input type="text" id="user" name="user">
- <label for="guild">Guild:</label>
- <input type="text" id="guild" name="guild">
- <label for="channel">Channel:</label>
- <input type="text" id="channel" name="channel">
+ <input type="text" id="user" name="user" placeholder="User ID">
+ <input type="text" id="guild" name="guild" placeholder="Guild ID">
+ <input type="text" id="channel" name="channel" placeholder="Channel ID">
+ <input type="text" id="search" name="search" placeholder="URL Search">
<input type="submit" value="Filter">
</form>
</div>
<p>
- {{- if or (ne .User "") (ne .Guild "") (ne .Channel "") -}}
+ {{- if or (ne .User "") (ne .Guild "") (ne .Channel "") (ne .Search "" ) -}}
Entries filtered by:
{{- end -}}
{{- if ne .User "" }} <b>User</b> ({{ .User -}}){{- end -}}
{{- if ne .Guild "" }} <b>Guild</b> ({{ .Guild -}}){{- end -}}
{{- if ne .Channel "" }} <b>Channel</b> ({{ .Channel -}}){{- end -}}
+ {{- if ne .Search "" }} <b>URL</b> ({{ .Search -}}){{- end -}}
</p>
{{- if gt (len .Entries) 0 -}}
<div id="navigate">
@@ -202,9 +205,10 @@ func (db *SqliteDB) IndexHandler(w http.ResponseWriter, r *http.Request) {
resp.User = query.Get("user")
resp.Guild = query.Get("guild")
resp.Channel = query.Get("channel")
+ resp.Search = query.Get("search")
resp.Entries, resp.Err = db.ListEntries(100, resp.Offset, resp.User,
- resp.Guild, resp.Channel)
+ resp.Guild, resp.Channel, resp.Search)
if resp.Err != nil {
log.Println(resp.Err)
}