diff --git a/docs/spec/components/schemas/Link.yaml b/docs/spec/components/schemas/Link.yaml index c7d0d9d..f28d6c5 100644 --- a/docs/spec/components/schemas/Link.yaml +++ b/docs/spec/components/schemas/Link.yaml @@ -16,5 +16,6 @@ allOf: example: '05260b49-2c35-48b9-8300-d39ae48ca312' created_at: type: string - description: The date and time when the proof was created in the timestamp format - example: '1702480643' + format: time.Time + description: The date and time when the proof was created in the RFC3339 format + example: "2021-08-12T12:00:00Z" diff --git a/docs/spec/components/schemas/Proof.yaml b/docs/spec/components/schemas/Proof.yaml index b09e99e..09c7d96 100644 --- a/docs/spec/components/schemas/Proof.yaml +++ b/docs/spec/components/schemas/Proof.yaml @@ -12,14 +12,16 @@ allOf: - proof - proof_type - org_id + - schema_url properties: creator: type: string description: The ID of the user who created the proof created_at: type: string - description: The date and time when the proof was created in the timestamp format - example: '1702480643' + format: time.Time + description: The date and time when the proof was created in the RFC3339 format + example: "2021-08-12T12:00:00Z" proof: type: string description: The proof object in JSON string format @@ -30,3 +32,6 @@ allOf: org_id: type: string description: The ID of the organization that issued the proof's claim + schema_url: + type: string + description: The schema URL of the claim the proof was created based on diff --git a/docs/spec/components/schemas/ProofCreate.yaml b/docs/spec/components/schemas/ProofCreate.yaml index 5afb079..f2f7218 100644 --- a/docs/spec/components/schemas/ProofCreate.yaml +++ b/docs/spec/components/schemas/ProofCreate.yaml @@ -3,6 +3,7 @@ required: - proof - proof_type - org_id + - schema_url properties: proof: type: string @@ -14,3 +15,6 @@ properties: org_id: type: string description: The ID of the organization that issued the proof's claim + schema_url: + type: string + description: The schema URL of the claim the proof was created based on diff --git a/internal/assets/migrations/002_proof_schema.sql b/internal/assets/migrations/002_proof_schema.sql new file mode 100644 index 0000000..0dd53cf --- /dev/null +++ b/internal/assets/migrations/002_proof_schema.sql @@ -0,0 +1,5 @@ +-- +migrate Up +alter table proofs add column schema_url text not null default ''; + +-- +migrate Down +alter table proofs drop column schema_url; diff --git a/internal/data/main.go b/internal/data/main.go index 8de91f6..045735d 100644 --- a/internal/data/main.go +++ b/internal/data/main.go @@ -6,8 +6,8 @@ import ( "github.com/google/uuid" ) -//go:generate xo schema "postgres://link:link@localhost:15432/link-db?sslmode=disable" -o ./ --single=schema.xo.go --src templates -//go:generate xo schema "postgres://link:link@localhost:15432/link-db?sslmode=disable" -o pg --single=schema.xo.go --src=pg/templates --go-context=both +//go:generate xo schema "postgres://link:link@localhost:25432/link-db?sslmode=disable" -o ./ --single=schema.xo.go --src templates +//go:generate xo schema "postgres://link:link@localhost:25432/link-db?sslmode=disable" -o pg --single=schema.xo.go --src=pg/templates --go-context=both //go:generate goimports -w ./ type Storage interface { diff --git a/internal/data/pg/schema.xo.go b/internal/data/pg/schema.xo.go index 96f7968..04a535d 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` +var colsProof = `id, creator, created_at, proof, org_id, type, schema_url` // 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` + + `id, creator, created_at, proof, org_id, type, schema_url` + `) VALUES (` + - `$1, $2, $3, $4, $5, $6` + + `$1, $2, $3, $4, $5, $6, $7` + `)` // run - err := q.db.ExecRawContext(ctx, sqlstr, p.ID, p.Creator, p.CreatedAt, p.Proof, p.OrgID, p.Type) + err := q.db.ExecRawContext(ctx, sqlstr, p.ID, p.Creator, p.CreatedAt, p.Proof, p.OrgID, p.Type, p.SchemaURL) 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 ` + - `WHERE id = $5` + `creator = $1, proof = $2, org_id = $3, type = $4, schema_url = $5 ` + + `WHERE id = $6` // run - err := q.db.ExecRawContext(ctx, sqlstr, p.Creator, p.Proof, p.OrgID, p.Type, p.ID) + err := q.db.ExecRawContext(ctx, sqlstr, p.Creator, p.Proof, p.OrgID, p.Type, p.SchemaURL, 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` + + `id, creator, created_at, proof, org_id, type, schema_url` + `) VALUES (` + - `$1, $2, $3, $4, $5, $6` + + `$1, $2, $3, $4, $5, $6, $7` + `)` + ` ON CONFLICT (id) DO ` + `UPDATE SET ` + - `creator = EXCLUDED.creator, proof = EXCLUDED.proof, org_id = EXCLUDED.org_id, type = EXCLUDED.type ` + `creator = EXCLUDED.creator, proof = EXCLUDED.proof, org_id = EXCLUDED.org_id, type = EXCLUDED.type, schema_url = EXCLUDED.schema_url ` // run - if err := q.db.ExecRawContext(ctx, sqlstr, p.ID, p.Creator, p.CreatedAt, p.Proof, p.OrgID, p.Type); err != nil { + if err := q.db.ExecRawContext(ctx, sqlstr, p.ID, p.Creator, p.CreatedAt, p.Proof, p.OrgID, p.Type, p.SchemaURL); err != nil { return errors.Wrap(err, "failed to execute upsert stmt") } return nil @@ -473,7 +473,7 @@ func (q LinksToProofQ) LinksToProofByLinkIDProofID(linkID, proofID uuid.UUID, is 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 ` + + `id, creator, created_at, proof, org_id, type, schema_url ` + `FROM public.proofs ` + `WHERE id = $1` // run diff --git a/internal/data/schema.xo.go b/internal/data/schema.xo.go index e7c81ca..070683a 100644 --- a/internal/data/schema.xo.go +++ b/internal/data/schema.xo.go @@ -94,5 +94,6 @@ type Proof struct { Proof xo.Jsonb `db:"proof" json:"proof" structs:"proof"` // proof OrgID uuid.UUID `db:"org_id" json:"org_id" structs:"org_id"` // org_id Type string `db:"type" json:"type" structs:"type"` // type + SchemaURL string `db:"schema_url" json:"schema_url" structs:"schema_url"` // schema_url } diff --git a/internal/services/api/handlers/create_proof.go b/internal/services/api/handlers/create_proof.go index e00406e..d2060c7 100644 --- a/internal/services/api/handlers/create_proof.go +++ b/internal/services/api/handlers/create_proof.go @@ -2,6 +2,9 @@ package handlers import ( "encoding/json" + "net/http" + "time" + validation "github.com/go-ozzo/ozzo-validation/v4" "github.com/google/uuid" "github.com/rarimo/rarime-link-svc/internal/data" @@ -9,9 +12,6 @@ import ( "gitlab.com/distributed_lab/ape" "gitlab.com/distributed_lab/ape/problems" "gitlab.com/distributed_lab/logan/v3/errors" - "net/http" - "strconv" - "time" ) type proofCreateRequest struct { @@ -40,8 +40,6 @@ func newProofCreateRequest(r *http.Request) (*proofCreateRequest, error) { return &req, nil } -// TODO add schema_url; change time format - func CreateProof(w http.ResponseWriter, r *http.Request) { req, err := newProofCreateRequest(r) if err != nil { @@ -62,6 +60,7 @@ func CreateProof(w http.ResponseWriter, r *http.Request) { Proof: []byte(req.Data.Proof), Type: req.Data.ProofType, OrgID: orgID, + SchemaURL: req.Data.SchemaUrl, } err = Storage(r).ProofQ().Insert(&proof) @@ -78,11 +77,12 @@ func CreateProof(w http.ResponseWriter, r *http.Request) { Type: resources.PROOFS, }, Attributes: resources.ProofAttributes{ - CreatedAt: strconv.FormatInt(proof.CreatedAt.Unix(), 10), + CreatedAt: proof.CreatedAt, Creator: proof.Creator, Proof: string(proof.Proof), ProofType: proof.Type, OrgId: proof.OrgID.String(), + SchemaUrl: req.Data.SchemaUrl, }, }, Included: resources.Included{}, diff --git a/internal/services/api/handlers/create_proof_link.go b/internal/services/api/handlers/create_proof_link.go index 8ae3dc7..d80a4bf 100644 --- a/internal/services/api/handlers/create_proof_link.go +++ b/internal/services/api/handlers/create_proof_link.go @@ -92,7 +92,7 @@ func CreateProofLink(w http.ResponseWriter, r *http.Request) { }, Attributes: resources.LinkAttributes{ Link: linkID.String(), - CreatedAt: timestamp.String(), + CreatedAt: timestamp, }, }, Included: resources.Included{}, diff --git a/internal/services/api/handlers/get_links_by_user.go b/internal/services/api/handlers/get_links_by_user.go index 86bb0a3..3fda9ce 100644 --- a/internal/services/api/handlers/get_links_by_user.go +++ b/internal/services/api/handlers/get_links_by_user.go @@ -8,7 +8,6 @@ import ( "gitlab.com/distributed_lab/ape/problems" "gitlab.com/distributed_lab/urlval" "net/http" - "strconv" ) type proofsLinksByUserDIDRequest struct { @@ -55,7 +54,7 @@ func GetLinks(w http.ResponseWriter, r *http.Request) { Type: resources.LINKS, }, Attributes: resources.LinkAttributes{ - CreatedAt: strconv.FormatInt(link.CreatedAt.Unix(), 10), + CreatedAt: link.CreatedAt, Link: link.ID.String(), }, }, diff --git a/internal/services/api/handlers/get_proof.go b/internal/services/api/handlers/get_proof.go index be518fe..b1e5b5b 100644 --- a/internal/services/api/handlers/get_proof.go +++ b/internal/services/api/handlers/get_proof.go @@ -9,7 +9,6 @@ import ( "gitlab.com/distributed_lab/ape/problems" "gitlab.com/distributed_lab/logan/v3/errors" "net/http" - "strconv" ) type proofByIDRequest struct { @@ -54,11 +53,12 @@ func ProofByID(w http.ResponseWriter, r *http.Request) { Type: resources.PROOFS, }, Attributes: resources.ProofAttributes{ - CreatedAt: strconv.FormatInt(proof.CreatedAt.Unix(), 10), + CreatedAt: proof.CreatedAt, Creator: proof.Creator, Proof: string(proof.Proof), ProofType: proof.Type, OrgId: proof.OrgID.String(), + SchemaUrl: proof.SchemaURL, }, }, 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 78e67a3..f4a3106 100644 --- a/internal/services/api/handlers/get_proofs_by_link.go +++ b/internal/services/api/handlers/get_proofs_by_link.go @@ -66,7 +66,7 @@ func GetLinkByID(w http.ResponseWriter, r *http.Request) { Type: resources.LINKS, }, Attributes: resources.LinkAttributes{ - CreatedAt: link.CreatedAt.UTC().String(), + CreatedAt: link.CreatedAt, Link: link.ID.String(), }, }, @@ -86,11 +86,12 @@ func GetLinkByID(w http.ResponseWriter, r *http.Request) { Type: resources.PROOFS, }, Attributes: resources.ProofAttributes{ - CreatedAt: proof.CreatedAt.String(), + CreatedAt: proof.CreatedAt, Creator: proof.Creator, Proof: string(proof.Proof), ProofType: proof.Type, OrgId: proof.OrgID.String(), + SchemaUrl: proof.SchemaURL, }, }) } diff --git a/internal/services/api/handlers/get_proofs_by_user.go b/internal/services/api/handlers/get_proofs_by_user.go index 92c9b46..6cd05a5 100644 --- a/internal/services/api/handlers/get_proofs_by_user.go +++ b/internal/services/api/handlers/get_proofs_by_user.go @@ -1,14 +1,12 @@ package handlers import ( - "net/http" - "strconv" - validation "github.com/go-ozzo/ozzo-validation/v4" "github.com/rarimo/rarime-link-svc/resources" "gitlab.com/distributed_lab/ape" "gitlab.com/distributed_lab/ape/problems" "gitlab.com/distributed_lab/urlval" + "net/http" ) type proofsByUserDIDRequest struct { @@ -57,11 +55,12 @@ func GetProofs(w http.ResponseWriter, r *http.Request) { Type: resources.PROOFS, }, Attributes: resources.ProofAttributes{ - CreatedAt: strconv.FormatInt(proof.CreatedAt.Unix(), 10), + CreatedAt: proof.CreatedAt, Creator: proof.Creator, Proof: string(proof.Proof), ProofType: proof.Type, OrgId: proof.OrgID.String(), + SchemaUrl: proof.SchemaURL, }, }, } diff --git a/resources/model_link_attributes.go b/resources/model_link_attributes.go index 90f4fd2..3049748 100644 --- a/resources/model_link_attributes.go +++ b/resources/model_link_attributes.go @@ -4,9 +4,11 @@ package resources +import "time" + type LinkAttributes struct { - // The date and time when the proof was created in the timestamp format - CreatedAt string `json:"created_at"` + // The date and time when the proof was created in the RFC3339 format + CreatedAt time.Time `json:"created_at"` // UUID Link to proofs Link string `json:"link"` } diff --git a/resources/model_proof_attributes.go b/resources/model_proof_attributes.go index 39aa8dd..31ff939 100644 --- a/resources/model_proof_attributes.go +++ b/resources/model_proof_attributes.go @@ -4,9 +4,11 @@ package resources +import "time" + type ProofAttributes struct { - // The date and time when the proof was created in the timestamp format - CreatedAt string `json:"created_at"` + // The date and time when the proof was created in the RFC3339 format + CreatedAt time.Time `json:"created_at"` // The ID of the user who created the proof Creator string `json:"creator"` // The ID of the organization that issued the proof's claim @@ -15,4 +17,6 @@ type ProofAttributes struct { Proof string `json:"proof"` // The type of the proof ProofType string `json:"proof_type"` + // The schema URL of the claim the proof was created based on + SchemaUrl string `json:"schema_url"` } diff --git a/resources/model_proof_create.go b/resources/model_proof_create.go index 7dfa995..cce7254 100644 --- a/resources/model_proof_create.go +++ b/resources/model_proof_create.go @@ -11,4 +11,6 @@ type ProofCreate struct { Proof string `json:"proof"` // The type of the proof ProofType string `json:"proof_type"` + // The schema URL of the claim the proof was created based on + SchemaUrl string `json:"schema_url"` }