Scaffold full-stack volunteer scheduling application
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>
This commit is contained in:
148
internal/checkin/checkin.go
Normal file
148
internal/checkin/checkin.go
Normal file
@@ -0,0 +1,148 @@
|
||||
package checkin
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
type CheckIn struct {
|
||||
ID int64 `json:"id"`
|
||||
VolunteerID int64 `json:"volunteer_id"`
|
||||
ScheduleID *int64 `json:"schedule_id,omitempty"`
|
||||
CheckedInAt time.Time `json:"checked_in_at"`
|
||||
CheckedOutAt *time.Time `json:"checked_out_at,omitempty"`
|
||||
Notes string `json:"notes,omitempty"`
|
||||
}
|
||||
|
||||
type CheckInInput struct {
|
||||
ScheduleID *int64 `json:"schedule_id"`
|
||||
Notes string `json:"notes"`
|
||||
}
|
||||
|
||||
type CheckOutInput struct {
|
||||
Notes string `json:"notes"`
|
||||
}
|
||||
|
||||
type Store struct {
|
||||
db *sql.DB
|
||||
}
|
||||
|
||||
func NewStore(db *sql.DB) *Store {
|
||||
return &Store{db: db}
|
||||
}
|
||||
|
||||
func (s *Store) CheckIn(volunteerID int64, in CheckInInput) (*CheckIn, error) {
|
||||
// Ensure no active check-in exists
|
||||
var count int
|
||||
s.db.QueryRow(
|
||||
`SELECT COUNT(*) FROM checkins WHERE volunteer_id = ? AND checked_out_at IS NULL`, volunteerID,
|
||||
).Scan(&count)
|
||||
if count > 0 {
|
||||
return nil, fmt.Errorf("already checked in")
|
||||
}
|
||||
|
||||
res, err := s.db.Exec(
|
||||
`INSERT INTO checkins (volunteer_id, schedule_id, notes) VALUES (?, ?, ?)`,
|
||||
volunteerID, in.ScheduleID, in.Notes,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("insert checkin: %w", err)
|
||||
}
|
||||
id, _ := res.LastInsertId()
|
||||
return s.GetByID(id)
|
||||
}
|
||||
|
||||
func (s *Store) CheckOut(volunteerID int64, in CheckOutInput) (*CheckIn, error) {
|
||||
var id int64
|
||||
err := s.db.QueryRow(
|
||||
`SELECT id FROM checkins WHERE volunteer_id = ? AND checked_out_at IS NULL ORDER BY checked_in_at DESC LIMIT 1`,
|
||||
volunteerID,
|
||||
).Scan(&id)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return nil, fmt.Errorf("not checked in")
|
||||
}
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("find active checkin: %w", err)
|
||||
}
|
||||
_, err = s.db.Exec(
|
||||
`UPDATE checkins SET checked_out_at=datetime('now'), notes=COALESCE(NULLIF(?, ''), notes) WHERE id=?`,
|
||||
in.Notes, id,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("checkout: %w", err)
|
||||
}
|
||||
return s.GetByID(id)
|
||||
}
|
||||
|
||||
func (s *Store) GetByID(id int64) (*CheckIn, error) {
|
||||
ci := &CheckIn{}
|
||||
var checkedInAt string
|
||||
var checkedOutAt sql.NullString
|
||||
var scheduleID sql.NullInt64
|
||||
var notes sql.NullString
|
||||
|
||||
err := s.db.QueryRow(
|
||||
`SELECT id, volunteer_id, schedule_id, checked_in_at, checked_out_at, notes FROM checkins WHERE id = ?`, id,
|
||||
).Scan(&ci.ID, &ci.VolunteerID, &scheduleID, &checkedInAt, &checkedOutAt, ¬es)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return nil, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("get checkin: %w", err)
|
||||
}
|
||||
ci.CheckedInAt, _ = time.Parse("2006-01-02 15:04:05", checkedInAt)
|
||||
if checkedOutAt.Valid {
|
||||
t, _ := time.Parse("2006-01-02 15:04:05", checkedOutAt.String)
|
||||
ci.CheckedOutAt = &t
|
||||
}
|
||||
if scheduleID.Valid {
|
||||
ci.ScheduleID = &scheduleID.Int64
|
||||
}
|
||||
if notes.Valid {
|
||||
ci.Notes = notes.String
|
||||
}
|
||||
return ci, nil
|
||||
}
|
||||
|
||||
func (s *Store) History(volunteerID int64) ([]CheckIn, error) {
|
||||
query := `SELECT id, volunteer_id, schedule_id, checked_in_at, checked_out_at, notes FROM checkins`
|
||||
args := []any{}
|
||||
if volunteerID > 0 {
|
||||
query += ` WHERE volunteer_id = ?`
|
||||
args = append(args, volunteerID)
|
||||
}
|
||||
query += ` ORDER BY checked_in_at DESC`
|
||||
|
||||
rows, err := s.db.Query(query, args...)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("list checkins: %w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var checkins []CheckIn
|
||||
for rows.Next() {
|
||||
var ci CheckIn
|
||||
var checkedInAt string
|
||||
var checkedOutAt sql.NullString
|
||||
var scheduleID sql.NullInt64
|
||||
var notes sql.NullString
|
||||
if err := rows.Scan(&ci.ID, &ci.VolunteerID, &scheduleID, &checkedInAt, &checkedOutAt, ¬es); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ci.CheckedInAt, _ = time.Parse("2006-01-02 15:04:05", checkedInAt)
|
||||
if checkedOutAt.Valid {
|
||||
t, _ := time.Parse("2006-01-02 15:04:05", checkedOutAt.String)
|
||||
ci.CheckedOutAt = &t
|
||||
}
|
||||
if scheduleID.Valid {
|
||||
ci.ScheduleID = &scheduleID.Int64
|
||||
}
|
||||
if notes.Valid {
|
||||
ci.Notes = notes.String
|
||||
}
|
||||
checkins = append(checkins, ci)
|
||||
}
|
||||
return checkins, rows.Err()
|
||||
}
|
||||
64
internal/checkin/handler.go
Normal file
64
internal/checkin/handler.go
Normal file
@@ -0,0 +1,64 @@
|
||||
package checkin
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"git.unsupervised.ca/walkies/internal/respond"
|
||||
"git.unsupervised.ca/walkies/internal/server/middleware"
|
||||
)
|
||||
|
||||
type Handler struct {
|
||||
store *Store
|
||||
}
|
||||
|
||||
func NewHandler(store *Store) *Handler {
|
||||
return &Handler{store: store}
|
||||
}
|
||||
|
||||
// POST /api/v1/checkin
|
||||
func (h *Handler) CheckIn(w http.ResponseWriter, r *http.Request) {
|
||||
claims := middleware.ClaimsFromContext(r.Context())
|
||||
var in CheckInInput
|
||||
if err := json.NewDecoder(r.Body).Decode(&in); err != nil {
|
||||
respond.Error(w, http.StatusBadRequest, "invalid request body")
|
||||
return
|
||||
}
|
||||
ci, err := h.store.CheckIn(claims.VolunteerID, in)
|
||||
if err != nil {
|
||||
respond.Error(w, http.StatusConflict, err.Error())
|
||||
return
|
||||
}
|
||||
respond.JSON(w, http.StatusCreated, ci)
|
||||
}
|
||||
|
||||
// POST /api/v1/checkout
|
||||
func (h *Handler) CheckOut(w http.ResponseWriter, r *http.Request) {
|
||||
claims := middleware.ClaimsFromContext(r.Context())
|
||||
var in CheckOutInput
|
||||
json.NewDecoder(r.Body).Decode(&in)
|
||||
ci, err := h.store.CheckOut(claims.VolunteerID, in)
|
||||
if err != nil {
|
||||
respond.Error(w, http.StatusConflict, err.Error())
|
||||
return
|
||||
}
|
||||
respond.JSON(w, http.StatusOK, ci)
|
||||
}
|
||||
|
||||
// GET /api/v1/checkin/history
|
||||
func (h *Handler) History(w http.ResponseWriter, r *http.Request) {
|
||||
claims := middleware.ClaimsFromContext(r.Context())
|
||||
volunteerID := int64(0)
|
||||
if claims.Role != "admin" {
|
||||
volunteerID = claims.VolunteerID
|
||||
}
|
||||
history, err := h.store.History(volunteerID)
|
||||
if err != nil {
|
||||
respond.Error(w, http.StatusInternalServerError, "could not get history")
|
||||
return
|
||||
}
|
||||
if history == nil {
|
||||
history = []CheckIn{}
|
||||
}
|
||||
respond.JSON(w, http.StatusOK, history)
|
||||
}
|
||||
Reference in New Issue
Block a user