Add message system (inbox, read view, dropdown) and truncate helper
Implemented message retrieval and read logic in storage layer Added handlers for inbox and individual message view Integrated messages into topbar dropdown with unread badge Added truncate helper to template functions Created new templates: messages/index.html and messages/read.html Fixed missing template function error in topbar rendering
This commit is contained in:
54
handlers/messages.go
Normal file
54
handlers/messages.go
Normal file
@@ -0,0 +1,54 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"synlotto-website/helpers"
|
||||
"synlotto-website/storage"
|
||||
)
|
||||
|
||||
func MessagesInboxHandler(db *sql.DB) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
data := BuildTemplateData(db, w, r)
|
||||
context := helpers.TemplateContext(w, r, data)
|
||||
|
||||
tmpl := helpers.LoadTemplateFiles("messages.html", "templates/account/messages/index.html")
|
||||
|
||||
err := tmpl.ExecuteTemplate(w, "layout", context)
|
||||
if err != nil {
|
||||
helpers.RenderError(w, r, 500)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func ReadMessageHandler(db *sql.DB) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
idStr := r.URL.Query().Get("id")
|
||||
messageID := helpers.Atoi(idStr)
|
||||
|
||||
session, _ := helpers.GetSession(w, r)
|
||||
userID, ok := session.Values["user_id"].(int)
|
||||
if !ok {
|
||||
helpers.RenderError(w, r, 403)
|
||||
return
|
||||
}
|
||||
|
||||
message, err := storage.GetMessageByID(db, userID, messageID)
|
||||
if err != nil {
|
||||
log.Printf("❌ Message not found: %v", err)
|
||||
message = nil
|
||||
} else if !message.IsRead {
|
||||
_ = storage.MarkMessageAsRead(db, messageID, userID)
|
||||
}
|
||||
|
||||
data := BuildTemplateData(db, w, r)
|
||||
context := helpers.TemplateContext(w, r, data)
|
||||
context["Message"] = message
|
||||
|
||||
tmpl := helpers.LoadTemplateFiles("read-message.html", "templates/account/messages/read.html")
|
||||
|
||||
tmpl.ExecuteTemplate(w, "layout", context)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user