project_module_controller.go 3.7 KB
package controllers

import (
	"encoding/json"

	"github.com/astaxie/beego"
	"github.com/linmadan/egglib-go/web/beego/utils"
	"github.com/tiptok/godevp/pkg/application/projectModule/command"
	"github.com/tiptok/godevp/pkg/application/projectModule/query"
	"github.com/tiptok/godevp/pkg/application/projectModule/service"
)

type ProjectModuleController struct {
	beego.Controller
}

func (controller *ProjectModuleController) CreateProjectModule() {
	projectModuleService := service.NewProjectModuleService(nil)
	createProjectModuleCommand := &command.CreateProjectModuleCommand{}
	json.Unmarshal(controller.Ctx.Input.GetData("requestBody").([]byte), createProjectModuleCommand)
	data, err := projectModuleService.CreateProjectModule(createProjectModuleCommand)
	var response utils.JsonResponse
	if err != nil {
		response = utils.ResponseError(controller.Ctx, err)
	} else {
		response = utils.ResponseData(controller.Ctx, data)
	}
	controller.Data["json"] = response
	controller.ServeJSON()
}

func (controller *ProjectModuleController) UpdateProjectModule() {
	projectModuleService := service.NewProjectModuleService(nil)
	updateProjectModuleCommand := &command.UpdateProjectModuleCommand{}
	json.Unmarshal(controller.Ctx.Input.GetData("requestBody").([]byte), updateProjectModuleCommand)
	projectModuleId, _ := controller.GetInt64(":projectModuleId")
	updateProjectModuleCommand.ProjectModuleId = projectModuleId
	data, err := projectModuleService.UpdateProjectModule(updateProjectModuleCommand)
	var response utils.JsonResponse
	if err != nil {
		response = utils.ResponseError(controller.Ctx, err)
	} else {
		response = utils.ResponseData(controller.Ctx, data)
	}
	controller.Data["json"] = response
	controller.ServeJSON()
}

func (controller *ProjectModuleController) GetProjectModule() {
	projectModuleService := service.NewProjectModuleService(nil)
	getProjectModuleQuery := &query.GetProjectModuleQuery{}
	projectModuleId, _ := controller.GetInt64(":projectModuleId")
	getProjectModuleQuery.ProjectModuleId = projectModuleId
	data, err := projectModuleService.GetProjectModule(getProjectModuleQuery)
	var response utils.JsonResponse
	if err != nil {
		response = utils.ResponseError(controller.Ctx, err)
	} else {
		response = utils.ResponseData(controller.Ctx, data)
	}
	controller.Data["json"] = response
	controller.ServeJSON()
}

func (controller *ProjectModuleController) RemoveProjectModule() {
	projectModuleService := service.NewProjectModuleService(nil)
	removeProjectModuleCommand := &command.RemoveProjectModuleCommand{}
	json.Unmarshal(controller.Ctx.Input.GetData("requestBody").([]byte), removeProjectModuleCommand)
	projectModuleId, _ := controller.GetInt64(":projectModuleId")
	removeProjectModuleCommand.ProjectModuleId = projectModuleId
	data, err := projectModuleService.RemoveProjectModule(removeProjectModuleCommand)
	var response utils.JsonResponse
	if err != nil {
		response = utils.ResponseError(controller.Ctx, err)
	} else {
		response = utils.ResponseData(controller.Ctx, data)
	}
	controller.Data["json"] = response
	controller.ServeJSON()
}

func (controller *ProjectModuleController) ListProjectModule() {
	projectModuleService := service.NewProjectModuleService(nil)
	listProjectModuleQuery := &query.ListProjectModuleQuery{}
	offset, _ := controller.GetInt("offset")
	listProjectModuleQuery.Offset = offset
	limit, _ := controller.GetInt("limit")
	listProjectModuleQuery.Limit = limit
	data, err := projectModuleService.ListProjectModule(listProjectModuleQuery)
	var response utils.JsonResponse
	if err != nil {
		response = utils.ResponseError(controller.Ctx, err)
	} else {
		response = utils.ResponseData(controller.Ctx, data)
	}
	controller.Data["json"] = response
	controller.ServeJSON()
}