Add in mark as reaad button to list view, use ajax to preform the action without page refresh.
This commit is contained in:
@@ -19,8 +19,7 @@ type MessageService interface {
|
||||
GetByID(userID, id int64) (*Message, error)
|
||||
Create(userID int64, in CreateMessageInput) (int64, error)
|
||||
Archive(userID, id int64) error
|
||||
//Restore()
|
||||
//ToDo: implement
|
||||
Unarchive(userID, id int64) error
|
||||
//MarkRead(userID, id int64) error
|
||||
MarkRead(userID, id int64) error
|
||||
//MarkUnread(userID, id int64) error
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ package accountMessageHandler
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"database/sql"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
@@ -137,3 +138,36 @@ func (h *AccountMessageHandlers) ReadGet(c *gin.Context) {
|
||||
}
|
||||
c.Data(http.StatusOK, "text/html; charset=utf-8", buf.Bytes())
|
||||
}
|
||||
|
||||
func (h *AccountMessageHandlers) MarkReadPost(c *gin.Context) {
|
||||
app := c.MustGet("app").(*bootstrap.App)
|
||||
sm := app.SessionManager
|
||||
userID := mustUserID(c)
|
||||
|
||||
idStr := c.PostForm("id")
|
||||
id, err := strconv.ParseInt(idStr, 10, 64)
|
||||
if err != nil || id <= 0 {
|
||||
sm.Put(c.Request.Context(), "flash", "Invalid message id.")
|
||||
c.Redirect(http.StatusSeeOther, c.Request.Referer()) // back to where they came from
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.Svc.MarkRead(userID, id); err != nil {
|
||||
logging.Info("❌ MarkRead error: %v", err)
|
||||
if err == sql.ErrNoRows {
|
||||
sm.Put(c.Request.Context(), "flash", "Message not found or not permitted.")
|
||||
} else {
|
||||
sm.Put(c.Request.Context(), "flash", "Could not mark message as read.")
|
||||
}
|
||||
c.Redirect(http.StatusSeeOther, "/account/messages")
|
||||
return
|
||||
}
|
||||
|
||||
sm.Put(c.Request.Context(), "flash", "Message marked as read.")
|
||||
// Redirect back to referer when possible so UX is smooth.
|
||||
if ref := c.Request.Referer(); ref != "" {
|
||||
c.Redirect(http.StatusSeeOther, ref)
|
||||
} else {
|
||||
c.Redirect(http.StatusSeeOther, "/account/messages")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -72,6 +72,7 @@ func RegisterAccountRoutes(app *bootstrap.App) {
|
||||
messages.GET("/archive", msgH.ArchivedList) // view archived messages
|
||||
messages.POST("/archive", msgH.ArchivePost) // archive a message
|
||||
messages.POST("/restore", msgH.RestoreArchived)
|
||||
messages.POST("/mark-read", msgH.MarkReadPost)
|
||||
}
|
||||
|
||||
// Notifications (auth-required)
|
||||
|
||||
@@ -277,3 +277,25 @@ func (s *Service) Unarchive(userID, id int64) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Service) MarkRead(userID, id int64) error {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), s.Timeout)
|
||||
defer cancel()
|
||||
|
||||
q := `
|
||||
UPDATE user_messages
|
||||
SET is_read = 1
|
||||
WHERE id = ? AND recipientId = ?
|
||||
`
|
||||
q = s.bind(q)
|
||||
|
||||
res, err := s.DB.ExecContext(ctx, q, id, userID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
n, _ := res.RowsAffected()
|
||||
if n == 0 {
|
||||
return sql.ErrNoRows
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user