7
"github.com/go-chi/chi/v5"
8
"xelbot.com/reprogl/container"
9
"xelbot.com/reprogl/models"
10
"xelbot.com/reprogl/models/repositories"
11
"xelbot.com/reprogl/session"
12
"xelbot.com/reprogl/utils/hashid"
13
"xelbot.com/reprogl/views"
16
const successUnsubscribe = "success.unsubscribe"
18
func EmailUnsubscribe(app *container.Application) http.HandlerFunc {
19
return func(w http.ResponseWriter, r *http.Request) {
20
hash := chi.URLParam(r, "hash")
22
data, err := hashid.Decode(hash, false)
29
repo := repositories.EmailSubscriptionRepository{DB: app.DB}
30
settings, err := repo.Find(data.ID)
32
if errors.Is(err, models.RecordNotFound) {
35
app.ServerError(w, err)
41
_, ok := session.Pop[string](r.Context(), successUnsubscribe)
43
templateData := views.NewUnsubscribePageData(
45
models.AvatarLink(3, hashid.Male|hashid.User, 160),
46
ok || settings.BlockSending,
48
err = views.WriteTemplate(w, "unsubscribe.gohtml", templateData)
50
app.ServerError(w, err)
57
func EmailUnsubscribePost(app *container.Application) http.HandlerFunc {
58
return func(w http.ResponseWriter, r *http.Request) {
59
hash := chi.URLParam(r, "hash")
61
data, err := hashid.Decode(hash, false)
68
repo := repositories.EmailSubscriptionRepository{DB: app.DB}
69
err = repo.Unsubscribe(data.ID)
71
app.ServerError(w, err)
76
session.Put(r.Context(), successUnsubscribe, "*")
78
http.Redirect(w, r, r.URL.Path, http.StatusFound)