Refactor and remove sqlite and replace with MySQL
This commit is contained in:
46
internal/handlers/template/templatedata.go
Normal file
46
internal/handlers/template/templatedata.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
httpHelper "synlotto-website/helpers/http"
|
||||
|
||||
"synlotto-website/models"
|
||||
"synlotto-website/storage"
|
||||
)
|
||||
|
||||
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 = storage.GetUserByID(db, userId)
|
||||
if user != nil {
|
||||
isAdmin = user.IsAdmin
|
||||
notificationCount = storage.GetNotificationCount(db, user.Id)
|
||||
notifications = storage.GetRecentNotifications(db, user.Id, 15)
|
||||
messageCount, _ = storage.GetMessageCount(db, user.Id)
|
||||
messages = storage.GetRecentMessages(db, user.Id, 15)
|
||||
}
|
||||
}
|
||||
|
||||
return models.TemplateData{
|
||||
User: user,
|
||||
IsAdmin: isAdmin,
|
||||
NotificationCount: notificationCount,
|
||||
Notifications: notifications,
|
||||
MessageCount: messageCount,
|
||||
Messages: messages,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user