diff options
author | Bryan Newbold <bnewbold@robocracy.org> | 2018-05-10 22:17:43 -0700 |
---|---|---|
committer | Bryan Newbold <bnewbold@robocracy.org> | 2018-05-10 22:17:43 -0700 |
commit | 1434bc978f0dc2e25455974903c040230b36101b (patch) | |
tree | 9d09367401c2ab6b0f2a1b6a6612f4d2d32b24ba /golang/api/handlers | |
parent | e63d2b6c2bc7c7ecf5e57e4e25267113f8a1ed51 (diff) | |
download | fatcat-1434bc978f0dc2e25455974903c040230b36101b.tar.gz fatcat-1434bc978f0dc2e25455974903c040230b36101b.zip |
basic creator POST
Diffstat (limited to 'golang/api/handlers')
-rw-r--r-- | golang/api/handlers/entities.go | 60 |
1 files changed, 55 insertions, 5 deletions
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) } |