作者 yangfu

fix: 微调

... ... @@ -9,6 +9,7 @@ require (
github.com/go-pg/pg/v10 v10.0.0-beta.2
github.com/tiptok/gocomm v1.0.2
github.com/tal-tech/go-zero v1.0.11
github.com/linmadan/egglib-go v0.0.0-20191217144343-ca4539f95bf9
)
replace github.com/tiptok/gocomm v1.0.2 => F:\go\src\learn_project\gocomm
... ...
... ... @@ -3,6 +3,7 @@ package user
import (
"crypto/sha1"
"fmt"
"github.com/linmadan/egglib-go/core/application"
"github.com/tiptok/gocomm/common"
"github.com/tiptok/gocomm/pkg/log"
"gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/application/factory"
... ... @@ -20,12 +21,12 @@ func (svr *UserService) CreateUser(header *protocol.RequestHeader, request *prot
)
rsp = &protocolx.CreateUserResponse{}
if err = request.ValidateCommand(); err != nil {
err = protocol.NewCustomMessage(2, err.Error())
err = application.ThrowError(application.ARG_ERROR, err.Error())
return
}
if err = transactionContext.StartTransaction(); err != nil {
log.Error(err)
return nil, err
err = application.ThrowError(application.TRANSACTION_ERROR, err.Error())
return
}
defer func() {
transactionContext.RollbackTransaction()
... ... @@ -49,12 +50,16 @@ func (svr *UserService) CreateUser(header *protocol.RequestHeader, request *prot
if len(newUser.Passwd) == 0 {
newUser.Passwd = fmt.Sprintf("%x", sha1.Sum([]byte("123456")))
}
if m, err := UserRepository.Save(newUser); err != nil {
return nil, err
if m, e := UserRepository.Save(newUser); e != nil {
err = application.ThrowError(application.INTERNAL_SERVER_ERROR, e.Error())
return
} else {
rsp = m
}
err = transactionContext.CommitTransaction()
if err = transactionContext.CommitTransaction(); err != nil {
err = application.ThrowError(application.TRANSACTION_ERROR, err.Error())
return
}
return
}
... ... @@ -64,7 +69,7 @@ func (svr *UserService) UpdateUser(header *protocol.RequestHeader, request *prot
)
rsp = &protocolx.UpdateUserResponse{}
if err = request.ValidateCommand(); err != nil {
err = protocol.NewCustomMessage(2, err.Error())
err = application.ThrowError(application.ARG_ERROR, err.Error())
return
}
if err = transactionContext.StartTransaction(); err != nil {
... ... @@ -88,12 +93,17 @@ func (svr *UserService) UpdateUser(header *protocol.RequestHeader, request *prot
}
}
if err = user.Update(common.ObjectToMap(request)); err != nil {
err = application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
return
}
if user, err = UserRepository.Save(user); err != nil {
err = application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
return
}
if err = transactionContext.CommitTransaction(); err != nil {
err = application.ThrowError(application.TRANSACTION_ERROR, err.Error())
return
}
err = transactionContext.CommitTransaction()
return
}
... ... @@ -103,12 +113,12 @@ func (svr *UserService) GetUser(header *protocol.RequestHeader, request *protoco
)
rsp = &protocolx.GetUserResponse{}
if err = request.ValidateCommand(); err != nil {
err = protocol.NewCustomMessage(2, err.Error())
err = application.ThrowError(application.ARG_ERROR, err.Error())
return
}
if err = transactionContext.StartTransaction(); err != nil {
log.Error(err)
return nil, err
err = application.ThrowError(application.TRANSACTION_ERROR, err.Error())
return
}
defer func() {
transactionContext.RollbackTransaction()
... ... @@ -117,10 +127,14 @@ func (svr *UserService) GetUser(header *protocol.RequestHeader, request *protoco
var UserRepository, _ = factory.CreateUserRepository(transactionContext)
var user *domain.User
if user, err = UserRepository.FindOne(common.ObjectToMap(request)); err != nil {
err = application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
return
}
rsp = user
err = transactionContext.CommitTransaction()
if err = transactionContext.CommitTransaction(); err != nil {
err = application.ThrowError(application.TRANSACTION_ERROR, err.Error())
return
}
return
}
... ... @@ -130,12 +144,12 @@ func (svr *UserService) DeleteUser(header *protocol.RequestHeader, request *prot
)
rsp = &protocolx.DeleteUserResponse{}
if err = request.ValidateCommand(); err != nil {
err = protocol.NewCustomMessage(2, err.Error())
err = application.ThrowError(application.ARG_ERROR, err.Error())
return
}
if err = transactionContext.StartTransaction(); err != nil {
log.Error(err)
return nil, err
err = application.ThrowError(application.TRANSACTION_ERROR, err.Error())
return
}
defer func() {
transactionContext.RollbackTransaction()
... ... @@ -144,13 +158,18 @@ func (svr *UserService) DeleteUser(header *protocol.RequestHeader, request *prot
var UserRepository, _ = factory.CreateUserRepository(transactionContext)
var user *domain.User
if user, err = UserRepository.FindOne(common.ObjectToMap(request)); err != nil {
err = application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
return
}
if user, err = UserRepository.Remove(user); err != nil {
err = application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
return
}
rsp = user
err = transactionContext.CommitTransaction()
if err = transactionContext.CommitTransaction(); err != nil {
err = application.ThrowError(application.TRANSACTION_ERROR, err.Error())
return
}
return
}
... ... @@ -160,7 +179,8 @@ func (svr *UserService) ListUser(header *protocol.RequestHeader, request *protoc
)
rsp = &protocolx.ListUserResponse{}
if err = request.ValidateCommand(); err != nil {
err = protocol.NewCustomMessage(2, err.Error())
err = application.ThrowError(application.TRANSACTION_ERROR, err.Error())
return
}
if err = transactionContext.StartTransaction(); err != nil {
log.Error(err)
... ... @@ -174,13 +194,17 @@ func (svr *UserService) ListUser(header *protocol.RequestHeader, request *protoc
var user []*domain.User
var total int64
if total, user, err = UserRepository.Find(common.ObjectToMap(request)); err != nil {
err = application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
return
}
rsp = map[string]interface{}{
"total": total,
"list": user,
}
err = transactionContext.CommitTransaction()
if err = transactionContext.CommitTransaction(); err != nil {
err = application.ThrowError(application.TRANSACTION_ERROR, err.Error())
return
}
return
}
... ...
... ... @@ -2,6 +2,7 @@ package protocol
import (
"encoding/json"
"github.com/linmadan/egglib-go/core/application"
)
//CustomErrParse 解析自定义错误结构体
... ... @@ -72,6 +73,13 @@ func NewResponseMessageData(data interface{}, err error) *ResponseMessage {
msg.Data = data
return msg
}
if v, ok := err.(*application.ServiceError); ok {
msg = &ResponseMessage{
ErrorCode{v.Code, v.Message},
data,
}
return msg
}
return NewMesage(1)
}
... ...