From 0fef89dcb23578683c4d385572ae26333affdfd4 Mon Sep 17 00:00:00 2001 From: "daniel.w" Date: Sun, 6 Oct 2024 15:02:30 +0800 Subject: [PATCH] init project --- .gitignore | 2 +- Makefile | 11 +- .../20240828100000_create_schema.down.sql | 1 - .../20240828100000_create_schema.up.sql | 1 - generate/protobuf/order.proto | 35 +++++- go.mod | 89 +++++++++++++++ internal/config/config.go | 7 ++ .../logic/orderservice/cancel_order_logic.go | 31 ++++++ .../logic/orderservice/create_order_logic.go | 31 ++++++ .../logic/orderservice/delete_order_logic.go | 31 ++++++ .../logic/orderservice/get_order_logic.go | 31 ++++++ .../logic/orderservice/list_order_logic.go | 31 ++++++ .../logic/orderservice/modify_order_logic.go | 31 ++++++ .../orderservice/modify_order_status_logic.go | 31 ++++++ .../order_status_timeout_logic.go | 31 ++++++ internal/mock/model/order_model.go | 101 ++++++++++++++++++ internal/mock/model/order_model_gen.go | 101 ++++++++++++++++++ internal/model/mongo/error.go | 12 +++ internal/model/mongo/order_model.go | 25 +++++ internal/model/mongo/order_model_gen.go | 74 +++++++++++++ internal/model/mongo/order_types.go | 14 +++ .../orderservice/order_service_server.go | 71 ++++++++++++ internal/svc/service_context.go | 13 +++ order.go | 39 +++++++ 24 files changed, 832 insertions(+), 12 deletions(-) delete mode 100644 generate/database/mysql/create/20240828100000_create_schema.down.sql delete mode 100644 generate/database/mysql/create/20240828100000_create_schema.up.sql create mode 100644 go.mod create mode 100755 internal/config/config.go create mode 100644 internal/logic/orderservice/cancel_order_logic.go create mode 100644 internal/logic/orderservice/create_order_logic.go create mode 100644 internal/logic/orderservice/delete_order_logic.go create mode 100644 internal/logic/orderservice/get_order_logic.go create mode 100644 internal/logic/orderservice/list_order_logic.go create mode 100644 internal/logic/orderservice/modify_order_logic.go create mode 100644 internal/logic/orderservice/modify_order_status_logic.go create mode 100644 internal/logic/orderservice/order_status_timeout_logic.go create mode 100644 internal/mock/model/order_model.go create mode 100644 internal/mock/model/order_model_gen.go create mode 100644 internal/model/mongo/error.go create mode 100644 internal/model/mongo/order_model.go create mode 100644 internal/model/mongo/order_model_gen.go create mode 100644 internal/model/mongo/order_types.go create mode 100644 internal/server/orderservice/order_service_server.go create mode 100644 internal/svc/service_context.go create mode 100644 order.go diff --git a/.gitignore b/.gitignore index 480bfb9..d179dd3 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,6 @@ go.sum account/ gen_result/ -etc/feed.yaml +etc/order.yaml client/ .DS_Store diff --git a/Makefile b/Makefile index beb3d7a..fbbfdf5 100644 --- a/Makefile +++ b/Makefile @@ -50,18 +50,15 @@ build-docker: gen-mongo-model: # 建立 rpc 資料庫 # 只產生 Model 剩下的要自己撰寫,連欄位名稱也是 - goctl model mongo -t post --dir ./internal/model/mongo --style $(GO_ZERO_STYLE) - goctl model mongo -t comment --dir ./internal/model/mongo --style $(GO_ZERO_STYLE) - goctl model mongo -t tags --dir ./internal/model/mongo --style $(GO_ZERO_STYLE) - goctl model mongo -t post_likes --dir ./internal/model/mongo --style $(GO_ZERO_STYLE) - goctl model mongo -t comment_likes --dir ./internal/model/mongo --style $(GO_ZERO_STYLE) + goctl model mongo -t order --dir ./internal/model/mongo --style $(GO_ZERO_STYLE) @echo "Generate mongo model files successfully" .PHONY: mock-gen mock-gen: # 建立 mock 資料 - mockgen -source=./internal/model/mongo/post_model_gen.go -destination=./internal/mock/model/post_model_gen.go -package=mock + mockgen -source=./internal/model/mongo/order_model_gen.go -destination=./internal/mock/model/order_model_gen.go -package=mock + mockgen -source=./internal/model/mongo/order_model.go -destination=./internal/mock/model/order_model.go -package=mock @echo "Generate mock files successfully" .PHONY: migrate-database migrate-database: - migrate -source file://generate/database/migrations/mongodb -database 'mongodb://127.0.0.1:27017/digimon_tweeting' up + migrate -source file://generate/database/migrations/mongodb -database 'mongodb://127.0.0.1:27017/digimon_order' up diff --git a/generate/database/mysql/create/20240828100000_create_schema.down.sql b/generate/database/mysql/create/20240828100000_create_schema.down.sql deleted file mode 100644 index 3da4282..0000000 --- a/generate/database/mysql/create/20240828100000_create_schema.down.sql +++ /dev/null @@ -1 +0,0 @@ -DROP DATABASE IF EXISTS `digimon_order`; \ No newline at end of file diff --git a/generate/database/mysql/create/20240828100000_create_schema.up.sql b/generate/database/mysql/create/20240828100000_create_schema.up.sql deleted file mode 100644 index ccfd4ec..0000000 --- a/generate/database/mysql/create/20240828100000_create_schema.up.sql +++ /dev/null @@ -1 +0,0 @@ -CREATE DATABASE IF NOT EXISTS `digimon_order`; \ No newline at end of file diff --git a/generate/protobuf/order.proto b/generate/protobuf/order.proto index ee764f6..7372d20 100644 --- a/generate/protobuf/order.proto +++ b/generate/protobuf/order.proto @@ -16,8 +16,39 @@ message Pager int64 index = 3; // 當前頁碼 } -// ========== 貼文區 =========== +message CreateOrderReq{} +message CreateOrderResp{} +message CancelOrderReq{} +message ModifyOrderReq{} +message ModifyOrderStatusReq{} +message DeleteOrderReq{} + +message GetOrderReq{} +message GetOrderResp{} + +message ListOrderReq{} +message ListOrderResp{} + + +message OrderStatusTimeoutReq{} // OrderService 訂單服務(業務邏輯在外面組合) -service OrderService{} +service OrderService{ + // CreateOrder 建立訂單 + rpc CreateOrder(CreateOrderReq)returns(CreateOrderResp); + // CancelOrder 取消訂單 + rpc CancelOrder(CancelOrderReq)returns(OKResp); + // ModifyOrder 修改訂單 + rpc ModifyOrder(ModifyOrderReq)returns(OKResp); + // ModifyOrderStatus 修改訂單狀態 + rpc ModifyOrderStatus(ModifyOrderStatusReq)returns(OKResp); + // DeleteOrder 刪除訂單(軟刪除) + rpc DeleteOrder(DeleteOrderReq)returns(OKResp); + // GetOrder 取得訂單詳情 + rpc GetOrder(GetOrderReq)returns(GetOrderResp); + // ListOrder 取得訂單列表 + rpc ListOrder(ListOrderReq)returns(ListOrderResp); + // OrderStatusTimeout 訂單超時任務/cron/order-status/timeout + rpc OrderStatusTimeout(OrderStatusTimeoutReq)returns(OKResp); +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..0a5d0f1 --- /dev/null +++ b/go.mod @@ -0,0 +1,89 @@ +module app-cloudep-order-server + +go 1.22.3 + +require ( + github.com/zeromicro/go-zero v1.7.2 + google.golang.org/grpc v1.67.1 + google.golang.org/protobuf v1.34.2 +) + +require ( + github.com/beorn7/perks v1.0.1 // indirect + github.com/cenkalti/backoff/v4 v4.3.0 // indirect + github.com/cespare/xxhash/v2 v2.3.0 // indirect + github.com/coreos/go-semver v0.3.1 // indirect + github.com/coreos/go-systemd/v22 v22.5.0 // indirect + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect + github.com/emicklei/go-restful/v3 v3.11.0 // indirect + github.com/fatih/color v1.17.0 // indirect + github.com/go-logr/logr v1.4.2 // indirect + github.com/go-logr/stdr v1.2.2 // indirect + github.com/go-openapi/jsonpointer v0.19.6 // indirect + github.com/go-openapi/jsonreference v0.20.2 // indirect + github.com/go-openapi/swag v0.22.4 // indirect + github.com/gogo/protobuf v1.3.2 // indirect + github.com/golang/mock v1.6.0 // indirect + github.com/golang/protobuf v1.5.4 // indirect + github.com/google/gnostic-models v0.6.8 // indirect + github.com/google/go-cmp v0.6.0 // indirect + github.com/google/gofuzz v1.2.0 // indirect + github.com/google/uuid v1.6.0 // indirect + github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0 // indirect + github.com/josharian/intern v1.0.0 // indirect + github.com/json-iterator/go v1.1.12 // indirect + github.com/klauspost/compress v1.17.9 // indirect + github.com/mailru/easyjson v0.7.7 // indirect + github.com/mattn/go-colorable v0.1.13 // indirect + github.com/mattn/go-isatty v0.0.20 // indirect + github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect + github.com/modern-go/reflect2 v1.0.2 // indirect + github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect + github.com/openzipkin/zipkin-go v0.4.3 // indirect + github.com/pelletier/go-toml/v2 v2.2.2 // indirect + github.com/prometheus/client_golang v1.20.2 // indirect + github.com/prometheus/client_model v0.6.1 // indirect + github.com/prometheus/common v0.55.0 // indirect + github.com/prometheus/procfs v0.15.1 // indirect + github.com/redis/go-redis/v9 v9.6.1 // indirect + github.com/spaolacci/murmur3 v1.1.0 // indirect + go.etcd.io/etcd/api/v3 v3.5.15 // indirect + go.etcd.io/etcd/client/pkg/v3 v3.5.15 // indirect + go.etcd.io/etcd/client/v3 v3.5.15 // indirect + go.opentelemetry.io/otel v1.24.0 // indirect + go.opentelemetry.io/otel/exporters/jaeger v1.17.0 // indirect + go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.24.0 // indirect + go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.24.0 // indirect + go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.24.0 // indirect + go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.24.0 // indirect + go.opentelemetry.io/otel/exporters/zipkin v1.24.0 // indirect + go.opentelemetry.io/otel/metric v1.24.0 // indirect + go.opentelemetry.io/otel/sdk v1.24.0 // indirect + go.opentelemetry.io/otel/trace v1.24.0 // indirect + go.opentelemetry.io/proto/otlp v1.3.1 // indirect + go.uber.org/atomic v1.10.0 // indirect + go.uber.org/automaxprocs v1.5.3 // indirect + go.uber.org/multierr v1.9.0 // indirect + go.uber.org/zap v1.24.0 // indirect + golang.org/x/net v0.28.0 // indirect + golang.org/x/oauth2 v0.22.0 // indirect + golang.org/x/sys v0.24.0 // indirect + golang.org/x/term v0.23.0 // indirect + golang.org/x/text v0.17.0 // indirect + golang.org/x/time v0.6.0 // indirect + google.golang.org/genproto/googleapis/api v0.0.0-20240814211410-ddb44dafa142 // indirect + google.golang.org/genproto/googleapis/rpc v0.0.0-20240814211410-ddb44dafa142 // indirect + gopkg.in/inf.v0 v0.9.1 // indirect + gopkg.in/yaml.v2 v2.4.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect + k8s.io/api v0.29.3 // indirect + k8s.io/apimachinery v0.29.4 // indirect + k8s.io/client-go v0.29.3 // indirect + k8s.io/klog/v2 v2.110.1 // indirect + k8s.io/kube-openapi v0.0.0-20231010175941-2dd684a91f00 // indirect + k8s.io/utils v0.0.0-20240711033017-18e509b52bc8 // indirect + sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect + sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect + sigs.k8s.io/yaml v1.3.0 // indirect +) diff --git a/internal/config/config.go b/internal/config/config.go new file mode 100755 index 0000000..c1f85b9 --- /dev/null +++ b/internal/config/config.go @@ -0,0 +1,7 @@ +package config + +import "github.com/zeromicro/go-zero/zrpc" + +type Config struct { + zrpc.RpcServerConf +} diff --git a/internal/logic/orderservice/cancel_order_logic.go b/internal/logic/orderservice/cancel_order_logic.go new file mode 100644 index 0000000..053b579 --- /dev/null +++ b/internal/logic/orderservice/cancel_order_logic.go @@ -0,0 +1,31 @@ +package orderservicelogic + +import ( + "context" + + "app-cloudep-order-server/gen_result/pb/tweeting" + "app-cloudep-order-server/internal/svc" + + "github.com/zeromicro/go-zero/core/logx" +) + +type CancelOrderLogic struct { + ctx context.Context + svcCtx *svc.ServiceContext + logx.Logger +} + +func NewCancelOrderLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CancelOrderLogic { + return &CancelOrderLogic{ + ctx: ctx, + svcCtx: svcCtx, + Logger: logx.WithContext(ctx), + } +} + +// CancelOrder 取消訂單 +func (l *CancelOrderLogic) CancelOrder(in *tweeting.CancelOrderReq) (*tweeting.OKResp, error) { + // todo: add your logic here and delete this line + + return &tweeting.OKResp{}, nil +} diff --git a/internal/logic/orderservice/create_order_logic.go b/internal/logic/orderservice/create_order_logic.go new file mode 100644 index 0000000..9ae8e56 --- /dev/null +++ b/internal/logic/orderservice/create_order_logic.go @@ -0,0 +1,31 @@ +package orderservicelogic + +import ( + "context" + + "app-cloudep-order-server/gen_result/pb/tweeting" + "app-cloudep-order-server/internal/svc" + + "github.com/zeromicro/go-zero/core/logx" +) + +type CreateOrderLogic struct { + ctx context.Context + svcCtx *svc.ServiceContext + logx.Logger +} + +func NewCreateOrderLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateOrderLogic { + return &CreateOrderLogic{ + ctx: ctx, + svcCtx: svcCtx, + Logger: logx.WithContext(ctx), + } +} + +// CreateOrder 建立訂單 +func (l *CreateOrderLogic) CreateOrder(in *tweeting.CreateOrderReq) (*tweeting.CreateOrderResp, error) { + // todo: add your logic here and delete this line + + return &tweeting.CreateOrderResp{}, nil +} diff --git a/internal/logic/orderservice/delete_order_logic.go b/internal/logic/orderservice/delete_order_logic.go new file mode 100644 index 0000000..f66b6cb --- /dev/null +++ b/internal/logic/orderservice/delete_order_logic.go @@ -0,0 +1,31 @@ +package orderservicelogic + +import ( + "context" + + "app-cloudep-order-server/gen_result/pb/tweeting" + "app-cloudep-order-server/internal/svc" + + "github.com/zeromicro/go-zero/core/logx" +) + +type DeleteOrderLogic struct { + ctx context.Context + svcCtx *svc.ServiceContext + logx.Logger +} + +func NewDeleteOrderLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DeleteOrderLogic { + return &DeleteOrderLogic{ + ctx: ctx, + svcCtx: svcCtx, + Logger: logx.WithContext(ctx), + } +} + +// DeleteOrder 刪除訂單(軟刪除) +func (l *DeleteOrderLogic) DeleteOrder(in *tweeting.DeleteOrderReq) (*tweeting.OKResp, error) { + // todo: add your logic here and delete this line + + return &tweeting.OKResp{}, nil +} diff --git a/internal/logic/orderservice/get_order_logic.go b/internal/logic/orderservice/get_order_logic.go new file mode 100644 index 0000000..9260f0f --- /dev/null +++ b/internal/logic/orderservice/get_order_logic.go @@ -0,0 +1,31 @@ +package orderservicelogic + +import ( + "context" + + "app-cloudep-order-server/gen_result/pb/tweeting" + "app-cloudep-order-server/internal/svc" + + "github.com/zeromicro/go-zero/core/logx" +) + +type GetOrderLogic struct { + ctx context.Context + svcCtx *svc.ServiceContext + logx.Logger +} + +func NewGetOrderLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetOrderLogic { + return &GetOrderLogic{ + ctx: ctx, + svcCtx: svcCtx, + Logger: logx.WithContext(ctx), + } +} + +// GetOrder 取得訂單詳情 +func (l *GetOrderLogic) GetOrder(in *tweeting.GetOrderReq) (*tweeting.GetOrderResp, error) { + // todo: add your logic here and delete this line + + return &tweeting.GetOrderResp{}, nil +} diff --git a/internal/logic/orderservice/list_order_logic.go b/internal/logic/orderservice/list_order_logic.go new file mode 100644 index 0000000..67e4cdb --- /dev/null +++ b/internal/logic/orderservice/list_order_logic.go @@ -0,0 +1,31 @@ +package orderservicelogic + +import ( + "context" + + "app-cloudep-order-server/gen_result/pb/tweeting" + "app-cloudep-order-server/internal/svc" + + "github.com/zeromicro/go-zero/core/logx" +) + +type ListOrderLogic struct { + ctx context.Context + svcCtx *svc.ServiceContext + logx.Logger +} + +func NewListOrderLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ListOrderLogic { + return &ListOrderLogic{ + ctx: ctx, + svcCtx: svcCtx, + Logger: logx.WithContext(ctx), + } +} + +// ListOrder 取得訂單列表 +func (l *ListOrderLogic) ListOrder(in *tweeting.ListOrderReq) (*tweeting.ListOrderResp, error) { + // todo: add your logic here and delete this line + + return &tweeting.ListOrderResp{}, nil +} diff --git a/internal/logic/orderservice/modify_order_logic.go b/internal/logic/orderservice/modify_order_logic.go new file mode 100644 index 0000000..a838866 --- /dev/null +++ b/internal/logic/orderservice/modify_order_logic.go @@ -0,0 +1,31 @@ +package orderservicelogic + +import ( + "context" + + "app-cloudep-order-server/gen_result/pb/tweeting" + "app-cloudep-order-server/internal/svc" + + "github.com/zeromicro/go-zero/core/logx" +) + +type ModifyOrderLogic struct { + ctx context.Context + svcCtx *svc.ServiceContext + logx.Logger +} + +func NewModifyOrderLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ModifyOrderLogic { + return &ModifyOrderLogic{ + ctx: ctx, + svcCtx: svcCtx, + Logger: logx.WithContext(ctx), + } +} + +// ModifyOrder 修改訂單 +func (l *ModifyOrderLogic) ModifyOrder(in *tweeting.ModifyOrderReq) (*tweeting.OKResp, error) { + // todo: add your logic here and delete this line + + return &tweeting.OKResp{}, nil +} diff --git a/internal/logic/orderservice/modify_order_status_logic.go b/internal/logic/orderservice/modify_order_status_logic.go new file mode 100644 index 0000000..4a2581b --- /dev/null +++ b/internal/logic/orderservice/modify_order_status_logic.go @@ -0,0 +1,31 @@ +package orderservicelogic + +import ( + "context" + + "app-cloudep-order-server/gen_result/pb/tweeting" + "app-cloudep-order-server/internal/svc" + + "github.com/zeromicro/go-zero/core/logx" +) + +type ModifyOrderStatusLogic struct { + ctx context.Context + svcCtx *svc.ServiceContext + logx.Logger +} + +func NewModifyOrderStatusLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ModifyOrderStatusLogic { + return &ModifyOrderStatusLogic{ + ctx: ctx, + svcCtx: svcCtx, + Logger: logx.WithContext(ctx), + } +} + +// ModifyOrderStatus 修改訂單狀態 +func (l *ModifyOrderStatusLogic) ModifyOrderStatus(in *tweeting.ModifyOrderStatusReq) (*tweeting.OKResp, error) { + // todo: add your logic here and delete this line + + return &tweeting.OKResp{}, nil +} diff --git a/internal/logic/orderservice/order_status_timeout_logic.go b/internal/logic/orderservice/order_status_timeout_logic.go new file mode 100644 index 0000000..67ce9c6 --- /dev/null +++ b/internal/logic/orderservice/order_status_timeout_logic.go @@ -0,0 +1,31 @@ +package orderservicelogic + +import ( + "context" + + "app-cloudep-order-server/gen_result/pb/tweeting" + "app-cloudep-order-server/internal/svc" + + "github.com/zeromicro/go-zero/core/logx" +) + +type OrderStatusTimeoutLogic struct { + ctx context.Context + svcCtx *svc.ServiceContext + logx.Logger +} + +func NewOrderStatusTimeoutLogic(ctx context.Context, svcCtx *svc.ServiceContext) *OrderStatusTimeoutLogic { + return &OrderStatusTimeoutLogic{ + ctx: ctx, + svcCtx: svcCtx, + Logger: logx.WithContext(ctx), + } +} + +// OrderStatusTimeout 訂單超時任務/cron/order-status/timeout +func (l *OrderStatusTimeoutLogic) OrderStatusTimeout(in *tweeting.OrderStatusTimeoutReq) (*tweeting.OKResp, error) { + // todo: add your logic here and delete this line + + return &tweeting.OKResp{}, nil +} diff --git a/internal/mock/model/order_model.go b/internal/mock/model/order_model.go new file mode 100644 index 0000000..fb24d12 --- /dev/null +++ b/internal/mock/model/order_model.go @@ -0,0 +1,101 @@ +// Code generated by MockGen. DO NOT EDIT. +// Source: ./internal/model/mongo/order_model.go +// +// Generated by this command: +// +// mockgen -source=./internal/model/mongo/order_model.go -destination=./internal/mock/model/order_model.go -package=mock +// + +// Package mock is a generated GoMock package. +package mock + +import ( + mongo "app-cloudep-order-server/internal/model/mongo" + context "context" + reflect "reflect" + + mongo0 "go.mongodb.org/mongo-driver/mongo" + gomock "go.uber.org/mock/gomock" +) + +// MockOrderModel is a mock of OrderModel interface. +type MockOrderModel struct { + ctrl *gomock.Controller + recorder *MockOrderModelMockRecorder +} + +// MockOrderModelMockRecorder is the mock recorder for MockOrderModel. +type MockOrderModelMockRecorder struct { + mock *MockOrderModel +} + +// NewMockOrderModel creates a new mock instance. +func NewMockOrderModel(ctrl *gomock.Controller) *MockOrderModel { + mock := &MockOrderModel{ctrl: ctrl} + mock.recorder = &MockOrderModelMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockOrderModel) EXPECT() *MockOrderModelMockRecorder { + return m.recorder +} + +// Delete mocks base method. +func (m *MockOrderModel) Delete(ctx context.Context, id string) (int64, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Delete", ctx, id) + ret0, _ := ret[0].(int64) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// Delete indicates an expected call of Delete. +func (mr *MockOrderModelMockRecorder) Delete(ctx, id any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Delete", reflect.TypeOf((*MockOrderModel)(nil).Delete), ctx, id) +} + +// FindOne mocks base method. +func (m *MockOrderModel) FindOne(ctx context.Context, id string) (*mongo.Order, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "FindOne", ctx, id) + ret0, _ := ret[0].(*mongo.Order) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// FindOne indicates an expected call of FindOne. +func (mr *MockOrderModelMockRecorder) FindOne(ctx, id any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "FindOne", reflect.TypeOf((*MockOrderModel)(nil).FindOne), ctx, id) +} + +// Insert mocks base method. +func (m *MockOrderModel) Insert(ctx context.Context, data *mongo.Order) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Insert", ctx, data) + ret0, _ := ret[0].(error) + return ret0 +} + +// Insert indicates an expected call of Insert. +func (mr *MockOrderModelMockRecorder) Insert(ctx, data any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Insert", reflect.TypeOf((*MockOrderModel)(nil).Insert), ctx, data) +} + +// Update mocks base method. +func (m *MockOrderModel) Update(ctx context.Context, data *mongo.Order) (*mongo0.UpdateResult, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Update", ctx, data) + ret0, _ := ret[0].(*mongo0.UpdateResult) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// Update indicates an expected call of Update. +func (mr *MockOrderModelMockRecorder) Update(ctx, data any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Update", reflect.TypeOf((*MockOrderModel)(nil).Update), ctx, data) +} diff --git a/internal/mock/model/order_model_gen.go b/internal/mock/model/order_model_gen.go new file mode 100644 index 0000000..f3fb4da --- /dev/null +++ b/internal/mock/model/order_model_gen.go @@ -0,0 +1,101 @@ +// Code generated by MockGen. DO NOT EDIT. +// Source: ./internal/model/mongo/order_model_gen.go +// +// Generated by this command: +// +// mockgen -source=./internal/model/mongo/order_model_gen.go -destination=./internal/mock/model/order_model_gen.go -package=mock +// + +// Package mock is a generated GoMock package. +package mock + +import ( + mongo "app-cloudep-order-server/internal/model/mongo" + context "context" + reflect "reflect" + + mongo0 "go.mongodb.org/mongo-driver/mongo" + gomock "go.uber.org/mock/gomock" +) + +// MockorderModel is a mock of orderModel interface. +type MockorderModel struct { + ctrl *gomock.Controller + recorder *MockorderModelMockRecorder +} + +// MockorderModelMockRecorder is the mock recorder for MockorderModel. +type MockorderModelMockRecorder struct { + mock *MockorderModel +} + +// NewMockorderModel creates a new mock instance. +func NewMockorderModel(ctrl *gomock.Controller) *MockorderModel { + mock := &MockorderModel{ctrl: ctrl} + mock.recorder = &MockorderModelMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockorderModel) EXPECT() *MockorderModelMockRecorder { + return m.recorder +} + +// Delete mocks base method. +func (m *MockorderModel) Delete(ctx context.Context, id string) (int64, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Delete", ctx, id) + ret0, _ := ret[0].(int64) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// Delete indicates an expected call of Delete. +func (mr *MockorderModelMockRecorder) Delete(ctx, id any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Delete", reflect.TypeOf((*MockorderModel)(nil).Delete), ctx, id) +} + +// FindOne mocks base method. +func (m *MockorderModel) FindOne(ctx context.Context, id string) (*mongo.Order, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "FindOne", ctx, id) + ret0, _ := ret[0].(*mongo.Order) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// FindOne indicates an expected call of FindOne. +func (mr *MockorderModelMockRecorder) FindOne(ctx, id any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "FindOne", reflect.TypeOf((*MockorderModel)(nil).FindOne), ctx, id) +} + +// Insert mocks base method. +func (m *MockorderModel) Insert(ctx context.Context, data *mongo.Order) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Insert", ctx, data) + ret0, _ := ret[0].(error) + return ret0 +} + +// Insert indicates an expected call of Insert. +func (mr *MockorderModelMockRecorder) Insert(ctx, data any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Insert", reflect.TypeOf((*MockorderModel)(nil).Insert), ctx, data) +} + +// Update mocks base method. +func (m *MockorderModel) Update(ctx context.Context, data *mongo.Order) (*mongo0.UpdateResult, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Update", ctx, data) + ret0, _ := ret[0].(*mongo0.UpdateResult) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// Update indicates an expected call of Update. +func (mr *MockorderModelMockRecorder) Update(ctx, data any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Update", reflect.TypeOf((*MockorderModel)(nil).Update), ctx, data) +} diff --git a/internal/model/mongo/error.go b/internal/model/mongo/error.go new file mode 100644 index 0000000..27d9244 --- /dev/null +++ b/internal/model/mongo/error.go @@ -0,0 +1,12 @@ +package model + +import ( + "errors" + + "github.com/zeromicro/go-zero/core/stores/mon" +) + +var ( + ErrNotFound = mon.ErrNotFound + ErrInvalidObjectId = errors.New("invalid objectId") +) diff --git a/internal/model/mongo/order_model.go b/internal/model/mongo/order_model.go new file mode 100644 index 0000000..73a14ba --- /dev/null +++ b/internal/model/mongo/order_model.go @@ -0,0 +1,25 @@ +package model + +import "github.com/zeromicro/go-zero/core/stores/mon" + +var _ OrderModel = (*customOrderModel)(nil) + +type ( + // OrderModel is an interface to be customized, add more methods here, + // and implement the added methods in customOrderModel. + OrderModel interface { + orderModel + } + + customOrderModel struct { + *defaultOrderModel + } +) + +// NewOrderModel returns a model for the mongo. +func NewOrderModel(url, db, collection string) OrderModel { + conn := mon.MustNewModel(url, db, collection) + return &customOrderModel{ + defaultOrderModel: newDefaultOrderModel(conn), + } +} diff --git a/internal/model/mongo/order_model_gen.go b/internal/model/mongo/order_model_gen.go new file mode 100644 index 0000000..0d9f599 --- /dev/null +++ b/internal/model/mongo/order_model_gen.go @@ -0,0 +1,74 @@ +// Code generated by goctl. DO NOT EDIT. +package model + +import ( + "context" + "time" + + "github.com/zeromicro/go-zero/core/stores/mon" + "go.mongodb.org/mongo-driver/bson" + "go.mongodb.org/mongo-driver/bson/primitive" + "go.mongodb.org/mongo-driver/mongo" +) + +type orderModel interface { + Insert(ctx context.Context, data *Order) error + FindOne(ctx context.Context, id string) (*Order, error) + Update(ctx context.Context, data *Order) (*mongo.UpdateResult, error) + Delete(ctx context.Context, id string) (int64, error) +} + +type defaultOrderModel struct { + conn *mon.Model +} + +func newDefaultOrderModel(conn *mon.Model) *defaultOrderModel { + return &defaultOrderModel{conn: conn} +} + +func (m *defaultOrderModel) Insert(ctx context.Context, data *Order) error { + if data.ID.IsZero() { + data.ID = primitive.NewObjectID() + data.CreateAt = time.Now() + data.UpdateAt = time.Now() + } + + _, err := m.conn.InsertOne(ctx, data) + return err +} + +func (m *defaultOrderModel) FindOne(ctx context.Context, id string) (*Order, error) { + oid, err := primitive.ObjectIDFromHex(id) + if err != nil { + return nil, ErrInvalidObjectId + } + + var data Order + + err = m.conn.FindOne(ctx, &data, bson.M{"_id": oid}) + switch err { + case nil: + return &data, nil + case mon.ErrNotFound: + return nil, ErrNotFound + default: + return nil, err + } +} + +func (m *defaultOrderModel) Update(ctx context.Context, data *Order) (*mongo.UpdateResult, error) { + data.UpdateAt = time.Now() + + res, err := m.conn.UpdateOne(ctx, bson.M{"_id": data.ID}, bson.M{"$set": data}) + return res, err +} + +func (m *defaultOrderModel) Delete(ctx context.Context, id string) (int64, error) { + oid, err := primitive.ObjectIDFromHex(id) + if err != nil { + return 0, ErrInvalidObjectId + } + + res, err := m.conn.DeleteOne(ctx, bson.M{"_id": oid}) + return res, err +} diff --git a/internal/model/mongo/order_types.go b/internal/model/mongo/order_types.go new file mode 100644 index 0000000..30198e9 --- /dev/null +++ b/internal/model/mongo/order_types.go @@ -0,0 +1,14 @@ +package model + +import ( + "time" + + "go.mongodb.org/mongo-driver/bson/primitive" +) + +type Order struct { + ID primitive.ObjectID `bson:"_id,omitempty" json:"id,omitempty"` + // TODO: Fill your own fields + UpdateAt time.Time `bson:"updateAt,omitempty" json:"updateAt,omitempty"` + CreateAt time.Time `bson:"createAt,omitempty" json:"createAt,omitempty"` +} diff --git a/internal/server/orderservice/order_service_server.go b/internal/server/orderservice/order_service_server.go new file mode 100644 index 0000000..7014e9d --- /dev/null +++ b/internal/server/orderservice/order_service_server.go @@ -0,0 +1,71 @@ +// Code generated by goctl. DO NOT EDIT. +// Source: order.proto + +package server + +import ( + "context" + + "app-cloudep-order-server/gen_result/pb/tweeting" + "app-cloudep-order-server/internal/logic/orderservice" + "app-cloudep-order-server/internal/svc" +) + +type OrderServiceServer struct { + svcCtx *svc.ServiceContext + tweeting.UnimplementedOrderServiceServer +} + +func NewOrderServiceServer(svcCtx *svc.ServiceContext) *OrderServiceServer { + return &OrderServiceServer{ + svcCtx: svcCtx, + } +} + +// CreateOrder 建立訂單 +func (s *OrderServiceServer) CreateOrder(ctx context.Context, in *tweeting.CreateOrderReq) (*tweeting.CreateOrderResp, error) { + l := orderservicelogic.NewCreateOrderLogic(ctx, s.svcCtx) + return l.CreateOrder(in) +} + +// CancelOrder 取消訂單 +func (s *OrderServiceServer) CancelOrder(ctx context.Context, in *tweeting.CancelOrderReq) (*tweeting.OKResp, error) { + l := orderservicelogic.NewCancelOrderLogic(ctx, s.svcCtx) + return l.CancelOrder(in) +} + +// ModifyOrder 修改訂單 +func (s *OrderServiceServer) ModifyOrder(ctx context.Context, in *tweeting.ModifyOrderReq) (*tweeting.OKResp, error) { + l := orderservicelogic.NewModifyOrderLogic(ctx, s.svcCtx) + return l.ModifyOrder(in) +} + +// ModifyOrderStatus 修改訂單狀態 +func (s *OrderServiceServer) ModifyOrderStatus(ctx context.Context, in *tweeting.ModifyOrderStatusReq) (*tweeting.OKResp, error) { + l := orderservicelogic.NewModifyOrderStatusLogic(ctx, s.svcCtx) + return l.ModifyOrderStatus(in) +} + +// DeleteOrder 刪除訂單(軟刪除) +func (s *OrderServiceServer) DeleteOrder(ctx context.Context, in *tweeting.DeleteOrderReq) (*tweeting.OKResp, error) { + l := orderservicelogic.NewDeleteOrderLogic(ctx, s.svcCtx) + return l.DeleteOrder(in) +} + +// GetOrder 取得訂單詳情 +func (s *OrderServiceServer) GetOrder(ctx context.Context, in *tweeting.GetOrderReq) (*tweeting.GetOrderResp, error) { + l := orderservicelogic.NewGetOrderLogic(ctx, s.svcCtx) + return l.GetOrder(in) +} + +// ListOrder 取得訂單列表 +func (s *OrderServiceServer) ListOrder(ctx context.Context, in *tweeting.ListOrderReq) (*tweeting.ListOrderResp, error) { + l := orderservicelogic.NewListOrderLogic(ctx, s.svcCtx) + return l.ListOrder(in) +} + +// OrderStatusTimeout 訂單超時任務/cron/order-status/timeout +func (s *OrderServiceServer) OrderStatusTimeout(ctx context.Context, in *tweeting.OrderStatusTimeoutReq) (*tweeting.OKResp, error) { + l := orderservicelogic.NewOrderStatusTimeoutLogic(ctx, s.svcCtx) + return l.OrderStatusTimeout(in) +} diff --git a/internal/svc/service_context.go b/internal/svc/service_context.go new file mode 100644 index 0000000..53b091b --- /dev/null +++ b/internal/svc/service_context.go @@ -0,0 +1,13 @@ +package svc + +import "app-cloudep-order-server/internal/config" + +type ServiceContext struct { + Config config.Config +} + +func NewServiceContext(c config.Config) *ServiceContext { + return &ServiceContext{ + Config: c, + } +} diff --git a/order.go b/order.go new file mode 100644 index 0000000..0876820 --- /dev/null +++ b/order.go @@ -0,0 +1,39 @@ +package main + +import ( + "flag" + "fmt" + + "app-cloudep-order-server/gen_result/pb/tweeting" + "app-cloudep-order-server/internal/config" + orderserviceServer "app-cloudep-order-server/internal/server/orderservice" + "app-cloudep-order-server/internal/svc" + + "github.com/zeromicro/go-zero/core/conf" + "github.com/zeromicro/go-zero/core/service" + "github.com/zeromicro/go-zero/zrpc" + "google.golang.org/grpc" + "google.golang.org/grpc/reflection" +) + +var configFile = flag.String("f", "etc/order.yaml", "the config file") + +func main() { + flag.Parse() + + var c config.Config + conf.MustLoad(*configFile, &c) + ctx := svc.NewServiceContext(c) + + s := zrpc.MustNewServer(c.RpcServerConf, func(grpcServer *grpc.Server) { + tweeting.RegisterOrderServiceServer(grpcServer, orderserviceServer.NewOrderServiceServer(ctx)) + + if c.Mode == service.DevMode || c.Mode == service.TestMode { + reflection.Register(grpcServer) + } + }) + defer s.Stop() + + fmt.Printf("Starting rpc server at %s...\n", c.ListenOn) + s.Start() +}