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
55 lines
1.3 KiB
Go
55 lines
1.3 KiB
Go
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)
|
|
}
|
|
}
|