Add DB delete (#134)

This commit is contained in:
Asim Aslam
2021-06-02 18:19:09 +01:00
committed by GitHub
parent d49c75bd5c
commit abbb37c592
8 changed files with 57 additions and 33 deletions

View File

@@ -6,6 +6,7 @@ import (
"fmt"
"github.com/google/uuid"
"github.com/micro/micro/v3/service/errors"
db "github.com/micro/services/db/proto"
gorm2 "github.com/micro/services/pkg/gorm"
"gorm.io/datatypes"
@@ -22,6 +23,10 @@ type Db struct {
// Call is a single request handler called via client.Call or the generated client code
func (e *Db) Create(ctx context.Context, req *db.CreateRequest, rsp *db.CreateResponse) error {
if len(req.Record) == 0 {
return errors.BadRequest("db.create", "missing record")
}
db, err := e.GetDBConn(ctx)
if err != nil {
return err
@@ -35,10 +40,19 @@ func (e *Db) Create(ctx context.Context, req *db.CreateRequest, rsp *db.CreateRe
m["ID"] = uuid.New().String()
}
bs, _ := json.Marshal(m)
return db.Table(req.Table).Create(Record{
err = db.Table(req.Table).Create(Record{
ID: m["ID"].(string),
Data: bs,
}).Error
if err != nil {
return err
}
// set the response id
rsp.Id = m["ID"].(string)
return nil
}
func (e *Db) Update(ctx context.Context, req *db.UpdateRequest, rsp *db.UpdateResponse) error {
@@ -103,6 +117,16 @@ func (e *Db) Read(ctx context.Context, req *db.ReadRequest, rsp *db.ReadResponse
}
func (e *Db) Delete(ctx context.Context, req *db.DeleteRequest, rsp *db.DeleteResponse) error {
if len(req.Id) == 0 {
return errors.BadRequest("db.delete", "missing id")
}
return nil
db, err := e.GetDBConn(ctx)
if err != nil {
return err
}
return db.Table(req.Table).Delete(Record{
ID: req.Id,
}).Error
}

View File

@@ -96,7 +96,7 @@ func Parse(q string) ([]Query, error) {
switch current.Op {
case itemEquals, itemNotEquals:
default:
return nil, fmt.Errorf("operator '%v' can't be used with strings", opToString[token.Typ])
return nil, fmt.Errorf("operator '%v' can't be used with strings", opToString[current.Op])
}
if len(token.Text) < 2 {
@@ -107,14 +107,14 @@ func Parse(q string) ([]Query, error) {
switch current.Op {
case itemEquals, itemNotEquals:
default:
return nil, fmt.Errorf("operator '%v' can't be used with bools", opToString[token.Typ])
return nil, fmt.Errorf("operator '%v' can't be used with bools", opToString[current.Op])
}
current.Value = true
case itemBoolFalse:
switch current.Op {
case itemEquals, itemNotEquals:
default:
return nil, fmt.Errorf("operator '%v' can't be used with bools", opToString[token.Typ])
return nil, fmt.Errorf("operator '%v' can't be used with bools", opToString[current.Op])
}
current.Value = false
case itemInt: