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,7 +6,6 @@ init:
|
||||
go get -u github.com/golang/protobuf/protoc-gen-go
|
||||
go get github.com/micro/micro/v3/cmd/protoc-gen-micro
|
||||
|
||||
|
||||
.PHONY: api
|
||||
api:
|
||||
protoc --openapi_out=. --proto_path=. proto/db.proto
|
||||
|
||||
@@ -1,2 +1,3 @@
|
||||
package main
|
||||
|
||||
//go:generate make proto
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
// Code generated by protoc-gen-go. DO NOT EDIT.
|
||||
// versions:
|
||||
// protoc-gen-go v1.26.0
|
||||
// protoc v3.6.1
|
||||
// protoc v3.15.6
|
||||
// source: proto/db.proto
|
||||
|
||||
package db
|
||||
@@ -220,6 +220,9 @@ type CreateResponse struct {
|
||||
state protoimpl.MessageState
|
||||
sizeCache protoimpl.SizeCache
|
||||
unknownFields protoimpl.UnknownFields
|
||||
|
||||
// The id of the record
|
||||
Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"`
|
||||
}
|
||||
|
||||
func (x *CreateResponse) Reset() {
|
||||
@@ -254,6 +257,13 @@ func (*CreateResponse) Descriptor() ([]byte, []int) {
|
||||
return file_proto_db_proto_rawDescGZIP(), []int{3}
|
||||
}
|
||||
|
||||
func (x *CreateResponse) GetId() string {
|
||||
if x != nil {
|
||||
return x.Id
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
type UpdateRequest struct {
|
||||
state protoimpl.MessageState
|
||||
sizeCache protoimpl.SizeCache
|
||||
@@ -462,8 +472,9 @@ var file_proto_db_proto_rawDesc = []byte{
|
||||
0x74, 0x65, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x14, 0x0a, 0x05, 0x74, 0x61, 0x62,
|
||||
0x6c, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x74, 0x61, 0x62, 0x6c, 0x65, 0x12,
|
||||
0x16, 0x0a, 0x06, 0x72, 0x65, 0x63, 0x6f, 0x72, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52,
|
||||
0x06, 0x72, 0x65, 0x63, 0x6f, 0x72, 0x64, 0x22, 0x10, 0x0a, 0x0e, 0x43, 0x72, 0x65, 0x61, 0x74,
|
||||
0x65, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x3d, 0x0a, 0x0d, 0x55, 0x70, 0x64,
|
||||
0x06, 0x72, 0x65, 0x63, 0x6f, 0x72, 0x64, 0x22, 0x20, 0x0a, 0x0e, 0x43, 0x72, 0x65, 0x61, 0x74,
|
||||
0x65, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18,
|
||||
0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x02, 0x69, 0x64, 0x22, 0x3d, 0x0a, 0x0d, 0x55, 0x70, 0x64,
|
||||
0x61, 0x74, 0x65, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x14, 0x0a, 0x05, 0x74, 0x61,
|
||||
0x62, 0x6c, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x74, 0x61, 0x62, 0x6c, 0x65,
|
||||
0x12, 0x16, 0x0a, 0x06, 0x72, 0x65, 0x63, 0x6f, 0x72, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09,
|
||||
@@ -486,9 +497,8 @@ var file_proto_db_proto_rawDesc = []byte{
|
||||
0x06, 0x44, 0x65, 0x6c, 0x65, 0x74, 0x65, 0x12, 0x11, 0x2e, 0x64, 0x62, 0x2e, 0x44, 0x65, 0x6c,
|
||||
0x65, 0x74, 0x65, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x12, 0x2e, 0x64, 0x62, 0x2e,
|
||||
0x44, 0x65, 0x6c, 0x65, 0x74, 0x65, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00,
|
||||
0x42, 0x24, 0x5a, 0x22, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x6d,
|
||||
0x69, 0x63, 0x72, 0x6f, 0x2f, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x73, 0x2f, 0x70, 0x72,
|
||||
0x6f, 0x74, 0x6f, 0x3b, 0x64, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
|
||||
0x42, 0x0c, 0x5a, 0x0a, 0x2e, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x3b, 0x64, 0x62, 0x62, 0x06,
|
||||
0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
|
||||
}
|
||||
|
||||
var (
|
||||
|
||||
@@ -2,7 +2,7 @@ syntax = "proto3";
|
||||
|
||||
package db;
|
||||
|
||||
option go_package = "github.com/micro/services/proto;db";
|
||||
option go_package = "./proto;db";
|
||||
|
||||
service Db {
|
||||
rpc Create(CreateRequest) returns (CreateResponse) {}
|
||||
@@ -38,7 +38,8 @@ message CreateRequest {
|
||||
}
|
||||
|
||||
message CreateResponse {
|
||||
|
||||
// The id of the record
|
||||
string id = 1;
|
||||
}
|
||||
|
||||
message UpdateRequest {
|
||||
@@ -59,4 +60,4 @@ message DeleteRequest {
|
||||
|
||||
message DeleteResponse {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user