Files
website/internal/handlers/template/templatedata.go

50 lines
1.5 KiB
Go

package handlers
import (
"database/sql"
"log"
"net/http"
httpHelper "synlotto-website/internal/helpers/http"
// ToDo: again, need to check if i should be using multiple stotage entries like this or if this si even correct would it not be a helper?
messageStorage "synlotto-website/internal/storage/messages"
notificationStorage "synlotto-website/internal/storage/notifications"
usersStorage "synlotto-website/internal/storage/users"
"synlotto-website/internal/models"
)
func BuildTemplateData(db *sql.DB, w http.ResponseWriter, r *http.Request) models.TemplateData {
session, err := httpHelper.GetSession(w, r)
if err != nil {
log.Printf("Session error: %v", err)
}
var user *models.User
var isAdmin bool
var notificationCount int
var notifications []models.Notification
var messageCount int
var messages []models.Message
if userId, ok := session.Values["user_id"].(int); ok {
user = usersStorage.GetUserByID(db, userId)
if user != nil {
isAdmin = user.IsAdmin
notificationCount = notificationStorage.GetNotificationCount(db, user.Id)
notifications = notificationStorage.GetRecentNotifications(db, user.Id, 15)
messageCount, _ = messageStorage.GetMessageCount(db, user.Id)
messages = messageStorage.GetRecentMessages(db, user.Id, 15)
}
}
return models.TemplateData{
User: user,
IsAdmin: isAdmin,
NotificationCount: notificationCount,
Notifications: notifications,
MessageCount: messageCount,
Messages: messages,
}
}