-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpage.go
63 lines (58 loc) · 1.46 KB
/
page.go
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
package main
import (
"log"
"log/slog"
"net/http"
"time"
"github.com/devilcove/timetraced/database"
"github.com/devilcove/timetraced/models"
"github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin"
)
func displayMain(c *gin.Context) {
var page models.Page
session := sessions.Default(c)
user := session.Get("user")
loggedIn := session.Get("loggedin")
slog.Debug("displaying status for", "user", user, "loggedIn", loggedIn)
if user == nil {
page = populatePage("")
} else {
page = populatePage(user.(string))
}
if loggedIn == nil {
page.NeedsLogin = true
}
slog.Debug("displaystatus", "page", page.NeedsLogin, "refresh", page.Refresh)
c.HTML(http.StatusOK, "layout", page)
}
func displayStatus(c *gin.Context) {
session := sessions.Default(c)
user := session.Get("user")
loggedIn := session.Get("loggedin")
if user == nil || loggedIn == nil {
displayMain(c)
return
}
page := populatePage(user.(string))
c.HTML(http.StatusOK, "content", page)
}
func populatePage(user string) models.Page {
page := models.GetUserPage(user)
page.Tracking = models.IsTrackingActive(user)
projects, err := database.GetAllProjects()
if err != nil {
slog.Error(err.Error())
} else {
for _, project := range projects {
page.Projects = append(page.Projects, project.Name)
}
}
status, err := getStatus(user)
if err != nil {
log.Println("getStatus", err)
}
page.Status = status
page.DefaultDate = time.Now().Local().Format("2006-01-02")
return page
}