client_version_controller.go 3.5 KB
package controllers

import (
	"encoding/json"

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

type ClientVersionController struct {
	beego.Controller
}

func (controller *ClientVersionController) CreateClientVersion() {
	clientVersionService := service.NewClientVersionService(nil)
	createClientVersionCommand := &command.CreateClientVersionCommand{}
	json.Unmarshal(controller.Ctx.Input.GetData("requestBody").([]byte), createClientVersionCommand)
	data, err := clientVersionService.CreateClientVersion(createClientVersionCommand)
	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 *ClientVersionController) UpdateClientVersion() {
	clientVersionService := service.NewClientVersionService(nil)
	updateClientVersionCommand := &command.UpdateClientVersionCommand{}
	json.Unmarshal(controller.Ctx.Input.GetData("requestBody").([]byte), updateClientVersionCommand)
	id, _ := controller.GetInt64(":id")
	updateClientVersionCommand.Id = id
	data, err := clientVersionService.UpdateClientVersion(updateClientVersionCommand)
	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 *ClientVersionController) GetClientVersion() {
	clientVersionService := service.NewClientVersionService(nil)
	getClientVersionQuery := &query.GetClientVersionQuery{}
	id, _ := controller.GetInt64(":id")
	getClientVersionQuery.Id = id
	data, err := clientVersionService.GetClientVersion(getClientVersionQuery)
	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 *ClientVersionController) RemoveClientVersion() {
	clientVersionService := service.NewClientVersionService(nil)
	removeClientVersionCommand := &command.RemoveClientVersionCommand{}
	json.Unmarshal(controller.Ctx.Input.GetData("requestBody").([]byte), removeClientVersionCommand)
	id, _ := controller.GetInt64(":id")
	removeClientVersionCommand.Id = id
	data, err := clientVersionService.RemoveClientVersion(removeClientVersionCommand)
	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 *ClientVersionController) ListClientVersion() {
	clientVersionService := service.NewClientVersionService(nil)
	listClientVersionQuery := &query.ListClientVersionQuery{}
	offset, _ := controller.GetInt("offset")
	listClientVersionQuery.Offset = offset
	limit, _ := controller.GetInt("limit")
	listClientVersionQuery.Limit = limit
	data, err := clientVersionService.ListClientVersion(listClientVersionQuery)
	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()
}