product_controller.go 2.2 KB
package controllers

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

type ProductController struct {
	beego.BaseController
}

func (controller *ProductController) CreateProduct() {
	productService := service.NewProductService(nil)
	createProductCommand := &command.CreateProductCommand{}
	controller.Unmarshal(createProductCommand)
	data, err := productService.CreateProduct(createProductCommand)
	controller.Response(data, err)
}

func (controller *ProductController) UpdateProduct() {
	productService := service.NewProductService(nil)
	updateProductCommand := &command.UpdateProductCommand{}
	controller.Unmarshal(updateProductCommand)
	productId, _ := controller.GetInt(":productId")
	updateProductCommand.ProductId = productId
	data, err := productService.UpdateProduct(updateProductCommand)
	controller.Response(data, err)
}

func (controller *ProductController) GetProduct() {
	productService := service.NewProductService(nil)
	getProductQuery := &query.GetProductQuery{}
	productId, _ := controller.GetInt(":productId")
	getProductQuery.ProductId = productId
	data, err := productService.GetProduct(getProductQuery)
	controller.Response(data, err)
}

func (controller *ProductController) RemoveProduct() {
	productService := service.NewProductService(nil)
	removeProductCommand := &command.RemoveProductCommand{}
	controller.Unmarshal(removeProductCommand)
	productId, _ := controller.GetInt(":productId")
	removeProductCommand.ProductId = productId
	data, err := productService.RemoveProduct(removeProductCommand)
	controller.Response(data, err)
}

func (controller *ProductController) ListProduct() {
	productService := service.NewProductService(nil)
	listProductQuery := &query.ListProductQuery{}
	offset, _ := controller.GetInt("offset")
	listProductQuery.Offset = offset
	limit, _ := controller.GetInt("limit")
	listProductQuery.Limit = limit
	data, err := productService.ListProduct(listProductQuery)
	controller.Response(data, err)
}