package store import ( "context" "time" "github.com/google/uuid" "github.com/jmoiron/sqlx" "git.hangman-lab.top/hzhang/Dialectic.Backend/internal/models" ) type Camp struct { ID string `db:"id" json:"id"` TopicID string `db:"topic_id" json:"topic_id"` Camp models.Camp `db:"camp" json:"camp"` AgentID string `db:"agent_id" json:"agent_id"` AllocatedAt time.Time `db:"allocated_at" json:"allocated_at"` } type CampStore struct { db *sqlx.DB } func NewCampStore(db *sqlx.DB) *CampStore { return &CampStore{db: db} } // WriteAllocation inserts all 3 camp rows for a topic atomically. Must // be called within a tx the orchestrator owns (so signup_close transition // + camps insert + status update are all-or-nothing). Receives an open // *sqlx.Tx, returns nothing on success. func (s *CampStore) WriteAllocation(ctx context.Context, tx *sqlx.Tx, topicID string, alloc map[models.Camp]string) error { for _, c := range models.AllCamps { agentID, ok := alloc[c] if !ok { continue } if _, err := tx.ExecContext(ctx, `INSERT INTO camps (id, topic_id, camp, agent_id) VALUES (?, ?, ?, ?)`, uuid.NewString(), topicID, c, agentID); err != nil { return err } } return nil } func (s *CampStore) ListByTopic(ctx context.Context, topicID string) ([]Camp, error) { var rows []Camp if err := s.db.SelectContext(ctx, &rows, `SELECT * FROM camps WHERE topic_id = ? ORDER BY allocated_at ASC`, topicID); err != nil { return nil, err } return rows, nil } // AgentCampInTopic returns the camp `agentID` was allocated to, or empty // if the agent isn't in any camp on this topic. Used by argument/verdict // handlers to enforce "only camp members can post". func (s *CampStore) AgentCampInTopic(ctx context.Context, topicID, agentID string) (models.Camp, error) { var camp models.Camp err := s.db.GetContext(ctx, &camp, `SELECT camp FROM camps WHERE topic_id = ? AND agent_id = ? LIMIT 1`, topicID, agentID) if err != nil { return "", err } return camp, nil }