From ff83437370775335b8776f63fac336b7fdb170a5 Mon Sep 17 00:00:00 2001 From: roman_harazha Date: Mon, 29 Jan 2024 14:58:22 +0200 Subject: [PATCH] add proof field --- docs/spec/components/schemas/Proof.yaml | 5 ++++ docs/spec/components/schemas/ProofCreate.yaml | 5 ++++ .../assets/migrations/003_proof_field.sql | 5 ++++ internal/data/pg/schema.xo.go | 24 +++++++++---------- internal/data/schema.xo.go | 1 + .../services/api/handlers/create_proof.go | 2 ++ internal/services/api/handlers/get_proof.go | 1 + .../api/handlers/get_proofs_by_link.go | 1 + .../api/handlers/get_proofs_by_user.go | 2 ++ resources/model_proof_attributes.go | 2 ++ resources/model_proof_create.go | 2 ++ 11 files changed, 38 insertions(+), 12 deletions(-) create mode 100644 internal/assets/migrations/003_proof_field.sql diff --git a/docs/spec/components/schemas/Proof.yaml b/docs/spec/components/schemas/Proof.yaml index f20e814..cfb5fdd 100644 --- a/docs/spec/components/schemas/Proof.yaml +++ b/docs/spec/components/schemas/Proof.yaml @@ -14,6 +14,7 @@ allOf: - org_id - schema_url - operator + - field properties: creator: type: string @@ -40,3 +41,7 @@ allOf: type: string description: The operator that will be used to check the proof example: "$eq" + field: + type: string + description: The field that will be checked + example: "groupID" diff --git a/docs/spec/components/schemas/ProofCreate.yaml b/docs/spec/components/schemas/ProofCreate.yaml index 9d6a9b2..8faec6b 100644 --- a/docs/spec/components/schemas/ProofCreate.yaml +++ b/docs/spec/components/schemas/ProofCreate.yaml @@ -6,6 +6,7 @@ required: - user_did - schema_url - operator + - field properties: user_did: type: string @@ -26,3 +27,7 @@ properties: operator: type: string description: The operator that will be used to check the proof + field: + type: string + description: The field that will be checked + example: "groupID" diff --git a/internal/assets/migrations/003_proof_field.sql b/internal/assets/migrations/003_proof_field.sql new file mode 100644 index 0000000..42e2f50 --- /dev/null +++ b/internal/assets/migrations/003_proof_field.sql @@ -0,0 +1,5 @@ +-- +migrate Up +alter table proofs add column field text not null; + +-- +migrate Down +alter table proofs drop column field; diff --git a/internal/data/pg/schema.xo.go b/internal/data/pg/schema.xo.go index b1a86a2..431e175 100644 --- a/internal/data/pg/schema.xo.go +++ b/internal/data/pg/schema.xo.go @@ -292,18 +292,18 @@ func (s Storage) ProofQ() data.ProofQ { return NewProofQ(s.DB()) } -var colsProof = `id, creator, created_at, proof, org_id, type, schema_url, operator` +var colsProof = `id, creator, created_at, proof, org_id, type, schema_url, operator, field` // InsertCtx inserts a Proof to the database. func (q ProofQ) InsertCtx(ctx context.Context, p *data.Proof) error { // sql insert query, primary key must be provided sqlstr := `INSERT INTO public.proofs (` + - `id, creator, created_at, proof, org_id, type, schema_url, operator` + + `id, creator, created_at, proof, org_id, type, schema_url, operator, field` + `) VALUES (` + - `$1, $2, $3, $4, $5, $6, $7, $8` + + `$1, $2, $3, $4, $5, $6, $7, $8, $9` + `)` // run - err := q.db.ExecRawContext(ctx, sqlstr, p.ID, p.Creator, p.CreatedAt, p.Proof, p.OrgID, p.Type, p.SchemaURL, p.Operator) + err := q.db.ExecRawContext(ctx, sqlstr, p.ID, p.Creator, p.CreatedAt, p.Proof, p.OrgID, p.Type, p.SchemaURL, p.Operator, p.Field) return errors.Wrap(err, "failed to execute insert query") } @@ -316,10 +316,10 @@ func (q ProofQ) Insert(p *data.Proof) error { func (q ProofQ) UpdateCtx(ctx context.Context, p *data.Proof) error { // update with composite primary key sqlstr := `UPDATE public.proofs SET ` + - `creator = $1, proof = $2, org_id = $3, type = $4, schema_url = $5, operator = $6 ` + - `WHERE id = $7` + `creator = $1, proof = $2, org_id = $3, type = $4, schema_url = $5, operator = $6, field = $7 ` + + `WHERE id = $8` // run - err := q.db.ExecRawContext(ctx, sqlstr, p.Creator, p.Proof, p.OrgID, p.Type, p.SchemaURL, p.Operator, p.ID) + err := q.db.ExecRawContext(ctx, sqlstr, p.Creator, p.Proof, p.OrgID, p.Type, p.SchemaURL, p.Operator, p.Field, p.ID) return errors.Wrap(err, "failed to execute update") } @@ -332,15 +332,15 @@ func (q ProofQ) Update(p *data.Proof) error { func (q ProofQ) UpsertCtx(ctx context.Context, p *data.Proof) error { // upsert sqlstr := `INSERT INTO public.proofs (` + - `id, creator, created_at, proof, org_id, type, schema_url, operator` + + `id, creator, created_at, proof, org_id, type, schema_url, operator, field` + `) VALUES (` + - `$1, $2, $3, $4, $5, $6, $7, $8` + + `$1, $2, $3, $4, $5, $6, $7, $8, $9` + `)` + ` ON CONFLICT (id) DO ` + `UPDATE SET ` + - `creator = EXCLUDED.creator, proof = EXCLUDED.proof, org_id = EXCLUDED.org_id, type = EXCLUDED.type, schema_url = EXCLUDED.schema_url, operator = EXCLUDED.operator ` + `creator = EXCLUDED.creator, proof = EXCLUDED.proof, org_id = EXCLUDED.org_id, type = EXCLUDED.type, schema_url = EXCLUDED.schema_url, operator = EXCLUDED.operator, field = EXCLUDED.field ` // run - if err := q.db.ExecRawContext(ctx, sqlstr, p.ID, p.Creator, p.CreatedAt, p.Proof, p.OrgID, p.Type, p.SchemaURL, p.Operator); err != nil { + if err := q.db.ExecRawContext(ctx, sqlstr, p.ID, p.Creator, p.CreatedAt, p.Proof, p.OrgID, p.Type, p.SchemaURL, p.Operator, p.Field); err != nil { return errors.Wrap(err, "failed to execute upsert stmt") } return nil @@ -473,7 +473,7 @@ func (q LinksToProofQ) LinksToProofByLinkIDProofID(linkID string, proofID uuid.U func (q ProofQ) ProofByIDCtx(ctx context.Context, id uuid.UUID, isForUpdate bool) (*data.Proof, error) { // query sqlstr := `SELECT ` + - `id, creator, created_at, proof, org_id, type, schema_url, operator ` + + `id, creator, created_at, proof, org_id, type, schema_url, operator, field ` + `FROM public.proofs ` + `WHERE id = $1` // run diff --git a/internal/data/schema.xo.go b/internal/data/schema.xo.go index 9e78ab9..35e6745 100644 --- a/internal/data/schema.xo.go +++ b/internal/data/schema.xo.go @@ -96,5 +96,6 @@ type Proof struct { Type string `db:"type" json:"type" structs:"type"` // type SchemaURL string `db:"schema_url" json:"schema_url" structs:"schema_url"` // schema_url Operator ProofOperator `db:"operator" json:"operator" structs:"operator"` // operator + Field string `db:"field" json:"field" structs:"field"` // field } diff --git a/internal/services/api/handlers/create_proof.go b/internal/services/api/handlers/create_proof.go index 116fba1..3fc1f7c 100644 --- a/internal/services/api/handlers/create_proof.go +++ b/internal/services/api/handlers/create_proof.go @@ -74,6 +74,7 @@ func CreateProof(w http.ResponseWriter, r *http.Request) { OrgID: orgID, SchemaURL: req.Data.SchemaUrl, Operator: data.MustProofOperatorFromString(req.Data.Operator), + Field: req.Data.Field, } err = Storage(r).ProofQ().Insert(&proof) @@ -97,6 +98,7 @@ func CreateProof(w http.ResponseWriter, r *http.Request) { OrgId: proof.OrgID.String(), SchemaUrl: req.Data.SchemaUrl, Operator: proof.Operator.String(), + Field: req.Data.Field, }, }, Included: resources.Included{}, diff --git a/internal/services/api/handlers/get_proof.go b/internal/services/api/handlers/get_proof.go index 03c73f5..25e2578 100644 --- a/internal/services/api/handlers/get_proof.go +++ b/internal/services/api/handlers/get_proof.go @@ -67,6 +67,7 @@ func ProofByID(w http.ResponseWriter, r *http.Request) { OrgId: proof.OrgID.String(), SchemaUrl: proof.SchemaURL, Operator: proof.Operator.String(), + Field: proof.Field, }, }, Included: resources.Included{}, diff --git a/internal/services/api/handlers/get_proofs_by_link.go b/internal/services/api/handlers/get_proofs_by_link.go index ef10293..32dcde3 100644 --- a/internal/services/api/handlers/get_proofs_by_link.go +++ b/internal/services/api/handlers/get_proofs_by_link.go @@ -102,6 +102,7 @@ func GetLinkByID(w http.ResponseWriter, r *http.Request) { OrgId: proof.OrgID.String(), SchemaUrl: proof.SchemaURL, Operator: proof.Operator.String(), + Field: proof.Field, }, }) } diff --git a/internal/services/api/handlers/get_proofs_by_user.go b/internal/services/api/handlers/get_proofs_by_user.go index ff9a763..5b017b8 100644 --- a/internal/services/api/handlers/get_proofs_by_user.go +++ b/internal/services/api/handlers/get_proofs_by_user.go @@ -67,6 +67,8 @@ func GetProofs(w http.ResponseWriter, r *http.Request) { ProofType: proof.Type, OrgId: proof.OrgID.String(), SchemaUrl: proof.SchemaURL, + Operator: proof.Operator.String(), + Field: proof.Field, }, }, } diff --git a/resources/model_proof_attributes.go b/resources/model_proof_attributes.go index 6338901..e43c450 100644 --- a/resources/model_proof_attributes.go +++ b/resources/model_proof_attributes.go @@ -11,6 +11,8 @@ type ProofAttributes struct { CreatedAt time.Time `json:"created_at"` // The ID of the user who created the proof Creator string `json:"creator"` + // The field that will be checked + Field string `json:"field"` // The operator that will be used to check the proof Operator string `json:"operator"` // The ID of the organization that issued the proof's claim diff --git a/resources/model_proof_create.go b/resources/model_proof_create.go index 64f1ff2..0b74403 100644 --- a/resources/model_proof_create.go +++ b/resources/model_proof_create.go @@ -5,6 +5,8 @@ package resources type ProofCreate struct { + // The field that will be checked + Field string `json:"field"` // The operator that will be used to check the proof Operator string `json:"operator"` // The ID of the organization that issued the proof's claim