作者 tangxuhui

添加 字典查询接口

package query
import (
"fmt"
"github.com/beego/beego/v2/core/validation"
)
type GetDictionaryByCodeQuery struct {
// 字典编码
DictCodes []string `json:"dictCodes" valid:"Required"`
}
func (getDictionaryQuery *GetDictionaryByCodeQuery) Valid(validation *validation.Validation) {
}
func (getDictionaryQuery *GetDictionaryByCodeQuery) ValidateQuery() error {
valid := validation.Validation{}
b, err := valid.Valid(getDictionaryQuery)
if err != nil {
return err
}
if !b {
for _, validErr := range valid.Errors {
return fmt.Errorf("%s %s", validErr.Key, validErr.Message)
}
}
return nil
}
... ...
... ... @@ -253,6 +253,47 @@ func (dictionaryService *DictionaryService) UpdateDictionary(updateDictionaryCom
}
}
// 返回数据字典设置
func (dictionaryService *DictionaryService) GetDictionaryByCode(getDictionaryQuery *query.GetDictionaryByCodeQuery) (interface{}, error) {
if err := getDictionaryQuery.ValidateQuery(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
}
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
var dictionaryRepository domain.DictionaryRepository
if value, err := factory.CreateDictionaryRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
dictionaryRepository = value
}
queryCondition := make(map[string]interface{})
if len(getDictionaryQuery.DictCodes) > 0 {
queryCondition["dictCodeIn"] = getDictionaryQuery.DictCodes
}
_, dictionarys, err := dictionaryRepository.Find(queryCondition)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return map[string]interface{}{
"dictionarys": dictionarys,
}, nil
}
func NewDictionaryService(options map[string]interface{}) *DictionaryService {
newDictionaryService := &DictionaryService{}
return newDictionaryService
... ...
... ... @@ -118,6 +118,9 @@ func (repository *DictionaryRepository) Find(queryOptions map[string]interface{}
query := sqlbuilder.BuildQuery(tx.Model(&dictionaryModels), queryOptions)
query.SetOffsetAndLimit(20)
query.SetOrderDirect("dictionary_id", "DESC")
if v, ok := queryOptions["dictCodeIn"]; ok {
query.Where("dict_code IN (?)", pg.Array(v))
}
if count, err := query.SelectAndCount(); err != nil {
return 0, dictionarys, err
} else {
... ...
... ... @@ -48,9 +48,8 @@ func (controller *DictionaryController) ListDictionary() {
func (controller *DictionaryController) GetDictionaryByCode() {
dictionaryService := service.NewDictionaryService(nil)
getDictionaryQuery := &query.GetDictionaryQuery{}
dictionaryCode := controller.GetString(":dictionaryCode")
getDictionaryQuery.DictCode = dictionaryCode
data, err := dictionaryService.GetDictionary(getDictionaryQuery)
getDictionaryQuery := &query.GetDictionaryByCodeQuery{}
_ = controller.Unmarshal(getDictionaryQuery)
data, err := dictionaryService.GetDictionaryByCode(getDictionaryQuery)
controller.Response(data, err)
}
... ...
... ... @@ -10,5 +10,5 @@ func init() {
web.Router("/dictionarys/:dictionaryId", &controllers.DictionaryController{}, "Put:UpdateDictionary")
web.Router("/dictionarys/:dictionaryId", &controllers.DictionaryController{}, "Get:GetDictionary")
web.Router("/dictionarys/search", &controllers.DictionaryController{}, "Post:ListDictionary")
web.Router("/dictionarys/dictionary-code/:dictionaryCode", &controllers.DictionaryController{}, "Get:GetDictionaryByCode")
web.Router("/dictionarys/dictionary-code", &controllers.DictionaryController{}, "Post:GetDictionaryByCode")
}
... ...
package dictionary
import (
"net/http"
"github.com/gavv/httpexpect"
"github.com/go-pg/pg/v10"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
pG "gitlab.fjmaimaimai.com/allied-creation/allied-creation-basic/pkg/infrastructure/pg"
)
var _ = Describe("返回数据字典设置列表", func() {
var dictionaryId int64
BeforeEach(func() {
items := `[{"itemCode":"xxx","itemLabel":"123","itemValue":"abc","isShow":1,"sort":1}]`
_, err := pG.DB.QueryOne(
pg.Scan(&dictionaryId),
"INSERT INTO dictionarys (dictionary_id, dict_code, dict_name, describe, is_show, dict_items) VALUES (?, ?, ?, ?, ?, ?) RETURNING dictionary_id",
10089, "testDictCode", "testDictName", "testDesc", 1, items)
Expect(err).NotTo(HaveOccurred())
})
Describe("根据参数返回字典列表", func() {
Context("传入有效的参数", func() {
It("返回字典数据列表", func() {
body := map[string]interface{}{
"dictCodes": []string{"testDictCode"},
}
httpExpect := httpexpect.New(GinkgoT(), server.URL)
httpExpect.POST("/dictionarys/dictionary-code").
WithJSON(body).
Expect().
Status(http.StatusOK).
JSON().
Object().
ContainsKey("code").ValueEqual("code", 0).
ContainsKey("msg").ValueEqual("msg", "ok").
ContainsKey("data").Value("data").Object().
ContainsKey("dictionarys").Value("dictionarys").Array()
})
})
})
AfterEach(func() {
_, err := pG.DB.Exec("DELETE FROM dictionarys WHERE true")
Expect(err).NotTo(HaveOccurred())
})
})
... ...