Skip to content

Commit

Permalink
RepoRepo: add update functions to the DB
Browse files Browse the repository at this point in the history
  • Loading branch information
bartekpacia committed Oct 14, 2024
1 parent 6f08329 commit 0cc9516
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions backend/internal/data/repo_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type Repo struct {
UserID int64 `db:"user_id"`
LatestCommitSHA string `db:"latest_commit_sha"`
LatestCommitPushedAt time.Time `db:"latest_commit_pushed_at"`
Description string `db:"description"`
}

func (r Repo) LogValue() slog.Value {
Expand All @@ -34,6 +35,10 @@ type RepoRepo interface {
//
// If searchRepo is empty, all repositories are considered.
GetAll(ctx context.Context, searchRepo string, userID int64) (repos []Repo, err error)

UpdateLatestCommit(id int64, sha string, pushedAt time.Time) (err error)

UpdateDescription(id int64, newDescription string) (err error)
}

type PostgresRepoRepo struct {
Expand Down Expand Up @@ -111,6 +116,41 @@ func (p PostgresRepoRepo) GetAll(ctx context.Context, searchRepo string, userID
return repos, nil
}

func (p PostgresRepoRepo) UpdateLatestCommit(id int64, sha string, pushedAt time.Time) (err error) {
_, err = p.db.NamedExecContext(
context.Background(),
`UPDATE bee_schema.repos
SET latest_commit_sha = :latest_commit, latest_commit_pushed_at = :latest_commit_pushed_at
WHERE id = :id`,
map[string]interface{}{
"id": id,
"latest_commit": sha,
"latest_commit_pushed_at": pushedAt,
})
if err != nil {
return fmt.Errorf("executing UPDATE query: %v", err)
}

return nil
}

func (p PostgresRepoRepo) UpdateDescription(id int64, newDescription string) (err error) {
_, err = p.db.NamedExecContext(
context.Background(),
`UPDATE bee_schema.repos
SET description = :description
WHERE id = :id`,
map[string]interface{}{
"id": id,
"description": newDescription,
})
if err != nil {
return fmt.Errorf("executing UPDATE query: %v", err)
}

return nil
}

var _ RepoRepo = &PostgresRepoRepo{}

func NewPostgresRepoRepo(db *sqlx.DB) *PostgresRepoRepo {
Expand Down

0 comments on commit 0cc9516

Please sign in to comment.