From 2621c0e2e2a2a1ed654f4dc9e05b50a88e5a35f9 Mon Sep 17 00:00:00 2001 From: Asim Aslam Date: Wed, 2 Jun 2021 18:31:27 +0100 Subject: [PATCH] Update endpoint (#135) * [WIP] DB service * F * F * F * F * F * F * F * F * F * F * F * F * F * F * F * F * F * F * F * F * F * F * F * F * fixup db and return id in create response * add delete method and some error checking * Add the update method Co-authored-by: Janos Dobronszki --- db/handler/db.go | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/db/handler/db.go b/db/handler/db.go index 3487a08..5b4e1ce 100644 --- a/db/handler/db.go +++ b/db/handler/db.go @@ -56,8 +56,29 @@ func (e *Db) Create(ctx context.Context, req *db.CreateRequest, rsp *db.CreateRe } func (e *Db) Update(ctx context.Context, req *db.UpdateRequest, rsp *db.UpdateResponse) error { + if len(req.Record) == 0 { + return errors.BadRequest("db.update", "missing record") + } - return nil + db, err := e.GetDBConn(ctx) + if err != nil { + return err + } + + m := map[string]interface{}{} + err = json.Unmarshal([]byte(req.Record), &m) + if err != nil { + return err + } + + // where ID is specified do a single update record update + if id, ok := m["ID"].(string); ok { + // apply the update to a single record + return db.Table(req.Table).First(Record{ID: id}).Updates(m).Error + } + + // apply all the updates + return db.Table(req.Table).Updates(m).Error } func (e *Db) Read(ctx context.Context, req *db.ReadRequest, rsp *db.ReadResponse) error {