summaryrefslogtreecommitdiff
path: root/internal/handlers/main.go
blob: 5546d9a875c4c89652f83b7c733eac96dd17dacb (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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package handlers

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

var defUS = models.UserScore{}

// Handlers structure
type Handlers struct {
	cfg  config.Config
	log  *slog.Logger
	repo repos.FullRepo
	mc   cache.Cache
}

// NewHandlers constructor
func NewHandlers(
	cfg config.Config, l *slog.Logger, repo repos.FullRepo,
) *Handlers {
	if l == nil {
		l = slog.New(slog.NewJSONHandler(os.Stdout, nil))
	}
	h := &Handlers{
		cfg:  cfg,
		log:  l,
		repo: repo,
		mc:   cache.MemCache,
	}
	return h
}

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) {
	tmpl, err := template.ParseGlob("components/*.html")
	if err != nil {
		abortWithError(w, err.Error())
		return
	}
	// get recommendations
	defUS.Recommendations, err = h.fetchRecommendations("", 5)
	if err != nil {
		panic(err)
	}
	usernameRaw := r.Context().Value("username")
	h.log.Info("got mainpage request", "username", usernameRaw)
	if usernameRaw == nil {
		tmpl.ExecuteTemplate(w, "main", defUS)
		return
	}
	username := usernameRaw.(string)
	if username == "" {
		tmpl.ExecuteTemplate(w, "main", defUS)
		return
	}
	userScore, err := h.repo.DBUserScoreGet(username)
	if err != nil {
		h.log.Warn("got db err", "err", err)
		tmpl.ExecuteTemplate(w, "main", defUS)
		return
	}
	userScore.Actions, err = h.repo.DBActionList(username)
	if err != nil {
		abortWithError(w, err.Error())
		return
	}
	userScore.Recommendations, err = h.fetchRecommendations(username, 5)
	if err != nil {
		abortWithError(w, err.Error())
		return
	}
	tmpl.ExecuteTemplate(w, "main", userScore)
}

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")
	if mS != "1" {
		u64, err := strconv.ParseUint(mS, 10, 64)
		magnitude = uint8(u64)
		if err != nil {
			h.log.Warn("failed to parse magnitude", "error", err,
				"payload", r.PostForm)
			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,
		CreatedAt:  time.Now(),
	}
	// get username from ctx
	username := r.Context().Value("username").(string)
	h.log.Info("got username from ctx", "username", username)
	userScore, err := h.repo.DBUserScoreGet(username)
	if err != nil {
		abortWithError(w, err.Error())
		return
	}
	act.Username = userScore.Username
	if err := h.repo.DBActionCreate(&act); err != nil {
		abortWithError(w, err.Error())
		return
	}
	http.Redirect(w, r, "/", 302)
}

func (h *Handlers) UserScoreWithActionsByUsername(
	username string,
) (*models.UserScore, error) {
	userScore, err := h.repo.DBUserScoreGet(username)
	if err != nil {
		return nil, err
	}
	list, err := h.repo.DBActionList(username)
	if err != nil {
		return nil, err
	}
	userScore.Actions = list
	return userScore, nil
}

func (h *Handlers) HandleDoneAction(w http.ResponseWriter, r *http.Request) {
	r.ParseForm()
	actionName := r.PostFormValue("name")
	username := r.Context().Value("username").(string)
	h.log.Info("got postform request", "name", actionName,
		"username", username)
	userScore, err := h.UserScoreWithActionsByUsername(username)
	if err != nil {
		abortWithError(w, err.Error())
		return
	}
	// get action by name
	action, err := h.repo.DBActionGetByName(actionName)
	magnitude := int8(action.Magnitude)
	if action.Type == models.ActionTypeMinus {
		magnitude *= -1
	}
	// change counter of user score
	userScore.Score += magnitude
	// disable action if repetable
	if err := h.repo.DBActionDone(actionName); err != nil {
		abortWithError(w, err.Error())
		return
	}
	// update score in db
	if err := h.repo.DBUserScoreUpdate(userScore); err != nil {
		abortWithError(w, err.Error())
		return
	}
	tmpl := template.Must(template.ParseGlob("components/*.html"))
	tmpl.ExecuteTemplate(w, "main", userScore)
}