Still working through messages and notifications.

This commit is contained in:
2025-10-30 17:22:52 +00:00
parent 8650b1fd63
commit 262536135d
18 changed files with 154 additions and 128 deletions

View File

@@ -7,8 +7,8 @@ import (
type Message = models.Message
type CreateMessageInput struct {
RecipientID int64 `form:"to" binding:"required,username"`
Subject string `form:"subject" binding:"required,max=200"`
RecipientID int64 `form:"to" binding:"required,numeric"`
Subject string `form:"recipient_id" binding:"required,max=200"`
Body string `form:"body" binding:"required"`
}

View File

@@ -7,10 +7,10 @@ package accountMessageHandler
import (
"net/http"
templateHandlers "synlotto-website/internal/handlers/template"
templateHelpers "synlotto-website/internal/helpers/template"
"synlotto-website/internal/logging"
"synlotto-website/internal/models"
"synlotto-website/internal/platform/bootstrap"
"github.com/gin-gonic/gin"
@@ -31,7 +31,9 @@ func (h *AccountMessageHandlers) ArchivedList(c *gin.Context) {
return
}
ctx := templateHelpers.TemplateContext(c.Writer, c.Request, models.TemplateData{})
data := templateHandlers.BuildTemplateData(app, c.Writer, c.Request)
ctx := templateHelpers.TemplateContext(c.Writer, c.Request, data)
if f := sm.PopString(c.Request.Context(), "flash"); f != "" {
ctx["Flash"] = f
}
@@ -39,10 +41,7 @@ func (h *AccountMessageHandlers) ArchivedList(c *gin.Context) {
ctx["Title"] = "Archived Messages"
ctx["Messages"] = msgs
tmpl := templateHelpers.LoadTemplateFiles(
"layout.html",
"web/templates/account/messages/archived.html",
)
tmpl := templateHelpers.LoadTemplateFiles("layout.html", "web/templates/account/messages/archived.html")
c.Status(http.StatusOK)
if err := tmpl.ExecuteTemplate(c.Writer, "layout", ctx); err != nil {

View File

@@ -7,10 +7,10 @@ package accountMessageHandler
import (
"net/http"
templateHandlers "synlotto-website/internal/handlers/template"
templateHelpers "synlotto-website/internal/helpers/template"
"synlotto-website/internal/logging"
"synlotto-website/internal/models"
"synlotto-website/internal/platform/bootstrap"
"github.com/gin-gonic/gin"
@@ -33,8 +33,8 @@ func (h *AccountMessageHandlers) List(c *gin.Context) {
return
}
// Build template context just like LoginGet
ctx := templateHelpers.TemplateContext(c.Writer, c.Request, models.TemplateData{})
data := templateHandlers.BuildTemplateData(app, c.Writer, c.Request)
ctx := templateHelpers.TemplateContext(c.Writer, c.Request, data)
if f := sm.PopString(c.Request.Context(), "flash"); f != "" {
ctx["Flash"] = f
@@ -73,7 +73,9 @@ func (h *AccountMessageHandlers) ReadGet(c *gin.Context) {
return
}
ctx := templateHelpers.TemplateContext(c.Writer, c.Request, models.TemplateData{})
data := templateHandlers.BuildTemplateData(app, c.Writer, c.Request)
ctx := templateHelpers.TemplateContext(c.Writer, c.Request, data)
if f := sm.PopString(c.Request.Context(), "flash"); f != "" {
ctx["Flash"] = f
}

View File

@@ -8,6 +8,7 @@ import (
"net/http"
domain "synlotto-website/internal/domain/messages"
templateHandlers "synlotto-website/internal/handlers/template"
templateHelpers "synlotto-website/internal/helpers/template"
"synlotto-website/internal/logging"
@@ -18,23 +19,22 @@ import (
"github.com/justinas/nosurf"
)
// GET /account/messages/add
// GET /account/messages/send
// Renders: web/templates/account/messages/send.html
func (h *AccountMessageHandlers) AddGet(c *gin.Context) {
func (h *AccountMessageHandlers) SendGet(c *gin.Context) {
app := c.MustGet("app").(*bootstrap.App)
sm := app.SessionManager
ctx := templateHelpers.TemplateContext(c.Writer, c.Request, models.TemplateData{})
data := templateHandlers.BuildTemplateData(app, c.Writer, c.Request)
ctx := templateHelpers.TemplateContext(c.Writer, c.Request, data)
if f := sm.PopString(c.Request.Context(), "flash"); f != "" {
ctx["Flash"] = f
}
ctx["CSRFToken"] = nosurf.Token(c.Request)
ctx["Title"] = "Send Message"
tmpl := templateHelpers.LoadTemplateFiles(
"layout.html",
"web/templates/account/messages/send.html",
)
tmpl := templateHelpers.LoadTemplateFiles("layout.html", "web/templates/account/messages/send.html")
c.Status(http.StatusOK)
if err := tmpl.ExecuteTemplate(c.Writer, "layout", ctx); err != nil {
@@ -43,8 +43,8 @@ func (h *AccountMessageHandlers) AddGet(c *gin.Context) {
}
}
// POST /account/messages/add
func (h *AccountMessageHandlers) AddPost(c *gin.Context) {
// POST /account/messages/send
func (h *AccountMessageHandlers) SendPost(c *gin.Context) {
app := c.MustGet("app").(*bootstrap.App)
sm := app.SessionManager

View File

@@ -38,8 +38,8 @@ func List(c *gin.Context) {
rows, err := app.DB.QueryContext(c.Request.Context(), `
SELECT id, numbers, game, price, purchased_at, created_at
FROM tickets
WHERE user_id = ?
FROM my_tickets
WHERE userId = ?
ORDER BY purchased_at DESC, id DESC
`, userID)
if err != nil {

View File

@@ -66,8 +66,8 @@ func RegisterAccountRoutes(app *bootstrap.App) {
messages.Use(middleware.AuthMiddleware(), middleware.RequireAuth())
{
messages.GET("/", msgH.List)
messages.GET("/add", msgH.AddGet)
messages.POST("/add", msgH.AddPost)
messages.GET("/send", msgH.SendGet)
messages.POST("/send", msgH.SendPost)
messages.GET("/archived", msgH.ArchivedList) // renders archived.html
messages.GET("/:id", msgH.ReadGet) // renders read.html
}

View File

@@ -0,0 +1,15 @@
package models
import "time"
type Message struct {
ID int
SenderId int
RecipientId int
Subject string
Body string
IsRead bool
IsArchived bool
CreatedAt time.Time
ArchivedAt *time.Time
}

View File

@@ -0,0 +1,12 @@
package models
import "time"
type Notification struct {
ID int
UserId int
Title string
Body string
IsRead bool
CreatedAt time.Time
}

View File

@@ -13,26 +13,3 @@ type User struct {
CreatedAt time.Time
UpdatedAt time.Time
}
// ToDo: should be in a notification model?
type Notification struct {
ID int
UserId int
Title string
Body string
IsRead bool
CreatedAt time.Time
}
// ToDo: should be in a message model?
type Message struct {
ID int
SenderId int
RecipientId int
Subject string
Body string
IsRead bool
IsArchived bool
CreatedAt time.Time
ArchivedAt *time.Time
}

View File

@@ -37,14 +37,15 @@ func New(db *sql.DB, opts ...func(*Service)) *Service {
// Ensure *Service satisfies the domain interface.
var _ domain.MessageService = (*Service)(nil)
// ToDo: Needs a userId on table or rename the recipiant id.. but then again dont want to expose userids to users for sending.
func (s *Service) ListInbox(userID int64) ([]domain.Message, error) {
ctx, cancel := context.WithTimeout(context.Background(), s.Timeout)
defer cancel()
q := `
SELECT id, from_email, to_email, subject, body, is_read, is_archived, created_at
SELECT id, senderId, recipientId, subject, message, is_read, is_archived, created_at
FROM users_messages
WHERE user_id = ? AND is_archived = FALSE
WHERE recipientId = ? AND is_archived = FALSE
ORDER BY created_at DESC`
q = s.bind(q)
@@ -57,7 +58,7 @@ func (s *Service) ListInbox(userID int64) ([]domain.Message, error) {
var out []domain.Message
for rows.Next() {
var m domain.Message
if err := rows.Scan(&m.ID, &m.From, &m.To, &m.Subject, &m.Body, &m.IsRead, &m.IsArchived, &m.CreatedAt); err != nil {
if err := rows.Scan(&m.ID, &m.SenderId, &m.RecipientId, &m.Subject, &m.Body, &m.IsRead, &m.IsArchived, &m.CreatedAt); err != nil {
return nil, err
}
out = append(out, m)
@@ -71,9 +72,9 @@ func (s *Service) ListArchived(userID int64) ([]domain.Message, error) {
defer cancel()
q := `
SELECT id, from_email, to_email, subject, body, is_read, is_archived, created_at
SELECT id, senderId, recipientId, subject, message, is_read, is_archived, created_at
FROM users_messages
WHERE user_id = ? AND is_archived = TRUE
WHERE recipientId = ? AND is_archived = TRUE
ORDER BY created_at DESC`
q = s.bind(q)
@@ -86,7 +87,7 @@ func (s *Service) ListArchived(userID int64) ([]domain.Message, error) {
var out []domain.Message
for rows.Next() {
var m domain.Message
if err := rows.Scan(&m.ID, &m.From, &m.To, &m.Subject, &m.Body, &m.IsRead, &m.IsArchived, &m.CreatedAt); err != nil {
if err := rows.Scan(&m.ID, &m.SenderId, &m.RecipientId, &m.Subject, &m.Body, &m.IsRead, &m.IsArchived, &m.CreatedAt); err != nil {
return nil, err
}
out = append(out, m)
@@ -99,14 +100,14 @@ func (s *Service) GetByID(userID, id int64) (*domain.Message, error) {
defer cancel()
q := `
SELECT id, from_email, to_email, subject, body, is_read, is_archived, created_at
SELECT id, senderId, recipientId, subject, message, is_read, is_archived, created_at
FROM users_messages
WHERE user_id = ? AND id = ?`
WHERE recipientId = ? AND id = ?`
q = s.bind(q)
var m domain.Message
err := s.DB.QueryRowContext(ctx, q, userID, id).
Scan(&m.ID, &m.From, &m.To, &m.Subject, &m.Body, &m.IsRead, &m.IsArchived, &m.CreatedAt)
Scan(&m.ID, &m.SenderId, &m.RecipientId, &m.Subject, &m.Body, &m.IsRead, &m.IsArchived, &m.CreatedAt)
if errors.Is(err, sql.ErrNoRows) {
return nil, nil
}
@@ -123,19 +124,19 @@ func (s *Service) Create(userID int64, in domain.CreateMessageInput) (int64, err
switch s.Dialect {
case "postgres":
const q = `
INSERT INTO messages (user_id, from_email, to_email, subject, body, is_read, is_archived, created_at)
INSERT INTO messages (id, senderId, recipientId, subject, message, is_read, is_archived, created_at)
VALUES ($1, $2, $3, $4, $5, FALSE, FALSE, NOW())
RETURNING id`
var id int64
if err := s.DB.QueryRowContext(ctx, q, userID, "", in.To, in.Subject, in.Body).Scan(&id); err != nil {
if err := s.DB.QueryRowContext(ctx, q, userID, "", in.RecipientID, in.Subject, in.Body).Scan(&id); err != nil {
return 0, err
}
return id, nil
default: // mysql/sqlite
const q = `
INSERT INTO messages (user_id, from_email, to_email, subject, body, is_read, is_archived, created_at)
INSERT INTO messages (id, senderId, recipientId, subject, message is_read, is_archived, created_at)
VALUES (?, ?, ?, ?, ?, FALSE, FALSE, CURRENT_TIMESTAMP)`
res, err := s.DB.ExecContext(ctx, q, userID, "", in.To, in.Subject, in.Body)
res, err := s.DB.ExecContext(ctx, q, userID, "", in.RecipientID, in.Subject, in.Body)
if err != nil {
return 0, err
}

View File

@@ -42,7 +42,7 @@ func (s *Service) List(userID int64) ([]domain.Notification, error) {
const q = `
SELECT id, title, body, is_read, created_at
FROM notifications
WHERE user_id = ?
WHERE userId = ?
ORDER BY created_at DESC`
rows, err := s.DB.QueryContext(ctx, q, userID)
@@ -69,7 +69,7 @@ func (s *Service) GetByID(userID, id int64) (*domain.Notification, error) {
const q = `
SELECT id, title, body, is_read, created_at
FROM notifications
WHERE user_id = ? AND id = ?`
WHERE userId = ? AND id = ?`
var n domain.Notification
err := s.DB.QueryRowContext(ctx, q, userID, id).

View File

@@ -36,7 +36,7 @@ func GetRecentMessages(db *sql.DB, userID int, limit int) []models.Message {
&m.SenderId,
&m.RecipientId,
&m.Subject,
&m.Message,
&m.Body,
&m.IsRead,
&m.CreatedAt,
)
@@ -55,7 +55,7 @@ func GetMessageByID(db *sql.DB, userID, messageID int) (*models.Message, error)
`, messageID, userID)
var m models.Message
err := row.Scan(&m.ID, &m.SenderId, &m.RecipientId, &m.Subject, &m.Message, &m.IsRead, &m.CreatedAt)
err := row.Scan(&m.ID, &m.SenderId, &m.RecipientId, &m.Subject, &m.Body, &m.IsRead, &m.CreatedAt)
if err != nil {
return nil, err
}
@@ -81,7 +81,7 @@ func GetArchivedMessages(db *sql.DB, userID int, page, perPage int) []models.Mes
var m models.Message
err := rows.Scan(
&m.ID, &m.SenderId, &m.RecipientId,
&m.Subject, &m.Message, &m.IsRead,
&m.Subject, &m.Body, &m.IsRead,
&m.CreatedAt, &m.ArchivedAt,
)
if err == nil {
@@ -110,7 +110,7 @@ func GetInboxMessages(db *sql.DB, userID int, page, perPage int) []models.Messag
var m models.Message
err := rows.Scan(
&m.ID, &m.SenderId, &m.RecipientId,
&m.Subject, &m.Message, &m.IsRead, &m.CreatedAt,
&m.Subject, &m.Body, &m.IsRead, &m.CreatedAt,
)
if err == nil {
messages = append(messages, m)

View File

@@ -16,7 +16,7 @@ func GetNotificationByID(db *sql.DB, userID, notificationID int) (*models.Notifi
`, notificationID, userID)
var n models.Notification
err := row.Scan(&n.ID, &n.UserId, &n.Subject, &n.Body, &n.IsRead)
err := row.Scan(&n.ID, &n.UserId, &n.Title, &n.Body, &n.IsRead)
if err != nil {
return nil, err
}
@@ -27,7 +27,7 @@ func GetNotificationCount(db *sql.DB, userID int) int {
var count int
err := db.QueryRow(`
SELECT COUNT(*) FROM users_notification
WHERE user_id = ? AND is_read = FALSE`, userID).Scan(&count)
WHERE userId = ? AND is_read = FALSE`, userID).Scan(&count)
if err != nil {
log.Println("⚠️ Failed to count notifications:", err)
@@ -41,7 +41,7 @@ func GetRecentNotifications(db *sql.DB, userID int, limit int) []models.Notifica
rows, err := db.Query(`
SELECT id, subject, body, is_read, created_at
FROM users_notification
WHERE user_id = ?
WHERE userId = ?
ORDER BY created_at DESC
LIMIT ?`, userID, limit)
if err != nil {
@@ -54,7 +54,7 @@ func GetRecentNotifications(db *sql.DB, userID int, limit int) []models.Notifica
for rows.Next() {
var n models.Notification
if err := rows.Scan(&n.ID, &n.Subject, &n.Body, &n.IsRead, &n.CreatedAt); err == nil {
if err := rows.Scan(&n.ID, &n.Title, &n.Body, &n.IsRead, &n.CreatedAt); err == nil {
notifications = append(notifications, n)
}
}

View File

@@ -7,20 +7,27 @@
<div class="card mb-3">
<div class="card-body">
<h5 class="card-title">{{ .Subject }}</h5>
<p class="card-text">{{ .Message }}</p>
<p class="card-text">{{ .Body }}</p>
<p class="card-text">
<small class="text-muted">Archived: {{ .ArchivedAt.Format "02 Jan 2006 15:04" }}</small>
<small class="text-muted">
Archived:
{{ if .ArchivedAt.Valid }}
{{ .ArchivedAt.Time.Format "02 Jan 2006 15:04" }}
{{ else }}
{{ end }}
</small>
</p>
<form method="POST" action="/account/messages/restore" class="m-0">
<input type="hidden" name="csrf_token" value="{{ $.CSRFToken }}">
<input type="hidden" name="id" value="{{ .ID }}">
<button type="submit" class="btn btn-sm btn-outline-success">Restore</button>
</form>
</div>
</div>
<form method="POST" action="/account/messages/restore" class="m-0">
{{ $.CSRFField }}
<input type="hidden" name="id" value="{{ .ID }}">
<button type="submit" class="btn btn-sm btn-outline-success">Restore</button>
</form>
{{ end }}
<!-- Pagination Controls -->
<!-- Pagination Controls (keep if your funcs exist) -->
<nav>
<ul class="pagination">
{{ if gt .Page 1 }}

View File

@@ -1,50 +1,48 @@
{{ define "content" }}
<!-- Todo lists messages but doesn't show which ones have been read and unread-->
<div class="container py-5">
<h2>Your Inbox</h2>
{{ if .Messages }}
<ul class="list-group mb-4">
{{ range .Messages }}
<li class="list-group-item d-flex justify-content-between align-items-center">
<div>
<a href="/account/messages/read?id={{ .ID }}" class="fw-bold text-dark">{{ .Subject }}</a>
<br>
<small class="text-muted">{{ .CreatedAt.Format "02 Jan 2006 15:04" }}</small>
</div>
<form method="POST" action="/account/messages/archive?id={{ .ID }}" class="m-0">
{{ $.CSRFField }}
<input type="hidden" name="id" value="{{ .ID }}">
<button type="submit" class="btn btn-sm btn-outline-secondary">Archive</button>
</form>
</li>
{{ end }}
</ul>
<!-- Pagination -->
<nav>
<ul class="pagination">
{{ if gt .CurrentPage 1 }}
<li class="page-item">
<a class="page-link" href="?page={{ sub .CurrentPage 1 }}">Previous</a>
</li>
{{ end }}
<ul class="list-group mb-4">
{{ range .Messages }}
<li class="list-group-item d-flex justify-content-between align-items-center">
<div>
<a href="/account/messages/{{ .ID }}" class="fw-bold text-dark">{{ .Subject }}</a><br>
<small class="text-muted">{{ .CreatedAt.Format "02 Jan 2006 15:04" }}</small>
</div>
<form method="POST" action="/account/messages/archive" class="m-0">
<input type="hidden" name="csrf_token" value="{{ $.CSRFToken }}">
<input type="hidden" name="id" value="{{ .ID }}">
<button type="submit" class="btn btn-sm btn-outline-secondary">Archive</button>
</form>
</li>
{{ end }}
</ul>
{{ range $i := .PageRange }}
<li class="page-item {{ if eq $i $.CurrentPage }}active{{ end }}">
<a class="page-link" href="?page={{ $i }}">{{ $i }}</a>
</li>
{{ end }}
{{ if lt .CurrentPage .TotalPages }}
<li class="page-item">
<a class="page-link" href="?page={{ add .CurrentPage 1 }}">Next</a>
</li>
{{ end }}
</ul>
</nav>
<!-- (Optional) Pagination if you have helpers wired -->
<nav>
<ul class="pagination">
{{ if gt .CurrentPage 1 }}
<li class="page-item">
<a class="page-link" href="?page={{ sub .CurrentPage 1 }}">Previous</a>
</li>
{{ end }}
{{ range $i := .PageRange }}
<li class="page-item {{ if eq $i $.CurrentPage }}active{{ end }}">
<a class="page-link" href="?page={{ $i }}">{{ $i }}</a>
</li>
{{ end }}
{{ if lt .CurrentPage .TotalPages }}
<li class="page-item">
<a class="page-link" href="?page={{ add .CurrentPage 1 }}">Next</a>
</li>
{{ end }}
</ul>
</nav>
{{ else }}
<div class="alert alert-info">No messages found.</div>
<div class="alert alert-info text-center">No messages found.</div>
{{ end }}
<div class="mt-3">

View File

@@ -4,8 +4,15 @@
<h2>{{ .Message.Subject }}</h2>
<p class="text-muted">Received: {{ .Message.CreatedAt.Format "02 Jan 2006 15:04" }}</p>
<hr>
<p>{{ .Message.Message }}</p>
<a href="/account/messages" class="btn btn-secondary mt-4">Back to Inbox</a> <a href="/account/messages/archive?id={{ .Message.ID }}" class="btn btn-outline-danger mt-3">Archive</a>
<p>{{ .Message.Body }}</p>
<form method="POST" action="/account/messages/archive" class="d-inline">
<input type="hidden" name="csrf_token" value="{{ $.CSRFToken }}">
<input type="hidden" name="id" value="{{ .Message.ID }}">
<button type="submit" class="btn btn-outline-danger mt-3">Archive</button>
</form>
<a href="/account/messages" class="btn btn-secondary mt-3">Back to Inbox</a>
{{ else }}
<div class="alert alert-danger text-center">
Message not found or access denied.

View File

@@ -1,24 +1,32 @@
{{ define "content" }}
<div class="container py-5">
<h2>Send a Message</h2>
{{ if .Flash }}
<div class="alert alert-info">{{ .Flash }}</div>
{{ end }}
{{ if .Error }}
<div class="alert alert-danger">{{ .Error }}</div>
{{ end }}
<form method="POST" action="/account/messages/send">
{{ .CSRFField }}
<input type="hidden" name="csrf_token" value="{{ .CSRFToken }}">
<div class="mb-3">
<label for="recipient_id" class="form-label">Recipient User ID</label>
<input type="number" class="form-control" name="recipient_id" required>
<input type="number" class="form-control" name="recipient_id" value="{{ with .Form }}{{ .RecipientID }}{{ end }}" required>
</div>
<div class="mb-3">
<label for="subject" class="form-label">Subject</label>
<input type="text" class="form-control" name="subject" required>
<input type="text" class="form-control" name="subject" value="{{ with .Form }}{{ .Subject }}{{ end }}" required>
</div>
<div class="mb-3">
<label for="message" class="form-label">Message</label>
<textarea class="form-control" name="message" rows="5" required></textarea>
<label for="body" class="form-label">Message</label>
<textarea class="form-control" name="body" rows="5" required>{{ with .Form }}{{ .Body }}{{ end }}</textarea>
</div>
<button type="submit" class="btn btn-primary">Send</button>
</form>

View File

@@ -13,7 +13,7 @@
<a href="/legal/terms">Terms & Conditions</a> |
<a href="/contact">Contact Us</a>
<br>
The content and operations of this website have not been approved or endorsed by {{ $lotteryOperator }} or the {{ $commisionName }}.
</small>
</footer>
{{ end }}