task_controller.go 8.8 KB
package controllers

import (
	"encoding/json"
	"fmt"

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

type TaskController struct {
	beego.Controller
}

func (controller *TaskController) RobTask() {
	taskService := service.NewTaskService(nil)
	robTaskCommand := &command.RobTaskCommand{}
	json.Unmarshal(controller.Ctx.Input.GetData("requestBody").([]byte), robTaskCommand)
	taskId, _ := controller.GetInt64(":taskId")
	robTaskCommand.TaskId = taskId
	data, err := taskService.RobTask(robTaskCommand)
	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 *TaskController) BidTask() {
	taskService := service.NewTaskService(nil)
	bidTaskCommand := &command.BidTaskCommand{}
	json.Unmarshal(controller.Ctx.Input.GetData("requestBody").([]byte), bidTaskCommand)
	taskId, _ := controller.GetInt64(":taskId")
	bidTaskCommand.TaskId = taskId
	data, err := taskService.BidTask(bidTaskCommand)
	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 *TaskController) ApplyCompleteTask() {
	taskService := service.NewTaskService(nil)
	applyCompleteTaskCommand := &command.ApplyCompleteTaskCommand{}
	json.Unmarshal(controller.Ctx.Input.GetData("requestBody").([]byte), applyCompleteTaskCommand)
	taskId, _ := controller.GetInt64(":taskId")
	applyCompleteTaskCommand.TaskId = taskId
	data, err := taskService.ApplyCompleteTask(applyCompleteTaskCommand)
	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 *TaskController) ReleaseTask() {
	taskService := service.NewTaskService(nil)
	releaseTaskCommand := &command.ReleaseTaskCommand{}
	json.Unmarshal(controller.Ctx.Input.GetData("requestBody").([]byte), releaseTaskCommand)
	taskId, _ := controller.GetInt64(":taskId")
	releaseTaskCommand.TaskId = taskId
	data, err := taskService.ReleaseTask(releaseTaskCommand)
	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 *TaskController) ChooseSuccessfulBidder() {
	taskService := service.NewTaskService(nil)
	chooseSuccessfulBidderCommand := &command.ChooseSuccessfulBidderCommand{}
	json.Unmarshal(controller.Ctx.Input.GetData("requestBody").([]byte), chooseSuccessfulBidderCommand)
	taskId, _ := controller.GetInt64(":taskId")
	chooseSuccessfulBidderCommand.TaskId = taskId
	data, err := taskService.ChooseSuccessfulBidder(chooseSuccessfulBidderCommand)
	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 *TaskController) OffTask() {
	taskService := service.NewTaskService(nil)
	offTaskCommand := &command.OffTaskCommand{}
	json.Unmarshal(controller.Ctx.Input.GetData("requestBody").([]byte), offTaskCommand)
	taskId, _ := controller.GetInt64(":taskId")
	offTaskCommand.TaskId = taskId
	data, err := taskService.OffTask(offTaskCommand)
	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 *TaskController) AcceptanceTask() {
	taskService := service.NewTaskService(nil)
	acceptanceTaskCommand := &command.AcceptanceTaskCommand{}
	json.Unmarshal(controller.Ctx.Input.GetData("requestBody").([]byte), acceptanceTaskCommand)
	taskId, _ := controller.GetInt64(":taskId")
	acceptanceTaskCommand.TaskId = taskId
	data, err := taskService.AcceptanceTask(acceptanceTaskCommand)
	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 *TaskController) SearchTask() {
	taskService := service.NewTaskService(nil)
	searchTaskCommand := &command.SearchTaskCommand{}
	json.Unmarshal(controller.Ctx.Input.GetData("requestBody").([]byte), searchTaskCommand)
	data, err := taskService.SearchTask(searchTaskCommand)
	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 *TaskController) CreateTask() {
	taskService := service.NewTaskService(nil)
	createTaskCommand := &command.CreateTaskCommand{}
	json.Unmarshal(controller.Ctx.Input.GetData("requestBody").([]byte), createTaskCommand)
	data, err := taskService.CreateTask(createTaskCommand)
	var response utils.JsonResponse
	if err != nil {
		fmt.Println(err.Error())
		response = utils.ResponseError(controller.Ctx, err)
	} else {
		response = utils.ResponseData(controller.Ctx, data)
	}
	controller.Data["json"] = response
	controller.ServeJSON()
}

func (controller *TaskController) UpdateTask() {
	taskService := service.NewTaskService(nil)
	updateTaskCommand := &command.UpdateTaskCommand{}
	json.Unmarshal(controller.Ctx.Input.GetData("requestBody").([]byte), updateTaskCommand)
	taskId, _ := controller.GetInt64(":taskId")
	updateTaskCommand.TaskId = taskId
	data, err := taskService.UpdateTask(updateTaskCommand)
	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 *TaskController) GetTask() {
	taskService := service.NewTaskService(nil)
	getTaskQuery := &query.GetTaskQuery{}
	taskId, _ := controller.GetInt64(":taskId")
	getTaskQuery.TaskId = taskId
	data, err := taskService.GetTask(getTaskQuery)
	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 *TaskController) RemoveTask() {
	taskService := service.NewTaskService(nil)
	removeTaskCommand := &command.RemoveTaskCommand{}
	taskId, _ := controller.GetInt64(":taskId")
	removeTaskCommand.TaskId = taskId
	data, err := taskService.RemoveTask(removeTaskCommand)
	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 *TaskController) ListTask() {
	taskService := service.NewTaskService(nil)
	listTaskQuery := &query.ListTaskQuery{}
	companyId, _ := controller.GetInt64("companyId")
	listTaskQuery.CompanyId = companyId
	sponsor, _ := controller.GetInt64("sponsor")
	listTaskQuery.Sponsor = sponsor
	taskContentMatch := controller.GetString("taskContentMatch")
	listTaskQuery.TaskContentMatch = taskContentMatch
	taskType, _ := controller.GetInt("taskType")
	listTaskQuery.TaskType = taskType
	taskStatus, _ := controller.GetInt("taskStatus")
	listTaskQuery.TaskStatus = taskStatus
	customerValue := controller.GetString("customerValue")
	listTaskQuery.CustomerValue = customerValue
	taskNature := controller.GetString("taskNature")
	listTaskQuery.TaskNature = taskNature
	isRewardTake, _ := controller.GetBool("isRewardTake")
	listTaskQuery.IsRewardTake = isRewardTake
	bidTimeMatch, _ := controller.GetInt("bidTimeMatch")
	listTaskQuery.BidTimeMatch = bidTimeMatch
	receiver, _ := controller.GetInt64("receiver")
	listTaskQuery.Receiver = receiver
	participator, _ := controller.GetInt64("participator")
	listTaskQuery.Participator = participator
	offset, _ := controller.GetInt("offset")
	listTaskQuery.Offset = offset
	limit, _ := controller.GetInt("limit")
	listTaskQuery.Limit = limit
	data, err := taskService.ListTask(listTaskQuery)
	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()
}