task_nature_controller.go 3.7 KB
package controllers

import (
	"encoding/json"

	"github.com/astaxie/beego"
	"github.com/linmadan/egglib-go/web/beego/utils"
	"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/application/taskNature/command"
	"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/application/taskNature/query"
	"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/application/taskNature/service"
)

type TaskNatureController struct {
	beego.Controller
}

func (controller *TaskNatureController) CreateTaskNature() {
	taskNatureService := service.NewTaskNatureService(nil)
	createTaskNatureCommand := &command.CreateTaskNatureCommand{}
	json.Unmarshal(controller.Ctx.Input.GetData("requestBody").([]byte), createTaskNatureCommand)
	data, err := taskNatureService.CreateTaskNature(createTaskNatureCommand)
	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 *TaskNatureController) UpdateTaskNature() {
	taskNatureService := service.NewTaskNatureService(nil)
	updateTaskNatureCommand := &command.UpdateTaskNatureCommand{}
	json.Unmarshal(controller.Ctx.Input.GetData("requestBody").([]byte), updateTaskNatureCommand)
	taskNatureId, _ := controller.GetInt(":taskNatureId")
	updateTaskNatureCommand.TaskNatureId = taskNatureId
	data, err := taskNatureService.UpdateTaskNature(updateTaskNatureCommand)
	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 *TaskNatureController) GetTaskNature() {
	taskNatureService := service.NewTaskNatureService(nil)
	getTaskNatureQuery := &query.GetTaskNatureQuery{}
	taskNatureId, _ := controller.GetInt(":taskNatureId")
	getTaskNatureQuery.TaskNatureId = taskNatureId
	data, err := taskNatureService.GetTaskNature(getTaskNatureQuery)
	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 *TaskNatureController) RemoveTaskNature() {
	taskNatureService := service.NewTaskNatureService(nil)
	removeTaskNatureCommand := &command.RemoveTaskNatureCommand{}
	taskNatureId, _ := controller.GetInt(":taskNatureId")
	removeTaskNatureCommand.TaskNatureId = taskNatureId
	data, err := taskNatureService.RemoveTaskNature(removeTaskNatureCommand)
	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 *TaskNatureController) ListTaskNature() {
	taskNatureService := service.NewTaskNatureService(nil)
	listTaskNatureQuery := &query.ListTaskNatureQuery{}
	companyId, _ := controller.GetInt64("companyId")
	listTaskNatureQuery.CompanyId = companyId
	taskNatureName := controller.GetString("taskNatureName")
	listTaskNatureQuery.TaskNatureName = taskNatureName
	taskNatureNameMatch := controller.GetString("taskNatureNameMatch")
	listTaskNatureQuery.TaskNatureNameMatch = taskNatureNameMatch
	offset, _ := controller.GetInt("offset")
	listTaskNatureQuery.Offset = offset
	limit, _ := controller.GetInt("limit")
	listTaskNatureQuery.Limit = limit
	data, err := taskNatureService.ListTaskNature(listTaskNatureQuery)
	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()
}