Changes to pagination and fixing archive messages in progress

This commit is contained in:
2025-10-31 22:55:04 +00:00
parent 8529116ad2
commit 9dc01f925a
9 changed files with 219 additions and 41 deletions

View File

@@ -42,7 +42,6 @@ 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()
@@ -164,7 +163,6 @@ func (s *Service) Create(senderID int64, in domain.CreateMessageInput) (int64, e
return id, nil
}
// compactSQL removes newlines/extra spaces for cleaner logs
func compactSQL(s string) string {
out := make([]rune, 0, len(s))
space := false
@@ -200,7 +198,28 @@ func (s *Service) bind(q string) string {
return string(out)
}
// ToDo: helper dont think it should be here.
func (s *Service) Archive(userID, id int64) error {
ctx, cancel := context.WithTimeout(context.Background(), s.Timeout)
defer cancel()
q := `
UPDATE user_messages
SET is_archived = 1, archived_at = CURRENT_TIMESTAMP
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
}
func intToStr(n int) string {
if n == 0 {
return "0"