Fix model issues.

This commit is contained in:
2025-10-30 22:19:48 +00:00
parent 262536135d
commit a7a5169c67
5 changed files with 11 additions and 11 deletions

View File

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

View File

@@ -53,7 +53,8 @@ func (h *AccountMessageHandlers) SendPost(c *gin.Context) {
var in domain.CreateMessageInput var in domain.CreateMessageInput
if err := c.ShouldBind(&in); err != nil { if err := c.ShouldBind(&in); err != nil {
// Re-render form with validation errors // Re-render form with validation errors
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 != "" { if f := sm.PopString(c.Request.Context(), "flash"); f != "" {
ctx["Flash"] = f ctx["Flash"] = f
} }
@@ -98,8 +99,6 @@ func (h *AccountMessageHandlers) SendPost(c *gin.Context) {
return return
} }
// Optional: set a flash message for success (since you already PopString elsewhere)
// If you're using scs/v2, Put is available:
sm.Put(c.Request.Context(), "flash", "Message sent!") sm.Put(c.Request.Context(), "flash", "Message sent!")
// Redirect back to inbox // Redirect back to inbox

View File

@@ -35,14 +35,15 @@ func New(db *sql.DB, opts ...func(*Service)) *Service {
func WithTimeout(d time.Duration) func(*Service) { return func(s *Service) { s.Timeout = d } } func WithTimeout(d time.Duration) func(*Service) { return func(s *Service) { s.Timeout = d } }
// List returns newest-first notifications for a user. // List returns newest-first notifications for a user.
// ToDo:table is users_notification, where as messages is plural, this table seems oto use user_id reather than userId need to unify. Do i want to prefix with users/user
func (s *Service) List(userID int64) ([]domain.Notification, error) { func (s *Service) List(userID int64) ([]domain.Notification, error) {
ctx, cancel := context.WithTimeout(context.Background(), s.Timeout) ctx, cancel := context.WithTimeout(context.Background(), s.Timeout)
defer cancel() defer cancel()
const q = ` const q = `
SELECT id, title, body, is_read, created_at SELECT id, title, body, is_read, created_at
FROM notifications FROM users_notification
WHERE userId = ? WHERE user_Id = ?
ORDER BY created_at DESC` ORDER BY created_at DESC`
rows, err := s.DB.QueryContext(ctx, q, userID) rows, err := s.DB.QueryContext(ctx, q, userID)

View File

@@ -27,7 +27,7 @@ func GetNotificationCount(db *sql.DB, userID int) int {
var count int var count int
err := db.QueryRow(` err := db.QueryRow(`
SELECT COUNT(*) FROM users_notification SELECT COUNT(*) FROM users_notification
WHERE userId = ? AND is_read = FALSE`, userID).Scan(&count) WHERE user_Id = ? AND is_read = FALSE`, userID).Scan(&count)
if err != nil { if err != nil {
log.Println("⚠️ Failed to count notifications:", err) 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(` rows, err := db.Query(`
SELECT id, subject, body, is_read, created_at SELECT id, subject, body, is_read, created_at
FROM users_notification FROM users_notification
WHERE userId = ? WHERE user_Id = ?
ORDER BY created_at DESC ORDER BY created_at DESC
LIMIT ?`, userID, limit) LIMIT ?`, userID, limit)
if err != nil { if err != nil {

View File

@@ -13,8 +13,8 @@
<input type="hidden" name="csrf_token" value="{{ .CSRFToken }}"> <input type="hidden" name="csrf_token" value="{{ .CSRFToken }}">
<div class="mb-3"> <div class="mb-3">
<label for="recipient_id" class="form-label">Recipient User ID</label> <label for="recipientId" class="form-label">Recipient User ID</label>
<input type="number" class="form-control" name="recipient_id" value="{{ with .Form }}{{ .RecipientID }}{{ end }}" required> <input type="number" class="form-control" name="recipientId" value="{{ with .Form }}{{ .RecipientID }}{{ end }}" required>
</div> </div>
<div class="mb-3"> <div class="mb-3">