Merged in feature/document (pull request #36)

Document CRUD

* doccreate

* dochashlocandget

* depsandtodo
This commit is contained in:
Michael McGuinness
2025-01-24 16:12:25 +00:00
parent 4ec1d51a12
commit b16ff55afa
31 changed files with 381 additions and 82 deletions
+18 -7
View File
@@ -12,29 +12,40 @@ import (
)
const createDocument = `-- name: CreateDocument :one
INSERT INTO documents (jobId) VALUES ($1) RETURNING id
INSERT INTO documents (jobId, hash, location) VALUES ($1, $2, $3) RETURNING id
`
type CreateDocumentParams struct {
Jobid pgtype.UUID `db:"jobid"`
Hash string `db:"hash"`
Location string `db:"location"`
}
// CreateDocument
//
// INSERT INTO documents (jobId) VALUES ($1) RETURNING id
func (q *Queries) CreateDocument(ctx context.Context, jobid pgtype.UUID) (pgtype.UUID, error) {
row := q.db.QueryRow(ctx, createDocument, jobid)
// INSERT INTO documents (jobId, hash, location) VALUES ($1, $2, $3) RETURNING id
func (q *Queries) CreateDocument(ctx context.Context, arg *CreateDocumentParams) (pgtype.UUID, error) {
row := q.db.QueryRow(ctx, createDocument, arg.Jobid, arg.Hash, arg.Location)
var id pgtype.UUID
err := row.Scan(&id)
return id, err
}
const getDocument = `-- name: GetDocument :one
SELECT id, jobId FROM documents WHERE id = $1 LIMIT 1
SELECT id, jobId, hash, location FROM documents WHERE id = $1 LIMIT 1
`
// GetDocument
//
// SELECT id, jobId FROM documents WHERE id = $1 LIMIT 1
// SELECT id, jobId, hash, location FROM documents WHERE id = $1 LIMIT 1
func (q *Queries) GetDocument(ctx context.Context, id pgtype.UUID) (*Document, error) {
row := q.db.QueryRow(ctx, getDocument, id)
var i Document
err := row.Scan(&i.ID, &i.Jobid)
err := row.Scan(
&i.ID,
&i.Jobid,
&i.Hash,
&i.Location,
)
return &i, err
}
@@ -27,7 +27,11 @@ func TestDocument(t *testing.T) {
jobId, err := queries.CreateJob(ctx, clientId)
assert.Nil(t, err)
id, err := queries.CreateDocument(ctx, jobId)
hash := "example_hash"
id, err := queries.CreateDocument(ctx, &repository.CreateDocumentParams{
Jobid: jobId,
Hash: hash,
})
assert.Nil(t, err)
assert.NotEmpty(t, id)
@@ -36,5 +40,6 @@ func TestDocument(t *testing.T) {
assert.EqualExportedValues(t, &repository.Document{
ID: id,
Jobid: jobId,
Hash: hash,
}, doc)
}
+4 -2
View File
@@ -102,8 +102,10 @@ type Collectorquerydependencytree struct {
}
type Document struct {
ID pgtype.UUID `db:"id"`
Jobid pgtype.UUID `db:"jobid"`
ID pgtype.UUID `db:"id"`
Jobid pgtype.UUID `db:"jobid"`
Hash string `db:"hash"`
Location string `db:"location"`
}
type Fullactivecollector struct {
+4 -1
View File
@@ -31,7 +31,10 @@ func TestResults(t *testing.T) {
assert.Nil(t, err)
jobId, err := queries.CreateJob(ctx, clientId)
assert.Nil(t, err)
documentID, err := queries.CreateDocument(ctx, jobId)
documentID, err := queries.CreateDocument(ctx, &repository.CreateDocumentParams{
Jobid: jobId,
Hash: "example_hash",
})
assert.Nil(t, err)
jsonQuery, err := queries.GetQuery(ctx, jsonQueryID)