[quotedb] Add simple page to list quotes

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2021-10-22 22:27:25 +02:00
parent 411756fa45
commit 4ead12f536
Signed by: luzifer
GPG key ID: 0066F03ED215AD7D
3 changed files with 84 additions and 1 deletions

View file

@ -1,6 +1,7 @@
package quotedb
import (
_ "embed"
"encoding/json"
"net/http"
"strconv"
@ -11,7 +12,22 @@ import (
"github.com/pkg/errors"
)
var (
//go:embed list.html
listFrontend []byte
//go:embed list.js
listScript []byte
)
func registerAPI(register plugins.HTTPRouteRegistrationFunc) {
register(plugins.HTTPRouteRegistrationArgs{
HandlerFunc: handleScript,
Method: http.MethodGet,
Module: "quotedb",
Path: "/app.js",
SkipDocumentation: true,
})
register(plugins.HTTPRouteRegistrationArgs{
Description: "Add quotes for the given {channel}",
HandlerFunc: handleAddQuotes,
@ -49,13 +65,14 @@ func registerAPI(register plugins.HTTPRouteRegistrationFunc) {
})
register(plugins.HTTPRouteRegistrationArgs{
Accept: []string{"application/json", "text/html"},
Description: "Lists all quotes for the given {channel}",
HandlerFunc: handleListQuotes,
Method: http.MethodGet,
Module: "quotedb",
Name: "List Quotes",
Path: "/{channel}",
ResponseType: plugins.HTTPRouteResponseTypeJSON,
ResponseType: plugins.HTTPRouteResponseTypeMultiple,
RouteParams: []plugins.HTTPRouteParamDocumentation{
{
Description: "Channel to delete the quote in",
@ -145,6 +162,12 @@ func handleDeleteQuote(w http.ResponseWriter, r *http.Request) {
}
func handleListQuotes(w http.ResponseWriter, r *http.Request) {
if strings.HasPrefix(r.Header.Get("Accept"), "text/html") {
w.Header().Set("Content-Type", "text/html")
w.Write(listFrontend)
return
}
channel := "#" + strings.TrimLeft(mux.Vars(r)["channel"], "#")
quotes := storedObject.GetChannelQuotes(channel)
@ -174,6 +197,11 @@ func handleReplaceQuotes(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNoContent)
}
func handleScript(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/javascript")
w.Write(listScript)
}
func handleUpdateQuote(w http.ResponseWriter, r *http.Request) {
var (
channel = "#" + strings.TrimLeft(mux.Vars(r)["channel"], "#")

View file

@ -0,0 +1,30 @@
<html>
<title>Quote-Database</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/combine/npm/bootstrap@4/dist/css/bootstrap.min.css,npm/bootstrap-vue@2/dist/bootstrap-vue.min.css,npm/bootswatch@4/dist/darkly/bootstrap.min.css">
<style>
[v-cloak] {
display: none;
}
</style>
<div id="app" v-cloak>
<b-container class="my-3">
<b-row>
<b-col>
<b-table
:fields="fields"
:items="quoteItems"
sort-by="id"
:sort-desc="true"
striped
></b-table>
</b-col>
</b-row>
</b-container>
</div>
<script src="https://cdn.jsdelivr.net/combine/npm/axios@0.21.1/dist/axios.min.js,npm/vue@2,npm/bootstrap-vue@2/dist/bootstrap-vue.min.js"></script>
<script src="app.js"></script>
</html>

View file

@ -0,0 +1,25 @@
new Vue({
computed: {
quoteItems() {
return this.quotes.map((q, i) => ({ id: i + 1, quote: q }))
},
},
data: {
fields: [
{ key: 'id', label: 'ID', sortable: true, sortDirection: 'desc' },
{ key: 'quote' },
],
quotes: [],
},
el: '#app',
mounted() {
axios.get(window.location.href)
.then(res => {
this.quotes = res.data
})
},
})