dictionary_controller.go 2.8 KB
package controllers

import (
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-basic/pkg/application/dictionary/command"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-basic/pkg/application/dictionary/query"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-basic/pkg/application/dictionary/service"
)

type DictionaryController struct {
	baseController
}

func (controller *DictionaryController) returnPageListData(count int64, data interface{}, err error, pageNumber int) {
	dataMap := map[string]interface{}{
		"grid": map[string]interface{}{
			"total":      count,
			"pageNumber": pageNumber,
			"list":       data,
		},
	}
	controller.Response(dataMap, err)
}

func (controller *DictionaryController) CreateDictionary() {
	dictionaryService := service.NewDictionaryService(nil)
	createDictionaryCommand := &command.CreateDictionaryCommand{}
	_ = controller.Unmarshal(createDictionaryCommand)
	data, err := dictionaryService.CreateDictionary(createDictionaryCommand)
	controller.Response(data, err)
}

func (controller *DictionaryController) UpdateDictionary() {
	dictionaryService := service.NewDictionaryService(nil)
	updateDictionaryCommand := &command.UpdateDictionaryCommand{}
	_ = controller.Unmarshal(updateDictionaryCommand)
	dictionaryId, _ := controller.GetInt64(":dictionaryId")
	updateDictionaryCommand.DictionaryId = dictionaryId
	data, err := dictionaryService.UpdateDictionary(updateDictionaryCommand)
	controller.Response(data, err)
}

func (controller *DictionaryController) GetDictionary() {
	dictionaryService := service.NewDictionaryService(nil)
	getDictionaryQuery := &query.GetDictionaryQuery{}
	dictionaryId, _ := controller.GetInt64(":dictionaryId")
	getDictionaryQuery.DictionaryId = dictionaryId
	data, err := dictionaryService.GetDictionary(getDictionaryQuery)
	controller.Response(data, err)
}

func (controller *DictionaryController) ListDictionary() {
	dictionaryService := service.NewDictionaryService(nil)
	listDictionaryQuery := &query.ListDictionaryQuery{}
	_ = controller.Unmarshal(listDictionaryQuery)
	count, listData, err := dictionaryService.ListDictionary(listDictionaryQuery)
	controller.returnPageListData(count, listData, err, listDictionaryQuery.Pageindex)
}

func (controller *DictionaryController) GetDictionaryByCode() {
	dictionaryService := service.NewDictionaryService(nil)
	getDictionaryQuery := &query.GetDictionaryByCodeQuery{}
	_ = controller.Unmarshal(getDictionaryQuery)
	data, err := dictionaryService.GetDictionaryByCode(getDictionaryQuery)
	controller.Response(data, err)
}

func (controller *DictionaryController) RemoveDictionary() {
	dictionaryService := service.NewDictionaryService(nil)
	removeDictionaryCommand := &command.RemoveDictionaryCommand{}
	_ = controller.Unmarshal(removeDictionaryCommand)
	data, err := dictionaryService.RemoveDictionary(removeDictionaryCommand)
	controller.Response(data, err)
}