project_module_files.go 9.7 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/projectModuleFiles/command"
	"github.com/tiptok/godevp/pkg/application/projectModuleFiles/query"
	"github.com/tiptok/godevp/pkg/domain"
)

// 项目模块文件
type ProjectModuleFilesService struct {
}

// 创建
func (projectModuleFilesService *ProjectModuleFilesService) CreateProjectModuleFiles(createProjectModuleFilesCommand *command.CreateProjectModuleFilesCommand) (interface{}, error) {
	if err := createProjectModuleFilesCommand.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()
	}()
	newProjectModuleFiles := &domain.ProjectModuleFiles{
		ProjectModuleId:      createProjectModuleFilesCommand.ProjectModuleId,
		ProjectModuleVersion: createProjectModuleFilesCommand.ProjectModuleVersion,
		FileType:             createProjectModuleFilesCommand.FileType,
		CodeBlock:            createProjectModuleFilesCommand.CodeBlock,
		ParentId:             createProjectModuleFilesCommand.ParentId,
		Sort:                 createProjectModuleFilesCommand.Sort,
		Remark:               createProjectModuleFilesCommand.Remark,
	}
	var projectModuleFilesRepository domain.ProjectModuleFilesRepository
	if value, err := factory.CreateProjectModuleFilesRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		projectModuleFilesRepository = value
	}
	if projectModuleFiles, err := projectModuleFilesRepository.Save(newProjectModuleFiles); 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 projectModuleFiles, nil
	}
}

// 返回
func (projectModuleFilesService *ProjectModuleFilesService) GetProjectModuleFiles(getProjectModuleFilesQuery *query.GetProjectModuleFilesQuery) (interface{}, error) {
	if err := getProjectModuleFilesQuery.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 projectModuleFilesRepository domain.ProjectModuleFilesRepository
	if value, err := factory.CreateProjectModuleFilesRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		projectModuleFilesRepository = value
	}
	projectModuleFiles, err := projectModuleFilesRepository.FindOne(map[string]interface{}{"projectModuleFilesId": getProjectModuleFilesQuery.ProjectModuleFilesId})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if projectModuleFiles == nil {
		return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(getProjectModuleFilesQuery.ProjectModuleFilesId)))
	} else {
		if err := transactionContext.CommitTransaction(); err != nil {
			return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
		}
		return projectModuleFiles, nil
	}
}

// 返回列表
func (projectModuleFilesService *ProjectModuleFilesService) ListProjectModuleFiles(listProjectModuleFilesQuery *query.ListProjectModuleFilesQuery) (interface{}, error) {
	if err := listProjectModuleFilesQuery.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 projectModuleFilesRepository domain.ProjectModuleFilesRepository
	if value, err := factory.CreateProjectModuleFilesRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		projectModuleFilesRepository = value
	}
	if count, projectModuleFiless, err := projectModuleFilesRepository.Find(tool_funs.SimpleStructToMap(listProjectModuleFilesQuery)); 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,
			"projectModuleFiless": projectModuleFiless,
		}, nil
	}
}

// 移除
func (projectModuleFilesService *ProjectModuleFilesService) RemoveProjectModuleFiles(removeProjectModuleFilesCommand *command.RemoveProjectModuleFilesCommand) (interface{}, error) {
	if err := removeProjectModuleFilesCommand.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 projectModuleFilesRepository domain.ProjectModuleFilesRepository
	if value, err := factory.CreateProjectModuleFilesRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		projectModuleFilesRepository = value
	}
	projectModuleFiles, err := projectModuleFilesRepository.FindOne(map[string]interface{}{"projectModuleFilesId": removeProjectModuleFilesCommand.ProjectModuleFilesId})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if projectModuleFiles == nil {
		return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(removeProjectModuleFilesCommand.ProjectModuleFilesId)))
	}
	if projectModuleFiles, err := projectModuleFilesRepository.Remove(projectModuleFiles); 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 projectModuleFiles, nil
	}
}

// 更新
func (projectModuleFilesService *ProjectModuleFilesService) UpdateProjectModuleFiles(updateProjectModuleFilesCommand *command.UpdateProjectModuleFilesCommand) (interface{}, error) {
	if err := updateProjectModuleFilesCommand.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 projectModuleFilesRepository domain.ProjectModuleFilesRepository
	if value, err := factory.CreateProjectModuleFilesRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		projectModuleFilesRepository = value
	}
	projectModuleFiles, err := projectModuleFilesRepository.FindOne(map[string]interface{}{"projectModuleFilesId": updateProjectModuleFilesCommand.ProjectModuleFilesId})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if projectModuleFiles == nil {
		return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(updateProjectModuleFilesCommand.ProjectModuleFilesId)))
	}
	if err := projectModuleFiles.Update(tool_funs.SimpleStructToMap(updateProjectModuleFilesCommand)); err != nil {
		return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	}
	if projectModuleFiles, err := projectModuleFilesRepository.Save(projectModuleFiles); 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 projectModuleFiles, nil
	}
}

func NewProjectModuleFilesService(options map[string]interface{}) *ProjectModuleFilesService {
	newProjectModuleFilesService := &ProjectModuleFilesService{}
	return newProjectModuleFilesService
}