|
|
package service
|
|
|
|
|
|
import (
|
|
|
"fmt"
|
|
|
"github.com/linmadan/egglib-go/core/application"
|
|
|
"github.com/linmadan/egglib-go/utils/tool_funs"
|
|
|
"github.com/tiptok/godevp/pkg/application/clientVersion/command"
|
|
|
"github.com/tiptok/godevp/pkg/application/clientVersion/query"
|
|
|
"github.com/tiptok/godevp/pkg/application/factory"
|
|
|
"github.com/tiptok/godevp/pkg/domain"
|
|
|
)
|
|
|
|
|
|
// 客户端版本服务
|
|
|
type ClientVersionService struct {
|
|
|
}
|
|
|
|
|
|
// 创建
|
|
|
func (clientVersionService *ClientVersionService) CreateClientVersion(createClientVersionCommand *command.CreateClientVersionCommand) (interface{}, error) {
|
|
|
if err := createClientVersionCommand.ValidateCommand(); err != nil {
|
|
|
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
|
|
|
}
|
|
|
transactionContext, err := factory.CreateTransactionContext(nil)
|
|
|
if err != nil {
|
|
|
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
}
|
|
|
if err := transactionContext.StartTransaction(); err != nil {
|
|
|
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
}
|
|
|
defer func() {
|
|
|
transactionContext.RollbackTransaction()
|
|
|
}()
|
|
|
newClientVersion := &domain.ClientVersion{
|
|
|
Commiter: createClientVersionCommand.Commiter,
|
|
|
ProjectName: createClientVersionCommand.ProjectName,
|
|
|
Version: createClientVersionCommand.Version,
|
|
|
Title: createClientVersionCommand.Title,
|
|
|
Remark: createClientVersionCommand.Remark,
|
|
|
ClientPackageInfo: createClientVersionCommand.ClientPackageInfo,
|
|
|
}
|
|
|
var clientVersionRepository domain.ClientVersionRepository
|
|
|
if value, err := factory.CreateClientVersionRepository(map[string]interface{}{
|
|
|
"transactionContext": transactionContext,
|
|
|
}); err != nil {
|
|
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
} else {
|
|
|
clientVersionRepository = value
|
|
|
}
|
|
|
if clientVersion, err := clientVersionRepository.Save(newClientVersion); err != nil {
|
|
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
} else {
|
|
|
if err := transactionContext.CommitTransaction(); err != nil {
|
|
|
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
}
|
|
|
return clientVersion, nil
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 返回
|
|
|
func (clientVersionService *ClientVersionService) GetClientVersion(getClientVersionQuery *query.GetClientVersionQuery) (interface{}, error) {
|
|
|
if err := getClientVersionQuery.ValidateQuery(); err != nil {
|
|
|
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
|
|
|
}
|
|
|
transactionContext, err := factory.CreateTransactionContext(nil)
|
|
|
if err != nil {
|
|
|
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
}
|
|
|
if err := transactionContext.StartTransaction(); err != nil {
|
|
|
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
}
|
|
|
defer func() {
|
|
|
transactionContext.RollbackTransaction()
|
|
|
}()
|
|
|
var clientVersionRepository domain.ClientVersionRepository
|
|
|
if value, err := factory.CreateClientVersionRepository(map[string]interface{}{
|
|
|
"transactionContext": transactionContext,
|
|
|
}); err != nil {
|
|
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
} else {
|
|
|
clientVersionRepository = value
|
|
|
}
|
|
|
clientVersion, err := clientVersionRepository.FindOne(map[string]interface{}{"clientVersionId": getClientVersionQuery.Id})
|
|
|
if err != nil {
|
|
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
}
|
|
|
if clientVersion == nil {
|
|
|
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(getClientVersionQuery.Id)))
|
|
|
} else {
|
|
|
if err := transactionContext.CommitTransaction(); err != nil {
|
|
|
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
}
|
|
|
return clientVersion, nil
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 返回列表
|
|
|
func (clientVersionService *ClientVersionService) ListClientVersion(listClientVersionQuery *query.ListClientVersionQuery) (interface{}, error) {
|
|
|
if err := listClientVersionQuery.ValidateQuery(); err != nil {
|
|
|
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
|
|
|
}
|
|
|
transactionContext, err := factory.CreateTransactionContext(nil)
|
|
|
if err != nil {
|
|
|
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
}
|
|
|
if err := transactionContext.StartTransaction(); err != nil {
|
|
|
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
}
|
|
|
defer func() {
|
|
|
transactionContext.RollbackTransaction()
|
|
|
}()
|
|
|
var clientVersionRepository domain.ClientVersionRepository
|
|
|
if value, err := factory.CreateClientVersionRepository(map[string]interface{}{
|
|
|
"transactionContext": transactionContext,
|
|
|
}); err != nil {
|
|
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
} else {
|
|
|
clientVersionRepository = value
|
|
|
}
|
|
|
if count, clientVersions, err := clientVersionRepository.Find(tool_funs.SimpleStructToMap(listClientVersionQuery)); err != nil {
|
|
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
} else {
|
|
|
if err := transactionContext.CommitTransaction(); err != nil {
|
|
|
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
}
|
|
|
return map[string]interface{}{
|
|
|
"count": count,
|
|
|
"clientVersions": clientVersions,
|
|
|
}, nil
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 移除
|
|
|
func (clientVersionService *ClientVersionService) RemoveClientVersion(removeClientVersionCommand *command.RemoveClientVersionCommand) (interface{}, error) {
|
|
|
if err := removeClientVersionCommand.ValidateCommand(); err != nil {
|
|
|
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
|
|
|
}
|
|
|
transactionContext, err := factory.CreateTransactionContext(nil)
|
|
|
if err != nil {
|
|
|
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
}
|
|
|
if err := transactionContext.StartTransaction(); err != nil {
|
|
|
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
}
|
|
|
defer func() {
|
|
|
transactionContext.RollbackTransaction()
|
|
|
}()
|
|
|
var clientVersionRepository domain.ClientVersionRepository
|
|
|
if value, err := factory.CreateClientVersionRepository(map[string]interface{}{
|
|
|
"transactionContext": transactionContext,
|
|
|
}); err != nil {
|
|
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
} else {
|
|
|
clientVersionRepository = value
|
|
|
}
|
|
|
clientVersion, err := clientVersionRepository.FindOne(map[string]interface{}{"clientVersionId": removeClientVersionCommand.Id})
|
|
|
if err != nil {
|
|
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
}
|
|
|
if clientVersion == nil {
|
|
|
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(removeClientVersionCommand.Id)))
|
|
|
}
|
|
|
if clientVersion, err := clientVersionRepository.Remove(clientVersion); err != nil {
|
|
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
} else {
|
|
|
if err := transactionContext.CommitTransaction(); err != nil {
|
|
|
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
}
|
|
|
return clientVersion, nil
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 更新
|
|
|
func (clientVersionService *ClientVersionService) UpdateClientVersion(updateClientVersionCommand *command.UpdateClientVersionCommand) (interface{}, error) {
|
|
|
if err := updateClientVersionCommand.ValidateCommand(); err != nil {
|
|
|
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
|
|
|
}
|
|
|
transactionContext, err := factory.CreateTransactionContext(nil)
|
|
|
if err != nil {
|
|
|
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
}
|
|
|
if err := transactionContext.StartTransaction(); err != nil {
|
|
|
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
}
|
|
|
defer func() {
|
|
|
transactionContext.RollbackTransaction()
|
|
|
}()
|
|
|
var clientVersionRepository domain.ClientVersionRepository
|
|
|
if value, err := factory.CreateClientVersionRepository(map[string]interface{}{
|
|
|
"transactionContext": transactionContext,
|
|
|
}); err != nil {
|
|
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
} else {
|
|
|
clientVersionRepository = value
|
|
|
}
|
|
|
clientVersion, err := clientVersionRepository.FindOne(map[string]interface{}{"clientVersionId": updateClientVersionCommand.Id})
|
|
|
if err != nil {
|
|
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
}
|
|
|
if clientVersion == nil {
|
|
|
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(updateClientVersionCommand.Id)))
|
|
|
}
|
|
|
if err := clientVersion.Update(tool_funs.SimpleStructToMap(updateClientVersionCommand)); err != nil {
|
|
|
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
|
|
|
}
|
|
|
if clientVersion, err := clientVersionRepository.Save(clientVersion); err != nil {
|
|
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
} else {
|
|
|
if err := transactionContext.CommitTransaction(); err != nil {
|
|
|
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
}
|
|
|
return clientVersion, nil
|
|
|
}
|
|
|
}
|
|
|
|
|
|
func NewClientVersionService(options map[string]interface{}) *ClientVersionService {
|
|
|
newClientVersionService := &ClientVersionService{}
|
|
|
return newClientVersionService
|
|
|
} |
...
|
...
|
|