DEVELOPMENT ENVIRONMENT

~liljamo/tixe

ref: fff9260842462f4b7bbc4b889ae11eb141ae5143 tixe/api/user_settings.go -rw-r--r-- 1.2 KiB
fff92608Jonni Liljamo docs: remove old comment 1 year, 20 days ago
                                                                                
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
package api

import (
	"context"
	"log"
	"net/http"
	"tixe/db"
	"tixe/types"

	"github.com/gin-contrib/sessions"
	"github.com/gin-gonic/gin"
)

type postDisplayName struct {
	DisplayName string `form:"display_name"`
}

func UserUpdateDisplayName(c *gin.Context) {
	data := &postDisplayName{}
	if err := c.Bind(data); err != nil {
		log.Printf("[tixe/api] ERROR: Could not bind display name update data: %v", err)
		c.String(http.StatusBadRequest, "Could not bind display name update data")
		return;
	}

	session := sessions.Default(c)
	user := session.Get("user").(types.User)

	_, err := db.PgPool.Exec(context.Background(),
		"UPDATE users SET display_name = $1 WHERE id = $2", data.DisplayName, user.Id)
	if err != nil {
		log.Printf("[tixe/api] ERROR: Could not update display name in database: %v", err)
		c.String(http.StatusInternalServerError, "Could not update display name in database")
		return;
	}

	// Update session data
	session.Set("user", types.User { Id: user.Id, DisplayName: data.DisplayName })
	if err := session.Save(); err != nil {
		log.Printf("[tixe/auth] ERROR: Failed to save session: %v", err)
		c.String(http.StatusInternalServerError, "Failed to save session!")
		return
	}

	c.Redirect(http.StatusFound, "/settings")
}