Messages now sending/loading and populating on message dropdown

This commit is contained in:
2025-10-31 12:08:38 +00:00
parent 776ea53a66
commit 8529116ad2
13 changed files with 136 additions and 81 deletions

View File

@@ -8,9 +8,14 @@ import (
"context"
"database/sql"
"errors"
"fmt"
"time"
"synlotto-website/internal/logging"
domain "synlotto-website/internal/domain/messages"
"github.com/go-sql-driver/mysql"
)
// Service implements domain.Service.
@@ -43,8 +48,8 @@ func (s *Service) ListInbox(userID int64) ([]domain.Message, error) {
defer cancel()
q := `
SELECT id, senderId, recipientId, subject, message, is_read, is_archived, created_at
FROM users_messages
SELECT id, senderId, recipientId, subject, body, is_read, is_archived, created_at
FROM user_messages
WHERE recipientId = ? AND is_archived = FALSE
ORDER BY created_at DESC`
q = s.bind(q)
@@ -72,8 +77,8 @@ func (s *Service) ListArchived(userID int64) ([]domain.Message, error) {
defer cancel()
q := `
SELECT id, senderId, recipientId, subject, message, is_read, is_archived, created_at
FROM users_messages
SELECT id, senderId, recipientId, subject, body, is_read, is_archived, created_at
FROM user_messages
WHERE recipientId = ? AND is_archived = TRUE
ORDER BY created_at DESC`
q = s.bind(q)
@@ -100,8 +105,8 @@ func (s *Service) GetByID(userID, id int64) (*domain.Message, error) {
defer cancel()
q := `
SELECT id, senderId, recipientId, subject, message, is_read, is_archived, created_at
FROM users_messages
SELECT id, senderId, recipientId, subject, body, is_read, is_archived, created_at
FROM user_messages
WHERE recipientId = ? AND id = ?`
q = s.bind(q)
@@ -117,34 +122,66 @@ func (s *Service) GetByID(userID, id int64) (*domain.Message, error) {
return &m, nil
}
func (s *Service) Create(userID int64, in domain.CreateMessageInput) (int64, error) {
func (s *Service) Create(senderID int64, in domain.CreateMessageInput) (int64, error) {
ctx, cancel := context.WithTimeout(context.Background(), s.Timeout)
defer cancel()
switch s.Dialect {
case "postgres":
const q = `
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.RecipientID, in.Subject, in.Body).Scan(&id); err != nil {
return 0, err
// ✅ make sure this matches your current table/column names
const q = `
INSERT INTO user_messages
(senderId, recipientId, subject, body, is_read, is_archived, created_at)
VALUES
(?, ?, ?, ?, 0, 0, CURRENT_TIMESTAMP)
`
// 👀 Log the SQL and arguments (truncate body in logs if you prefer)
logging.Info("🧪 SQL Exec: %s | args: senderId=%d recipientId=%d subject=%q body_len=%d", compactSQL(q), senderID, in.RecipientID, in.Subject, len(in.Body))
res, err := s.DB.ExecContext(ctx, q, senderID, in.RecipientID, in.Subject, in.Body)
if err != nil {
// Surface MySQL code/message (very helpful for FK #1452 etc.)
var me *mysql.MySQLError
if errors.As(err, &me) {
wrapped := fmt.Errorf("insert user_messages: mysql #%d %s | args(senderId=%d, recipientId=%d, subject=%q, body_len=%d)",
me.Number, me.Message, senderID, in.RecipientID, in.Subject, len(in.Body))
logging.Info("❌ %v", wrapped)
return 0, wrapped
}
return id, nil
default: // mysql/sqlite
const q = `
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.RecipientID, in.Subject, in.Body)
if err != nil {
return 0, err
}
return res.LastInsertId()
wrapped := fmt.Errorf("insert user_messages: %w | args(senderId=%d, recipientId=%d, subject=%q, body_len=%d)",
err, senderID, in.RecipientID, in.Subject, len(in.Body))
logging.Info("❌ %v", wrapped)
return 0, wrapped
}
id, err := res.LastInsertId()
if err != nil {
wrapped := fmt.Errorf("lastInsertId user_messages: %w", err)
logging.Info("❌ %v", wrapped)
return 0, wrapped
}
logging.Info("✅ Inserted message id=%d", id)
return id, nil
}
// compactSQL removes newlines/extra spaces for cleaner logs
func compactSQL(s string) string {
out := make([]rune, 0, len(s))
space := false
for _, r := range s {
if r == '\n' || r == '\t' || r == '\r' || r == ' ' {
if !space {
out = append(out, ' ')
space = true
}
continue
}
space = false
out = append(out, r)
}
return string(out)
}
// bind replaces '?' with '$1..' only for Postgres. For MySQL/SQLite it returns q unchanged.
func (s *Service) bind(q string) string {
if s.Dialect != "postgres" {
return q