project_module_version.go 9.6 KB
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/factory"
	"github.com/tiptok/godevp/pkg/application/projectModuleVersion/command"
	"github.com/tiptok/godevp/pkg/application/projectModuleVersion/query"
	"github.com/tiptok/godevp/pkg/domain"
)

// 项目模板版本
type ProjectModuleVersionService struct {
}

// 创建
func (projectModuleVersionService *ProjectModuleVersionService) CreateProjectModuleVersion(createProjectModuleVersionCommand *command.CreateProjectModuleVersionCommand) (interface{}, error) {
	if err := createProjectModuleVersionCommand.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()
	}()
	newProjectModuleVersion := &domain.ProjectModuleVersion{
		ProjectModuleId: createProjectModuleVersionCommand.ProjectModuleId,
		Version:         createProjectModuleVersionCommand.Version,
		Description:     createProjectModuleVersionCommand.Description,
	}
	var projectModuleVersionRepository domain.ProjectModuleVersionRepository
	if value, err := factory.CreateProjectModuleVersionRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		projectModuleVersionRepository = value
	}
	if projectModuleVersion, err := projectModuleVersionRepository.Save(newProjectModuleVersion); 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 projectModuleVersion, nil
	}
}

// 返回
func (projectModuleVersionService *ProjectModuleVersionService) GetProjectModuleVersion(getProjectModuleVersionQuery *query.GetProjectModuleVersionQuery) (interface{}, error) {
	if err := getProjectModuleVersionQuery.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 projectModuleVersionRepository domain.ProjectModuleVersionRepository
	if value, err := factory.CreateProjectModuleVersionRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		projectModuleVersionRepository = value
	}
	projectModuleVersion, err := projectModuleVersionRepository.FindOne(map[string]interface{}{"projectModuleVersionId": getProjectModuleVersionQuery.ProjectModuleVersionId})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if projectModuleVersion == nil {
		return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(getProjectModuleVersionQuery.ProjectModuleVersionId)))
	} else {
		if err := transactionContext.CommitTransaction(); err != nil {
			return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
		}
		return projectModuleVersion, nil
	}
}

// 返回列表
func (projectModuleVersionService *ProjectModuleVersionService) ListProjectModuleVersion(listProjectModuleVersionQuery *query.ListProjectModuleVersionQuery) (interface{}, error) {
	if err := listProjectModuleVersionQuery.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 projectModuleVersionRepository domain.ProjectModuleVersionRepository
	if value, err := factory.CreateProjectModuleVersionRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		projectModuleVersionRepository = value
	}
	if count, projectModuleVersions, err := projectModuleVersionRepository.Find(tool_funs.SimpleStructToMap(listProjectModuleVersionQuery)); 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,
			"projectModuleVersions": projectModuleVersions,
		}, nil
	}
}

// 移除
func (projectModuleVersionService *ProjectModuleVersionService) RemoveProjectModuleVersion(removeProjectModuleVersionCommand *command.RemoveProjectModuleVersionCommand) (interface{}, error) {
	if err := removeProjectModuleVersionCommand.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 projectModuleVersionRepository domain.ProjectModuleVersionRepository
	if value, err := factory.CreateProjectModuleVersionRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		projectModuleVersionRepository = value
	}
	projectModuleVersion, err := projectModuleVersionRepository.FindOne(map[string]interface{}{"projectModuleVersionId": removeProjectModuleVersionCommand.ProjectModuleVersionId})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if projectModuleVersion == nil {
		return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(removeProjectModuleVersionCommand.ProjectModuleVersionId)))
	}
	if projectModuleVersion, err := projectModuleVersionRepository.Remove(projectModuleVersion); 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 projectModuleVersion, nil
	}
}

// 更新
func (projectModuleVersionService *ProjectModuleVersionService) UpdateProjectModuleVersion(updateProjectModuleVersionCommand *command.UpdateProjectModuleVersionCommand) (interface{}, error) {
	if err := updateProjectModuleVersionCommand.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 projectModuleVersionRepository domain.ProjectModuleVersionRepository
	if value, err := factory.CreateProjectModuleVersionRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		projectModuleVersionRepository = value
	}
	projectModuleVersion, err := projectModuleVersionRepository.FindOne(map[string]interface{}{"projectModuleVersionId": updateProjectModuleVersionCommand.ProjectModuleVersionId})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if projectModuleVersion == nil {
		return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(updateProjectModuleVersionCommand.ProjectModuleVersionId)))
	}
	if err := projectModuleVersion.Update(tool_funs.SimpleStructToMap(updateProjectModuleVersionCommand)); err != nil {
		return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	}
	if projectModuleVersion, err := projectModuleVersionRepository.Save(projectModuleVersion); 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 projectModuleVersion, nil
	}
}

func NewProjectModuleVersionService(options map[string]interface{}) *ProjectModuleVersionService {
	newProjectModuleVersionService := &ProjectModuleVersionService{}
	return newProjectModuleVersionService
}