device_controller.go 2.1 KB
package controllers

import (
	"github.com/linmadan/egglib-go/web/beego"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/device/command"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/device/query"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/device/service"
)

type DeviceController struct {
	beego.BaseController
}

func (controller *DeviceController) CreateDevice() {
	deviceService := service.NewDeviceService(nil)
	createDeviceCommand := &command.CreateDeviceCommand{}
	controller.Unmarshal(createDeviceCommand)
	data, err := deviceService.CreateDevice(createDeviceCommand)
	controller.Response(data, err)
}

func (controller *DeviceController) UpdateDevice() {
	deviceService := service.NewDeviceService(nil)
	updateDeviceCommand := &command.UpdateDeviceCommand{}
	controller.Unmarshal(updateDeviceCommand)
	deviceId, _ := controller.GetInt(":deviceId")
	updateDeviceCommand.DeviceId = deviceId
	data, err := deviceService.UpdateDevice(updateDeviceCommand)
	controller.Response(data, err)
}

func (controller *DeviceController) GetDevice() {
	deviceService := service.NewDeviceService(nil)
	getDeviceQuery := &query.GetDeviceQuery{}
	deviceId, _ := controller.GetInt(":deviceId")
	getDeviceQuery.DeviceId = deviceId
	data, err := deviceService.GetDevice(getDeviceQuery)
	controller.Response(data, err)
}

func (controller *DeviceController) RemoveDevice() {
	deviceService := service.NewDeviceService(nil)
	removeDeviceCommand := &command.RemoveDeviceCommand{}
	controller.Unmarshal(removeDeviceCommand)
	deviceId, _ := controller.GetInt(":deviceId")
	removeDeviceCommand.DeviceId = deviceId
	data, err := deviceService.RemoveDevice(removeDeviceCommand)
	controller.Response(data, err)
}

func (controller *DeviceController) ListDevice() {
	deviceService := service.NewDeviceService(nil)
	listDeviceQuery := &query.ListDeviceQuery{}
	offset, _ := controller.GetInt("offset")
	listDeviceQuery.Offset = offset
	limit, _ := controller.GetInt("limit")
	listDeviceQuery.Limit = limit
	data, err := deviceService.ListDevice(listDeviceQuery)
	controller.Response(data, err)
}