summaryrefslogtreecommitdiff
path: root/internal/handlers/main.go
blob: 1f2276c39f1270f482bfc3787987eb6bf08e09a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package handlers

import (
	"apjournal/config"
	"apjournal/internal/database/repos"
	"apjournal/internal/models"
	"html/template"
	"log/slog"
	"net/http"
	"os"
	"strconv"
	"time"

	"github.com/jmoiron/sqlx"
)

// Handlers structure
type Handlers struct {
	cfg  config.Config
	log  *slog.Logger
	repo *repos.Provider
}

// NewHandlers constructor
func NewHandlers(
	// cfg config.Config, s *service.Service, l *slog.Logger,
	cfg config.Config, l *slog.Logger, conn *sqlx.DB,
) *Handlers {
	if l == nil {
		l = slog.New(slog.NewJSONHandler(os.Stdout, nil))
	}
	h := &Handlers{
		cfg:  cfg,
		log:  l,
		repo: repos.NewProvider(conn),
	}
	return h
}

// FIXME: global userscore for test
var us = models.UserScore{
	Username: "test",
	BurnTime: time.Now().Add(time.Duration(24) * time.Hour),
}

func (h *Handlers) Ping(w http.ResponseWriter, r *http.Request) {
	h.log.Info("got ping request")
	w.Write([]byte("pong"))
}

func (h *Handlers) MainPage(w http.ResponseWriter, r *http.Request) {
	h.log.Info("got mainpage request")
	// tmpl := template.Must(template.ParseFiles("components/index.html"))
	tmpl, err := template.ParseGlob("components/*.html")
	if err != nil {
		panic(err)
	}
	// tmpl.Execute(w, us)
	// us.Username = "test"
	// us.BurnTime = time.Now().Add(time.Duration(24) * time.Hour)
	tmpl.ExecuteTemplate(w, "main", us)
}

func (h *Handlers) HandleForm(w http.ResponseWriter, r *http.Request) {
	r.ParseForm()
	h.log.Info("got postform request", "payload", r.PostForm)
	magnitude := uint8(1)
	mS := r.PostFormValue("magnitude")
	h.log.Info("showing magnitude send", "mS", mS)
	if mS != "1" {
		u64, err := strconv.ParseUint(mS, 10, 64)
		magnitude = uint8(u64)
		if err != nil {
			// TODO: error handling
			h.log.Warn("got an error", "error", err)
			magnitude = uint8(1)
		}
	}
	repeat := false
	var at models.ActionType
	switch r.PostFormValue("act_type") {
	case "plus":
		at = models.ActionTypePlus
	case "minus":
		at = models.ActionTypeMinus
		repeat = true
	default:
		h.log.Warn("uknown actiontype", "type", r.PostFormValue("act_type"))
	}
	// if r.PostFormValue("repeatable") == "on" {
	// 	repeat = true
	// }
	// convert map to action object
	act := models.Action{
		Name:       r.PostFormValue("name"),
		Magnitude:  magnitude,
		Type:       at,
		Repeatable: repeat,
	}
	// TODO: check that name + userid key is unique
	us.Actions = append(us.Actions, &act)
	// TODO: redirect to the main page instead
	http.Redirect(w, r, "/", 302)

	// tmpl := template.Must(template.ParseGlob("components/*.html"))
	// // tmpl := template.Must(template.ParseFiles("components/index.html"))
	// // tmpl.Execute(w, us)
	// tmpl.ExecuteTemplate(w, "main", us)
}

func (h *Handlers) HandleDoneAction(w http.ResponseWriter, r *http.Request) {
	r.ParseForm()
	h.log.Info("got done request", "payload", r.PostForm)
	actionName := r.PostFormValue("name")
	h.log.Info("got postform request", "name", actionName)
	// change counter of user score
	// get action by name
	for _, act := range us.Actions {
		if act.Name != actionName {
			continue
		}
		us.UpdateScore(act)
		h.log.Info("show action", "act", act, "us", us)
	}
	tmpl := template.Must(template.ParseGlob("components/*.html"))
	tmpl.ExecuteTemplate(w, "main", us)
	// http.Redirect(w, r, "/", 302)
}