add timers page for specific channels

This commit is contained in:
lyx0 2024-02-11 15:48:32 +01:00
parent c28fbff3d0
commit 4e905e8522
3 changed files with 66 additions and 38 deletions

View file

@ -34,6 +34,11 @@ 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 {
@ -73,6 +78,46 @@ func (app *application) timersRoute(w http.ResponseWriter, r *http.Request, _ ht
} }
} }
func (app *application) channelTimersRoute(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
channel := ps.ByName("channel")
t, err := template.ParseFiles("./web/templates/channeltimers.page.gohtml")
if err != nil {
app.Log.Error(err)
return
}
var ts []data.Timer
timerData, err := app.Models.Timers.GetChannelTimer(channel)
if err != nil {
app.Log.Errorw("Error trying to retrieve all timer for a channel from database", err)
return
}
// The slice of timers is only used to log them at
// the start so it looks a bit nicer.
// Iterate over all timers and then add them onto the scheduler.
for i, v := range timerData {
// idk why this works but it does so no touchy touchy.
// https://github.com/robfig/cron/issues/420#issuecomment-940949195
i, v := i, v
_ = i
var t data.Timer
t.Name = v.Name
t.Text = v.Text
t.Repeat = v.Repeat
// Add new value to the slice
ts = append(ts, t)
}
data := &channelTimersRouteData{ts, channel}
err = t.Execute(w, data)
if err != nil {
app.Log.Error(err)
return
}
}
type commandsRouteData struct { type commandsRouteData struct {
Commands map[string]command Commands map[string]command
} }
@ -187,43 +232,6 @@ func (app *application) channelCommandsRoute(w http.ResponseWriter, r *http.Requ
} }
func (app *application) channelTimersRoute(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
channel := ps.ByName("channel")
var ts []string
var text string
timer, err := app.Models.Timers.GetChannelTimer(channel)
if err != nil {
app.Log.Errorw("Error trying to retrieve all timer for a channel from database", err)
return
}
// The slice of timers is only used to log them at
// the start so it looks a bit nicer.
// Iterate over all timers and then add them onto the scheduler.
for i, v := range timer {
// idk why this works but it does so no touchy touchy.
// https://github.com/robfig/cron/issues/420#issuecomment-940949195
i, v := i, v
_ = i
var t string
t = fmt.Sprintf(
"Name: \t%v\n"+
"Text: \t%v\n"+
"Repeat: %v\n"+
"\n",
v.Name, v.Text, v.Repeat,
)
// Add new value to the slice
ts = append(ts, t)
}
text = strings.Join(ts, "")
fmt.Fprintf(w, fmt.Sprint(text))
}
func (app *application) statusPageRoute(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { func (app *application) statusPageRoute(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
commit := common.GetVersion() commit := common.GetVersion()
started := common.GetUptime().Format("2006-1-2 15:4:5") started := common.GetUptime().Format("2006-1-2 15:4:5")

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 - timers">
<meta property="og:description" content="nourybot">
</head>
<h1>Timers in {{ .Channel }}</h1>
<p>General commands:</p>
{{ with .Timers }}
{{ range . }}
<p>
<b>Name:</b> {{ .Name }} </br>
<b>Text:</b> {{ .Text }} </br>
<b>Repeat:</b> {{ .Repeat }} </br>
</p>
{{ end }}
{{ end }}

View file

@ -7,7 +7,7 @@
<meta property="og:title" content="nourybot - timers"> <meta property="og:title" content="nourybot - timers">
<meta property="og:description" content="nourybot"> <meta property="og:description" content="nourybot">
</head> </head>
<h1>Commands</h1> <h1>Timers</h1>
<p>General commands:</p> <p>General commands:</p>
{{ with .Timers }} {{ with .Timers }}
{{ range . }} {{ range . }}