add command page for specific channels

This commit is contained in:
lyx0 2024-02-11 16:54:53 +01:00
parent 4e905e8522
commit 743cc82a87
3 changed files with 53 additions and 34 deletions

View file

@ -6,7 +6,6 @@ import (
"net/http" "net/http"
"os" "os"
"sort" "sort"
"strings"
"github.com/julienschmidt/httprouter" "github.com/julienschmidt/httprouter"
"github.com/lyx0/nourybot/internal/common" "github.com/lyx0/nourybot/internal/common"
@ -17,8 +16,8 @@ func (app *application) startRouter() {
router := httprouter.New() router := httprouter.New()
router.GET("/", app.homeRoute) router.GET("/", app.homeRoute)
router.GET("/status", app.statusPageRoute) router.GET("/status", app.statusPageRoute)
router.GET("/commands/:channel", app.channelCommandsRoute)
router.GET("/commands", app.commandsRoute) router.GET("/commands", app.commandsRoute)
router.GET("/commands/:channel", app.channelCommandsRoute)
router.GET("/timer", app.timersRoute) router.GET("/timer", app.timersRoute)
router.GET("/timer/:channel", app.channelTimersRoute) router.GET("/timer/:channel", app.channelTimersRoute)
@ -34,11 +33,6 @@ type timersRouteData struct {
Timers []data.Timer Timers []data.Timer
} }
type channelTimersRouteData struct {
Timers []data.Timer
Channel string
}
func (app *application) timersRoute(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { func (app *application) timersRoute(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
t, err := template.ParseFiles("./web/templates/timers.page.gohtml") t, err := template.ParseFiles("./web/templates/timers.page.gohtml")
if err != nil { if err != nil {
@ -78,6 +72,11 @@ func (app *application) timersRoute(w http.ResponseWriter, r *http.Request, _ ht
} }
} }
type channelTimersRouteData struct {
Timers []data.Timer
Channel string
}
func (app *application) channelTimersRoute(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { func (app *application) channelTimersRoute(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
channel := ps.ByName("channel") channel := ps.ByName("channel")
t, err := template.ParseFiles("./web/templates/channeltimers.page.gohtml") t, err := template.ParseFiles("./web/templates/channeltimers.page.gohtml")
@ -191,45 +190,46 @@ func (app *application) homeRoute(w http.ResponseWriter, r *http.Request, _ http
} }
type channelCommandsRouteData struct {
Commands []data.Command
Channel string
}
func (app *application) channelCommandsRoute(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { func (app *application) channelCommandsRoute(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
channel := ps.ByName("channel") channel := ps.ByName("channel")
var cs []string t, err := template.ParseFiles("./web/templates/channelcommands.page.gohtml")
var text string
command, err := app.Models.Commands.GetAllChannel(channel)
if err != nil { if err != nil {
app.Log.Errorw("Error trying to retrieve all commands for a channel from database", err) app.Log.Error(err)
return return
} }
// The slice of timers is only used to log them at var cs []data.Command
// the start so it looks a bit nicer.
heading := fmt.Sprintf("Commands in %s\n\n", channel) commandData, err := app.Models.Commands.GetAllChannel(channel)
cs = append(cs, heading) if err != nil {
// Iterate over all timers and then add them onto the scheduler. app.Log.Errorw("Error trying to retrieve all timer for a channel from database", err)
for i, v := range command { return
}
for i, v := range commandData {
// idk why this works but it does so no touchy touchy. // idk why this works but it does so no touchy touchy.
// https://github.com/robfig/cron/issues/420#issuecomment-940949195 // https://github.com/robfig/cron/issues/420#issuecomment-940949195
i, v := i, v i, v := i, v
_ = i _ = i
var c string var c data.Command
c.Name = v.Name
c.Level = v.Level
c.Description = v.Description
c.Text = v.Text
c = fmt.Sprintf(
"Name: %v\n"+
"Description: %v\n"+
"Level: %v\n"+
"Text: %v\n"+
"\n",
v.Name, v.Description, v.Level, v.Text,
)
// Add new value to the slice
cs = append(cs, c) cs = append(cs, c)
} }
text = strings.Join(cs, "") data := &channelCommandsRouteData{cs, channel}
fmt.Fprintf(w, fmt.Sprint(text)) err = t.Execute(w, data)
if err != nil {
app.Log.Error(err)
return
}
} }
func (app *application) statusPageRoute(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { func (app *application) statusPageRoute(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {

View file

@ -0,0 +1,20 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta content="width=device-width,initial-scale=1" name="viewport">
<title>nourybot - lidl twitch bot</title>
<meta property="og:title" content="nourybot - commands in {{ .Channel }}">
<meta property="og:description" content="nourybot">
</head>
<h1>Commands in {{ .Channel }}</h1>
{{ with .Commands }}
{{ range . }}
<p>
<b>Name:</b> {{ .Name }} </br>
<b>Level:</b> {{ .Level }} </br>
<b>Description:</b> {{ .Description }} </br>
<b>Text:</b> {{ .Text }} </br>
</p>
{{ end }}
{{ end }}

View file

@ -4,11 +4,10 @@
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta content="width=device-width,initial-scale=1" name="viewport"> <meta content="width=device-width,initial-scale=1" name="viewport">
<title>nourybot - lidl twitch bot</title> <title>nourybot - lidl twitch bot</title>
<meta property="og:title" content="nourybot - timers"> <meta property="og:title" content="nourybot - timers in {{ .Channel }}">
<meta property="og:description" content="nourybot"> <meta property="og:description" content="nourybot">
</head> </head>
<h1>Timers in {{ .Channel }}</h1> <h1>Timers in {{ .Channel }}</h1>
<p>General commands:</p>
{{ with .Timers }} {{ with .Timers }}
{{ range . }} {{ range . }}
<p> <p>