mirror of
https://github.com/kevin-DL/services.git
synced 2026-01-11 19:04:35 +00:00
Add DB delete (#134)
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user