From 1434bc978f0dc2e25455974903c040230b36101b Mon Sep 17 00:00:00 2001 From: Bryan Newbold Date: Thu, 10 May 2018 22:17:43 -0700 Subject: basic creator POST --- golang/api/handlers/entities.go | 60 +++++++++++++++-- golang/fatcat-openapi2.yml | 23 +++++-- golang/gen/models/creator_edit.go | 52 +++++++++++++++ golang/gen/models/creator_entity.go | 41 ++---------- golang/gen/models/entity_edit.go | 52 +++++++++++++++ golang/gen/restapi/embedded_spec.go | 76 ++++++++++++++++++---- .../restapi/operations/post_creator_responses.go | 6 +- golang/sql/dummy-data.sql | 56 ++++++++-------- golang/sql/fatcat-schema.sql | 2 +- 9 files changed, 278 insertions(+), 90 deletions(-) create mode 100644 golang/gen/models/creator_edit.go create mode 100644 golang/gen/models/entity_edit.go diff --git a/golang/api/handlers/entities.go b/golang/api/handlers/entities.go index 9e184d92..708aebdf 100644 --- a/golang/api/handlers/entities.go +++ b/golang/api/handlers/entities.go @@ -29,6 +29,20 @@ type CreatorIdent struct { tableName struct{} `sql:"creator_ident"` } +type CreatorEdit struct { + Id int64 + ExtraJson string + IdentId string + Ident *CreatorIdent + RevId int64 + Rev *CreatorRev + RedirectId string + Redirect *CreatorIdent + EditgroupId int64 + Editgroup *Editgroup + tableName struct{} `sql:"creator_edit"` +} + func (ci *CreatorIdent) State() string { if ci.IsLive && (ci.RedirectId == "") && (ci.RevId == 0) { return "deleted" @@ -66,9 +80,9 @@ func (d *getCreatorID) Handle(params operations.GetCreatorIDParams) middleware.R log.Fatal(err) } api_entity := &models.CreatorEntity{ - Ident: &db_entity_ident.Id, - State: swag.String(db_entity_ident.State()), - Name: swag.String(db_entity_ident.Rev.Name), + Ident: db_entity_ident.Id, + State: db_entity_ident.State(), + Name: db_entity_ident.Rev.Name, Orcid: db_entity_ident.Rev.Orcid, } return operations.NewGetCreatorIDOK().WithPayload(api_entity) @@ -96,6 +110,42 @@ type postCreator struct { } func (d *postCreator) Handle(params operations.PostCreatorParams) middleware.Responder { // get-or-create editgroup based on current editor (session) - // insert new rev, ident, and edit - return middleware.NotImplemented("operation .PostCreatorID has not yet been implemented") + var eg Editgroup + if true { + eg = Editgroup{Id: 1} + } else { + eg = GetOrCreateEditgroup() + } + ce := CreatorEdit{} + + // big honking SQL to update 3 tables in a single INSERT + _, err := d.db.QueryOne( + //Model(ce).ExecOne( + &ce, + `WITH rev AS ( INSERT INTO creator_rev (name, orcid) + VALUES (?, ?) + RETURNING id ), + ident AS ( INSERT INTO creator_ident (rev_id) + VALUES ((SELECT rev.id FROM rev)) + RETURNING id ) + INSERT INTO creator_edit (editgroup_id, ident_id, rev_id) VALUES + (?, (SELECT ident.id FROM ident), (SELECT rev.id FROM rev)) + RETURNING *`, + params.Body.Name, + params.Body.Orcid, + eg.Id) + if err != nil { + log.Fatal(err) + } + if err != nil { + log.Fatal(err) + } + // redirect? or return the edit? + api_edit:= models.EntityEdit { + ID: ce.Id, + Ident: ce.IdentId, + Revision: ce.RevId, + EditgroupID: ce.EditgroupId, + } + return operations.NewPostCreatorCreated().WithPayload(&api_edit) } diff --git a/golang/fatcat-openapi2.yml b/golang/fatcat-openapi2.yml index 5d22213f..6075940b 100644 --- a/golang/fatcat-openapi2.yml +++ b/golang/fatcat-openapi2.yml @@ -26,7 +26,15 @@ x-entity-props: &ENTITYPROPS redirect: type: string #format: uuid - +x-entity-edit-props: &ENTITYEDITPROPS + id: + type: integer + ident: + type: string + revision: + type: integer + editgroup_id: + type: integer definitions: error: @@ -57,10 +65,6 @@ definitions: #format: custom creator_entity: type: object - required: - - ident - - state - - name properties: <<: *ENTITYPROPS name: @@ -68,6 +72,10 @@ definitions: orcid: type: string #format: custom + entity_edit: + type: object + properties: + <<: *ENTITYEDITPROPS editor: type: object required: @@ -106,11 +114,14 @@ paths: in: body schema: $ref: "#/definitions/creator_entity" + #- name: editgroup + # in: body + # type: string responses: 201: description: created schema: - $ref: "#/definitions/creator_entity" + $ref: "#/definitions/entity_edit" 400: description: bad request schema: diff --git a/golang/gen/models/creator_edit.go b/golang/gen/models/creator_edit.go new file mode 100644 index 00000000..05bfbf75 --- /dev/null +++ b/golang/gen/models/creator_edit.go @@ -0,0 +1,52 @@ +// Code generated by go-swagger; DO NOT EDIT. + +package models + +// This file was generated by the swagger tool. +// Editing this file might prove futile when you re-run the swagger generate command + +import ( + strfmt "github.com/go-openapi/strfmt" + + "github.com/go-openapi/swag" +) + +// CreatorEdit creator edit +// swagger:model creator_edit +type CreatorEdit struct { + + // editgroup id + EditgroupID int64 `json:"editgroup_id,omitempty"` + + // id + ID int64 `json:"id,omitempty"` + + // ident + Ident string `json:"ident,omitempty"` + + // revision + Revision int64 `json:"revision,omitempty"` +} + +// Validate validates this creator edit +func (m *CreatorEdit) Validate(formats strfmt.Registry) error { + return nil +} + +// MarshalBinary interface implementation +func (m *CreatorEdit) MarshalBinary() ([]byte, error) { + if m == nil { + return nil, nil + } + return swag.WriteJSON(m) +} + +// UnmarshalBinary interface implementation +func (m *CreatorEdit) UnmarshalBinary(b []byte) error { + var res CreatorEdit + if err := swag.ReadJSON(b, &res); err != nil { + return err + } + *m = res + return nil +} diff --git a/golang/gen/models/creator_entity.go b/golang/gen/models/creator_entity.go index 0f1532a8..47ba34a6 100644 --- a/golang/gen/models/creator_entity.go +++ b/golang/gen/models/creator_entity.go @@ -20,12 +20,10 @@ import ( type CreatorEntity struct { // ident - // Required: true - Ident *string `json:"ident"` + Ident string `json:"ident,omitempty"` // name - // Required: true - Name *string `json:"name"` + Name string `json:"name,omitempty"` // orcid Orcid string `json:"orcid,omitempty"` @@ -37,23 +35,14 @@ type CreatorEntity struct { Revision int64 `json:"revision,omitempty"` // state - // Required: true // Enum: [wip active redirect deleted] - State *string `json:"state"` + State string `json:"state,omitempty"` } // Validate validates this creator entity func (m *CreatorEntity) Validate(formats strfmt.Registry) error { var res []error - if err := m.validateIdent(formats); err != nil { - res = append(res, err) - } - - if err := m.validateName(formats); err != nil { - res = append(res, err) - } - if err := m.validateState(formats); err != nil { res = append(res, err) } @@ -64,24 +53,6 @@ func (m *CreatorEntity) Validate(formats strfmt.Registry) error { return nil } -func (m *CreatorEntity) validateIdent(formats strfmt.Registry) error { - - if err := validate.Required("ident", "body", m.Ident); err != nil { - return err - } - - return nil -} - -func (m *CreatorEntity) validateName(formats strfmt.Registry) error { - - if err := validate.Required("name", "body", m.Name); err != nil { - return err - } - - return nil -} - var creatorEntityTypeStatePropEnum []interface{} func init() { @@ -119,12 +90,12 @@ func (m *CreatorEntity) validateStateEnum(path, location string, value string) e func (m *CreatorEntity) validateState(formats strfmt.Registry) error { - if err := validate.Required("state", "body", m.State); err != nil { - return err + if swag.IsZero(m.State) { // not required + return nil } // value enum - if err := m.validateStateEnum("state", "body", *m.State); err != nil { + if err := m.validateStateEnum("state", "body", m.State); err != nil { return err } diff --git a/golang/gen/models/entity_edit.go b/golang/gen/models/entity_edit.go new file mode 100644 index 00000000..a9b9ae9f --- /dev/null +++ b/golang/gen/models/entity_edit.go @@ -0,0 +1,52 @@ +// Code generated by go-swagger; DO NOT EDIT. + +package models + +// This file was generated by the swagger tool. +// Editing this file might prove futile when you re-run the swagger generate command + +import ( + strfmt "github.com/go-openapi/strfmt" + + "github.com/go-openapi/swag" +) + +// EntityEdit entity edit +// swagger:model entity_edit +type EntityEdit struct { + + // editgroup id + EditgroupID int64 `json:"editgroup_id,omitempty"` + + // id + ID int64 `json:"id,omitempty"` + + // ident + Ident string `json:"ident,omitempty"` + + // revision + Revision int64 `json:"revision,omitempty"` +} + +// Validate validates this entity edit +func (m *EntityEdit) Validate(formats strfmt.Registry) error { + return nil +} + +// MarshalBinary interface implementation +func (m *EntityEdit) MarshalBinary() ([]byte, error) { + if m == nil { + return nil, nil + } + return swag.WriteJSON(m) +} + +// UnmarshalBinary interface implementation +func (m *EntityEdit) UnmarshalBinary(b []byte) error { + var res EntityEdit + if err := swag.ReadJSON(b, &res); err != nil { + return err + } + *m = res + return nil +} diff --git a/golang/gen/restapi/embedded_spec.go b/golang/gen/restapi/embedded_spec.go index 8e2a9ac4..cec33018 100644 --- a/golang/gen/restapi/embedded_spec.go +++ b/golang/gen/restapi/embedded_spec.go @@ -51,7 +51,7 @@ func init() { "201": { "description": "created", "schema": { - "$ref": "#/definitions/creator_entity" + "$ref": "#/definitions/entity_edit" } }, "400": { @@ -313,11 +313,6 @@ func init() { }, "creator_entity": { "type": "object", - "required": [ - "ident", - "state", - "name" - ], "properties": { "ident": { "type": "string" @@ -371,6 +366,23 @@ func init() { } } }, + "entity_edit": { + "type": "object", + "properties": { + "editgroup_id": { + "type": "integer" + }, + "id": { + "type": "integer" + }, + "ident": { + "type": "string" + }, + "revision": { + "type": "integer" + } + } + }, "error": { "type": "object", "required": [ @@ -427,6 +439,20 @@ func init() { } } }, + "x-entity-edit-props": { + "editgroup_id": { + "type": "integer" + }, + "id": { + "type": "integer" + }, + "ident": { + "type": "string" + }, + "revision": { + "type": "integer" + } + }, "x-entity-props": { "ident": { "type": "string" @@ -482,7 +508,7 @@ func init() { "201": { "description": "created", "schema": { - "$ref": "#/definitions/creator_entity" + "$ref": "#/definitions/entity_edit" } }, "400": { @@ -744,11 +770,6 @@ func init() { }, "creator_entity": { "type": "object", - "required": [ - "ident", - "state", - "name" - ], "properties": { "ident": { "type": "string" @@ -802,6 +823,23 @@ func init() { } } }, + "entity_edit": { + "type": "object", + "properties": { + "editgroup_id": { + "type": "integer" + }, + "id": { + "type": "integer" + }, + "ident": { + "type": "string" + }, + "revision": { + "type": "integer" + } + } + }, "error": { "type": "object", "required": [ @@ -858,6 +896,20 @@ func init() { } } }, + "x-entity-edit-props": { + "editgroup_id": { + "type": "integer" + }, + "id": { + "type": "integer" + }, + "ident": { + "type": "string" + }, + "revision": { + "type": "integer" + } + }, "x-entity-props": { "ident": { "type": "string" diff --git a/golang/gen/restapi/operations/post_creator_responses.go b/golang/gen/restapi/operations/post_creator_responses.go index dcf661a5..ee95d2e4 100644 --- a/golang/gen/restapi/operations/post_creator_responses.go +++ b/golang/gen/restapi/operations/post_creator_responses.go @@ -25,7 +25,7 @@ type PostCreatorCreated struct { /* In: Body */ - Payload *models.CreatorEntity `json:"body,omitempty"` + Payload *models.EntityEdit `json:"body,omitempty"` } // NewPostCreatorCreated creates PostCreatorCreated with default headers values @@ -35,13 +35,13 @@ func NewPostCreatorCreated() *PostCreatorCreated { } // WithPayload adds the payload to the post creator created response -func (o *PostCreatorCreated) WithPayload(payload *models.CreatorEntity) *PostCreatorCreated { +func (o *PostCreatorCreated) WithPayload(payload *models.EntityEdit) *PostCreatorCreated { o.Payload = payload return o } // SetPayload sets the payload to the post creator created response -func (o *PostCreatorCreated) SetPayload(payload *models.CreatorEntity) { +func (o *PostCreatorCreated) SetPayload(payload *models.EntityEdit) { o.Payload = payload } diff --git a/golang/sql/dummy-data.sql b/golang/sql/dummy-data.sql index 9e01fae8..d553b9f1 100644 --- a/golang/sql/dummy-data.sql +++ b/golang/sql/dummy-data.sql @@ -3,29 +3,29 @@ BEGIN; -INSERT INTO editor (id, username, is_admin) VALUES - (1, 'admin', true), - (2, 'claire', true), - (3, 'doug', false); - -INSERT INTO editgroup (id, editor_id, description) VALUES - (1, 1, 'first edit ever!'), - (2, 1, 'another one!'), - (3, 3, 'user edit'), - (4, 2, 'uncommited edit'); - -INSERT INTO editor (id, username, is_admin, active_editgroup_id) VALUES - (4, 'bnewbold', true, 4); - -INSERT INTO changelog (id, editgroup_id) VALUES - (1, 1), - (2, 2), - (3, 3); - -INSERT INTO creator_rev (id, name, orcid) VALUES - (1, 'Grace Hopper', null), - (2, 'Emily Noethe', null), - (3, 'Christine Moran', '0000-0003-2088-7465'); +INSERT INTO editor (username, is_admin) VALUES + ('admin', true), + ('claire', true), + ('doug', false); + +INSERT INTO editgroup (editor_id, description) VALUES + (1, 'first edit ever!'), + (1, 'another one!'), + (3, 'user edit'), + (2, 'uncommited edit'); + +INSERT INTO editor (username, is_admin, active_editgroup_id) VALUES + ('bnewbold', true, 4); + +INSERT INTO changelog (editgroup_id) VALUES + (1), + (2), + (3); + +INSERT INTO creator_rev (name, orcid) VALUES + ('Grace Hopper', null), + ('Emily Noethe', null), + ('Christine Moran', '0000-0003-2088-7465'); INSERT INTO creator_ident (id, is_live, rev_id, redirect_id) VALUES ('f1f046a3-45c9-4b99-adce-000000000001', true, 1, null), @@ -33,10 +33,10 @@ INSERT INTO creator_ident (id, is_live, rev_id, redirect_id) VALUES ('f1f046a3-45c9-4b99-adce-000000000003', true, 3, null), ('f1f046a3-45c9-4b99-adce-000000000004', false, 2, null); -INSERT INTO creator_edit (id, ident_id, rev_id, redirect_id, editgroup_id) VALUES - (1, 'f1f046a3-45c9-4b99-adce-000000000001', 1, null, 1), - (2, 'f1f046a3-45c9-4b99-adce-000000000002', 2, null, 2), - (3, 'f1f046a3-45c9-4b99-adce-000000000003', 3, null, 3), - (4, 'f1f046a3-45c9-4b99-adce-000000000004', 2, null, 4); +INSERT INTO creator_edit (ident_id, rev_id, redirect_id, editgroup_id) VALUES + ('f1f046a3-45c9-4b99-adce-000000000001', 1, null, 1), + ('f1f046a3-45c9-4b99-adce-000000000002', 2, null, 2), + ('f1f046a3-45c9-4b99-adce-000000000003', 3, null, 3), + ('f1f046a3-45c9-4b99-adce-000000000004', 2, null, 4); COMMIT; diff --git a/golang/sql/fatcat-schema.sql b/golang/sql/fatcat-schema.sql index f5983371..fe07390f 100644 --- a/golang/sql/fatcat-schema.sql +++ b/golang/sql/fatcat-schema.sql @@ -53,7 +53,7 @@ CREATE INDEX creator_rev_orcid_idx ON creator_rev(orcid) WHERE orcid IS NOT NULL CREATE TABLE creator_ident ( id UUID PRIMARY KEY DEFAULT uuid_generate_v4(), - is_live BOOL NOT NULL, + is_live BOOL NOT NULL DEFAULT false, rev_id BIGINT REFERENCES creator_rev(id), redirect_id UUID REFERENCES creator_ident(id) ); -- cgit v1.2.3