mirror of
https://github.com/zhufuyi/sponge.git
synced 2025-09-26 20:51:14 +08:00
324 lines
12 KiB
Plaintext
324 lines
12 KiB
Plaintext
package service
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"math"
|
|
"strings"
|
|
"time"
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
"github.com/go-dev-frame/sponge/pkg/copier"
|
|
"github.com/go-dev-frame/sponge/pkg/sgorm/query"
|
|
"github.com/go-dev-frame/sponge/pkg/grpc/interceptor"
|
|
"github.com/go-dev-frame/sponge/pkg/logger"
|
|
|
|
serverNameExampleV1 "github.com/go-dev-frame/sponge/api/serverNameExample/v1"
|
|
"github.com/go-dev-frame/sponge/internal/cache"
|
|
"github.com/go-dev-frame/sponge/internal/dao"
|
|
"github.com/go-dev-frame/sponge/internal/database"
|
|
"github.com/go-dev-frame/sponge/internal/ecode"
|
|
"github.com/go-dev-frame/sponge/internal/model"
|
|
)
|
|
|
|
func init() {
|
|
registerFns = append(registerFns, func(server *grpc.Server) {
|
|
serverNameExampleV1.RegisterUserExampleServer(server, NewUserExampleServer()) // register service to the rpc service
|
|
})
|
|
}
|
|
|
|
var _ serverNameExampleV1.UserExampleServer = (*userExample)(nil)
|
|
var _ time.Time
|
|
|
|
type userExample struct {
|
|
serverNameExampleV1.UnimplementedUserExampleServer
|
|
|
|
iDao dao.UserExampleDao
|
|
}
|
|
|
|
// NewUserExampleServer create a new service
|
|
func NewUserExampleServer() serverNameExampleV1.UserExampleServer {
|
|
return &userExample{
|
|
iDao: dao.NewUserExampleDao(
|
|
database.GetDB(), // todo show db driver name here
|
|
cache.NewUserExampleCache(database.GetCacheType()),
|
|
),
|
|
}
|
|
}
|
|
|
|
// Create a new userExample
|
|
func (s *userExample) Create(ctx context.Context, req *serverNameExampleV1.CreateUserExampleRequest) (*serverNameExampleV1.CreateUserExampleReply, error) {
|
|
err := req.Validate()
|
|
if err != nil {
|
|
logger.Warn("req.Validate error", logger.Err(err), logger.Any("req", req), interceptor.ServerCtxRequestIDField(ctx))
|
|
return nil, ecode.StatusInvalidParams.Err()
|
|
}
|
|
ctx = interceptor.WrapServerCtx(ctx)
|
|
|
|
record := &model.UserExample{}
|
|
err = copier.Copy(record, req)
|
|
if err != nil {
|
|
return nil, ecode.StatusCreateUserExample.Err()
|
|
}
|
|
// Note: if copier.Copy cannot assign a value to a field, add it here
|
|
|
|
err = s.iDao.Create(ctx, record)
|
|
if err != nil {
|
|
logger.Error("Create error", logger.Err(err), logger.Any("userExample", record), interceptor.ServerCtxRequestIDField(ctx))
|
|
return nil, ecode.StatusInternalServerError.ToRPCErr()
|
|
}
|
|
|
|
return &serverNameExampleV1.CreateUserExampleReply{Id: record.ID}, nil
|
|
}
|
|
|
|
// DeleteByID delete a userExample by id
|
|
func (s *userExample) DeleteByID(ctx context.Context, req *serverNameExampleV1.DeleteUserExampleByIDRequest) (*serverNameExampleV1.DeleteUserExampleByIDReply, error) {
|
|
err := req.Validate()
|
|
if err != nil {
|
|
logger.Warn("req.Validate error", logger.Err(err), logger.Any("req", req), interceptor.ServerCtxRequestIDField(ctx))
|
|
return nil, ecode.StatusInvalidParams.Err()
|
|
}
|
|
ctx = interceptor.WrapServerCtx(ctx)
|
|
|
|
err = s.iDao.DeleteByID(ctx, req.Id)
|
|
if err != nil {
|
|
logger.Error("DeleteByID error", logger.Err(err), logger.Any("id", req.Id), interceptor.ServerCtxRequestIDField(ctx))
|
|
return nil, ecode.StatusInternalServerError.ToRPCErr()
|
|
}
|
|
|
|
return &serverNameExampleV1.DeleteUserExampleByIDReply{}, nil
|
|
}
|
|
|
|
// UpdateByID update a userExample by id
|
|
func (s *userExample) UpdateByID(ctx context.Context, req *serverNameExampleV1.UpdateUserExampleByIDRequest) (*serverNameExampleV1.UpdateUserExampleByIDReply, error) {
|
|
err := req.Validate()
|
|
if err != nil {
|
|
logger.Warn("req.Validate error", logger.Err(err), logger.Any("req", req), interceptor.ServerCtxRequestIDField(ctx))
|
|
return nil, ecode.StatusInvalidParams.Err()
|
|
}
|
|
ctx = interceptor.WrapServerCtx(ctx)
|
|
|
|
record := &model.UserExample{}
|
|
err = copier.Copy(record, req)
|
|
if err != nil {
|
|
return nil, ecode.StatusUpdateByIDUserExample.Err()
|
|
}
|
|
// Note: if copier.Copy cannot assign a value to a field, add it here
|
|
record.ID = req.Id
|
|
|
|
err = s.iDao.UpdateByID(ctx, record)
|
|
if err != nil {
|
|
logger.Error("UpdateByID error", logger.Err(err), logger.Any("userExample", record), interceptor.ServerCtxRequestIDField(ctx))
|
|
return nil, ecode.StatusInternalServerError.ToRPCErr()
|
|
}
|
|
|
|
return &serverNameExampleV1.UpdateUserExampleByIDReply{}, nil
|
|
}
|
|
|
|
// GetByID get a userExample by id
|
|
func (s *userExample) GetByID(ctx context.Context, req *serverNameExampleV1.GetUserExampleByIDRequest) (*serverNameExampleV1.GetUserExampleByIDReply, error) {
|
|
err := req.Validate()
|
|
if err != nil {
|
|
logger.Warn("req.Validate error", logger.Err(err), logger.Any("req", req), interceptor.ServerCtxRequestIDField(ctx))
|
|
return nil, ecode.StatusInvalidParams.Err()
|
|
}
|
|
ctx = interceptor.WrapServerCtx(ctx)
|
|
|
|
record, err := s.iDao.GetByID(ctx, req.Id)
|
|
if err != nil {
|
|
if errors.Is(err, database.ErrRecordNotFound) {
|
|
logger.Warn("GetByID error", logger.Err(err), logger.Any("id", req.Id), interceptor.ServerCtxRequestIDField(ctx))
|
|
return nil, ecode.StatusNotFound.Err()
|
|
}
|
|
logger.Error("GetByID error", logger.Err(err), logger.Any("id", req.Id), interceptor.ServerCtxRequestIDField(ctx))
|
|
return nil, ecode.StatusInternalServerError.ToRPCErr()
|
|
}
|
|
|
|
data, err := convertUserExample(record)
|
|
if err != nil {
|
|
logger.Warn("convertUserExample error", logger.Err(err), logger.Any("userExample", record), interceptor.ServerCtxRequestIDField(ctx))
|
|
return nil, ecode.StatusGetByIDUserExample.Err()
|
|
}
|
|
|
|
return &serverNameExampleV1.GetUserExampleByIDReply{UserExample: data}, nil
|
|
}
|
|
|
|
// List get a paginated list of userExamples by custom conditions
|
|
func (s *userExample) List(ctx context.Context, req *serverNameExampleV1.ListUserExampleRequest) (*serverNameExampleV1.ListUserExampleReply, error) {
|
|
err := req.Validate()
|
|
if err != nil {
|
|
logger.Warn("req.Validate error", logger.Err(err), logger.Any("req", req), interceptor.ServerCtxRequestIDField(ctx))
|
|
return nil, ecode.StatusInvalidParams.Err()
|
|
}
|
|
ctx = interceptor.WrapServerCtx(ctx)
|
|
|
|
params := &query.Params{}
|
|
err = copier.Copy(params, req.Params)
|
|
if err != nil {
|
|
return nil, ecode.StatusListUserExample.Err()
|
|
}
|
|
// Note: if copier.Copy cannot assign a value to a field, add it here
|
|
|
|
records, total, err := s.iDao.GetByColumns(ctx, params)
|
|
if err != nil {
|
|
if strings.Contains(err.Error(), "query params error:") {
|
|
logger.Warn("GetByColumns error", logger.Err(err), logger.Any("params", params), interceptor.ServerCtxRequestIDField(ctx))
|
|
return nil, ecode.StatusInvalidParams.Err()
|
|
}
|
|
logger.Error("GetByColumns error", logger.Err(err), logger.Any("params", params), interceptor.ServerCtxRequestIDField(ctx))
|
|
return nil, ecode.StatusInternalServerError.ToRPCErr()
|
|
}
|
|
|
|
userExamples := []*serverNameExampleV1.UserExample{}
|
|
for _, record := range records {
|
|
data, err := convertUserExample(record)
|
|
if err != nil {
|
|
logger.Warn("convertUserExample error", logger.Err(err), logger.Any("id", record.ID), interceptor.ServerCtxRequestIDField(ctx))
|
|
continue
|
|
}
|
|
userExamples = append(userExamples, data)
|
|
}
|
|
|
|
return &serverNameExampleV1.ListUserExampleReply{
|
|
Total: total,
|
|
UserExamples: userExamples,
|
|
}, nil
|
|
}
|
|
|
|
// DeleteByIDs batch delete userExample by ids
|
|
func (s *userExample) DeleteByIDs(ctx context.Context, req *serverNameExampleV1.DeleteUserExampleByIDsRequest) (*serverNameExampleV1.DeleteUserExampleByIDsReply, error) {
|
|
err := req.Validate()
|
|
if err != nil {
|
|
logger.Warn("req.Validate error", logger.Err(err), logger.Any("req", req), interceptor.ServerCtxRequestIDField(ctx))
|
|
return nil, ecode.StatusInvalidParams.Err()
|
|
}
|
|
ctx = interceptor.WrapServerCtx(ctx)
|
|
|
|
err = s.iDao.DeleteByIDs(ctx, req.Ids)
|
|
if err != nil {
|
|
logger.Error("DeleteByID error", logger.Err(err), logger.Any("ids", req.Ids), interceptor.ServerCtxRequestIDField(ctx))
|
|
return nil, ecode.StatusInternalServerError.ToRPCErr()
|
|
}
|
|
|
|
return &serverNameExampleV1.DeleteUserExampleByIDsReply{}, nil
|
|
}
|
|
|
|
// GetByCondition get a userExample by custom condition
|
|
func (s *userExample) GetByCondition(ctx context.Context, req *serverNameExampleV1.GetUserExampleByConditionRequest) (*serverNameExampleV1.GetUserExampleByConditionReply, error) {
|
|
err := req.Validate()
|
|
if err != nil {
|
|
logger.Warn("req.Validate error", logger.Err(err), logger.Any("req", req), interceptor.ServerCtxRequestIDField(ctx))
|
|
return nil, ecode.StatusInvalidParams.Err()
|
|
}
|
|
ctx = interceptor.WrapServerCtx(ctx)
|
|
|
|
conditions := &query.Conditions{}
|
|
for _, v := range req.Conditions.GetColumns() {
|
|
column := query.Column{}
|
|
_ = copier.Copy(&column, v)
|
|
conditions.Columns = append(conditions.Columns, column)
|
|
}
|
|
err = conditions.CheckValid()
|
|
if err != nil {
|
|
logger.Warn("Parameters error", logger.Err(err), logger.Any("conditions", conditions), interceptor.ServerCtxRequestIDField(ctx))
|
|
return nil, ecode.StatusInvalidParams.Err()
|
|
}
|
|
|
|
record, err := s.iDao.GetByCondition(ctx, conditions)
|
|
if err != nil {
|
|
if errors.Is(err, database.ErrRecordNotFound) {
|
|
logger.Warn("GetByCondition error", logger.Err(err), logger.Any("req", req), interceptor.ServerCtxRequestIDField(ctx))
|
|
return nil, ecode.StatusNotFound.Err()
|
|
}
|
|
logger.Error("GetByCondition error", logger.Err(err), logger.Any("req", req), interceptor.ServerCtxRequestIDField(ctx))
|
|
return nil, ecode.StatusInternalServerError.ToRPCErr()
|
|
}
|
|
|
|
data, err := convertUserExample(record)
|
|
if err != nil {
|
|
logger.Warn("convertUserExample error", logger.Err(err), logger.Any("userExample", record), interceptor.ServerCtxRequestIDField(ctx))
|
|
return nil, ecode.StatusGetByConditionUserExample.Err()
|
|
}
|
|
|
|
return &serverNameExampleV1.GetUserExampleByConditionReply{
|
|
UserExample: data,
|
|
}, nil
|
|
}
|
|
|
|
// ListByIDs batch get userExample by ids
|
|
func (s *userExample) ListByIDs(ctx context.Context, req *serverNameExampleV1.ListUserExampleByIDsRequest) (*serverNameExampleV1.ListUserExampleByIDsReply, error) {
|
|
err := req.Validate()
|
|
if err != nil {
|
|
logger.Warn("req.Validate error", logger.Err(err), logger.Any("req", req), interceptor.ServerCtxRequestIDField(ctx))
|
|
return nil, ecode.StatusInvalidParams.Err()
|
|
}
|
|
ctx = interceptor.WrapServerCtx(ctx)
|
|
|
|
userExampleMap, err := s.iDao.GetByIDs(ctx, req.Ids)
|
|
if err != nil {
|
|
logger.Error("GetByIDs error", logger.Err(err), logger.Any("ids", req.Ids), interceptor.ServerCtxRequestIDField(ctx))
|
|
return nil, ecode.StatusInternalServerError.ToRPCErr()
|
|
}
|
|
|
|
userExamples := []*serverNameExampleV1.UserExample{}
|
|
for _, id := range req.Ids {
|
|
if v, ok := userExampleMap[id]; ok {
|
|
record, err := convertUserExample(v)
|
|
if err != nil {
|
|
logger.Warn("convertUserExample error", logger.Err(err), logger.Any("userExample", v), interceptor.ServerCtxRequestIDField(ctx))
|
|
return nil, ecode.StatusInternalServerError.ToRPCErr()
|
|
}
|
|
userExamples = append(userExamples, record)
|
|
}
|
|
}
|
|
|
|
return &serverNameExampleV1.ListUserExampleByIDsReply{UserExamples: userExamples}, nil
|
|
}
|
|
|
|
// ListByLastID get a paginated list of userExamples by last id
|
|
func (s *userExample) ListByLastID(ctx context.Context, req *serverNameExampleV1.ListUserExampleByLastIDRequest) (*serverNameExampleV1.ListUserExampleByLastIDReply, error) {
|
|
err := req.Validate()
|
|
if err != nil {
|
|
logger.Warn("req.Validate error", logger.Err(err), logger.Any("req", req), interceptor.CtxRequestIDField(ctx))
|
|
return nil, ecode.StatusInvalidParams.Err()
|
|
}
|
|
if req.LastID == 0 {
|
|
req.LastID = math.MaxInt32
|
|
}
|
|
if req.Limit == 0 {
|
|
req.Limit = 10
|
|
}
|
|
|
|
records, err := s.iDao.GetByLastID(ctx, req.LastID, int(req.Limit), req.Sort)
|
|
if err != nil {
|
|
logger.Error("ListByLastID error", logger.Err(err), interceptor.CtxRequestIDField(ctx))
|
|
return nil, ecode.StatusInternalServerError.ToRPCErr()
|
|
}
|
|
|
|
userExamples := []*serverNameExampleV1.UserExample{}
|
|
for _, record := range records {
|
|
data, err := convertUserExample(record)
|
|
if err != nil {
|
|
logger.Warn("convertUserExample error", logger.Err(err), logger.Any("id", record.ID), interceptor.ServerCtxRequestIDField(ctx))
|
|
continue
|
|
}
|
|
userExamples = append(userExamples, data)
|
|
}
|
|
|
|
return &serverNameExampleV1.ListUserExampleByLastIDReply{
|
|
UserExamples: userExamples,
|
|
}, nil
|
|
}
|
|
|
|
func convertUserExample(record *model.UserExample) (*serverNameExampleV1.UserExample, error) {
|
|
value := &serverNameExampleV1.UserExample{}
|
|
err := copier.Copy(value, record)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
// Note: if copier.Copy cannot assign a value to a field, add it here
|
|
|
|
return value, nil
|
|
}
|