28 lines
1.2 KiB
Go
28 lines
1.2 KiB
Go
package postgres
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
commitcontract "git.cin.su/m/Assistant/contracts/commit"
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
)
|
|
|
|
type CommitRepository struct{ pool *pgxpool.Pool }
|
|
|
|
func OpenCommit(ctx context.Context, c ContinuityConfig) (*CommitRepository, error) {
|
|
p, e := pgxpool.New(ctx, fmt.Sprintf("host=%s port=%s dbname=%s user=%s password=%s", c.Host, c.Port, c.Database, c.User, c.Password))
|
|
if e != nil {
|
|
return nil, e
|
|
}
|
|
return &CommitRepository{p}, nil
|
|
}
|
|
func (r *CommitRepository) Close() { r.pool.Close() }
|
|
func (r *CommitRepository) SaveAuthorized(ctx context.Context, a commitcontract.AuthorizedAction) error {
|
|
_, e := r.pool.Exec(ctx, `INSERT INTO commit.authorized_actions (authorized_action_id,action_intent_id,state,revision) VALUES ($1,$2,$3,1) ON CONFLICT (authorized_action_id) DO NOTHING`, a.ID, a.Proposal.IntentID, a.State)
|
|
return e
|
|
}
|
|
func (r *CommitRepository) SaveAttempt(ctx context.Context, a commitcontract.ActionAttempt) error {
|
|
_, e := r.pool.Exec(ctx, `INSERT INTO commit.action_attempts (action_attempt_id,authorized_action_id,action_intent_id,state) VALUES ($1,$2,$3,$4) ON CONFLICT (action_attempt_id) DO NOTHING`, a.ID, a.AuthorizedID, a.IntentID, a.State)
|
|
return e
|
|
}
|