Go backend with domain-based packages (volunteer, schedule, timeoff, checkin, notification), SQLite storage, JWT auth, and chi router. React TypeScript frontend with routing, auth context, and pages for all core features. Multi-stage Dockerfile and docker-compose included. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
91 lines
2.2 KiB
Go
91 lines
2.2 KiB
Go
package notification
|
|
|
|
import (
|
|
"database/sql"
|
|
"errors"
|
|
"fmt"
|
|
"time"
|
|
)
|
|
|
|
type Notification struct {
|
|
ID int64 `json:"id"`
|
|
VolunteerID int64 `json:"volunteer_id"`
|
|
Message string `json:"message"`
|
|
Read bool `json:"read"`
|
|
CreatedAt time.Time `json:"created_at"`
|
|
}
|
|
|
|
type Store struct {
|
|
db *sql.DB
|
|
}
|
|
|
|
func NewStore(db *sql.DB) *Store {
|
|
return &Store{db: db}
|
|
}
|
|
|
|
func (s *Store) Create(volunteerID int64, message string) (*Notification, error) {
|
|
res, err := s.db.Exec(
|
|
`INSERT INTO notifications (volunteer_id, message) VALUES (?, ?)`,
|
|
volunteerID, message,
|
|
)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("insert notification: %w", err)
|
|
}
|
|
id, _ := res.LastInsertId()
|
|
return s.GetByID(id)
|
|
}
|
|
|
|
func (s *Store) GetByID(id int64) (*Notification, error) {
|
|
n := &Notification{}
|
|
var createdAt string
|
|
err := s.db.QueryRow(
|
|
`SELECT id, volunteer_id, message, read, created_at FROM notifications WHERE id = ?`, id,
|
|
).Scan(&n.ID, &n.VolunteerID, &n.Message, &n.Read, &createdAt)
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return nil, nil
|
|
}
|
|
if err != nil {
|
|
return nil, fmt.Errorf("get notification: %w", err)
|
|
}
|
|
n.CreatedAt, _ = time.Parse("2006-01-02 15:04:05", createdAt)
|
|
return n, nil
|
|
}
|
|
|
|
func (s *Store) ListForVolunteer(volunteerID int64) ([]Notification, error) {
|
|
rows, err := s.db.Query(
|
|
`SELECT id, volunteer_id, message, read, created_at FROM notifications WHERE volunteer_id = ? ORDER BY created_at DESC`,
|
|
volunteerID,
|
|
)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("list notifications: %w", err)
|
|
}
|
|
defer rows.Close()
|
|
|
|
var notifications []Notification
|
|
for rows.Next() {
|
|
var n Notification
|
|
var createdAt string
|
|
if err := rows.Scan(&n.ID, &n.VolunteerID, &n.Message, &n.Read, &createdAt); err != nil {
|
|
return nil, err
|
|
}
|
|
n.CreatedAt, _ = time.Parse("2006-01-02 15:04:05", createdAt)
|
|
notifications = append(notifications, n)
|
|
}
|
|
return notifications, rows.Err()
|
|
}
|
|
|
|
func (s *Store) MarkRead(id, volunteerID int64) (*Notification, error) {
|
|
result, err := s.db.Exec(
|
|
`UPDATE notifications SET read = 1 WHERE id = ? AND volunteer_id = ?`,
|
|
id, volunteerID,
|
|
)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("mark read: %w", err)
|
|
}
|
|
rows, _ := result.RowsAffected()
|
|
if rows == 0 {
|
|
return nil, nil
|
|
}
|
|
return s.GetByID(id)
|
|
}
|