diff options
author | Jordan <me@jordan.im> | 2021-12-26 20:55:20 -0700 |
---|---|---|
committer | Jordan <me@jordan.im> | 2021-12-26 20:55:20 -0700 |
commit | 69bc97bdbe330c084d9937360dd11c04b617033d (patch) | |
tree | 4bf808c75435b93552a4ed2ebd5b90cfc2fb0376 | |
parent | 9b28b7df9897353b5921dd13e2d6e07b016ac70a (diff) | |
download | keep-69bc97bdbe330c084d9937360dd11c04b617033d.tar.gz keep-69bc97bdbe330c084d9937360dd11c04b617033d.zip |
web, db: add wildcard URL search
-rw-r--r-- | db.go | 16 | ||||
-rw-r--r-- | db_test.go | 22 | ||||
-rw-r--r-- | web.go | 20 |
3 files changed, 42 insertions, 16 deletions
@@ -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) @@ -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) } @@ -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) } |