package store import ( "context" "atlas9.dev/c/core" "atlas9.dev/c/core/dbi" "atlas9.dev/c/demo/bots" "atlas9.dev/c/demo/lib" ) type SqliteBotStore struct { db dbi.DBI guard lib.Guard } var _ bots.BotStore = (*SqliteBotStore)(nil) func NewSqliteBotStore(db dbi.DBI, guard lib.Guard) *SqliteBotStore { return &SqliteBotStore{db: db, guard: guard} } func (s *SqliteBotStore) Get(ctx context.Context, tenant core.ID, id core.ID, out *bots.Bot) error { if err := s.guard.Check(ctx, bots.Cap_Bots_Read, tenant, ""); err != nil { return err } return dbi.Get(ctx, s.db, out, `SELECT id, tenant, name FROM bots WHERE id = $1`, id) } func (s *SqliteBotStore) List(ctx context.Context, tenant core.ID, page core.PageReq, out *core.Page[bots.Bot]) error { if err := s.guard.Check(ctx, bots.Cap_Bots_Read, tenant, ""); err != nil { return err } return dbi.Paginate(ctx, s.db, page, out, func(b bots.Bot) string { return b.ID.String() }, `SELECT id, tenant, name FROM bots WHERE tenant = $1 AND id > $cursor ORDER BY id LIMIT $limit`, tenant) } func (s *SqliteBotStore) Save(ctx context.Context, bot *bots.Bot) error { if err := s.guard.Check(ctx, bots.Cap_Bots_Write, bot.Tenant, ""); err != nil { return err } _, err := s.db.Exec(ctx, ` INSERT INTO bots (id, tenant, name) VALUES ($1, $2, $3) ON CONFLICT (id) DO UPDATE SET name = $3 `, bot.ID, bot.Tenant, bot.Name) return err } func (s *SqliteBotStore) Delete(ctx context.Context, tenant core.ID, id core.ID) error { if err := s.guard.Check(ctx, bots.Cap_Bots_Write, tenant, ""); err != nil { return err } _, err := s.db.Exec(ctx, `DELETE FROM bots WHERE id = $1`, id) return err }