fix(park):修复园区不能搜索的问题。

This commit is contained in:
徐涛 2022-09-06 15:28:27 +08:00
parent 954285e426
commit 8aa4e38760
2 changed files with 18 additions and 6 deletions

View File

@ -49,7 +49,8 @@ func listAllParksUnderSessionUser(c *gin.Context) {
result.Unauthorized(err.Error()) result.Unauthorized(err.Error())
return return
} }
parks, err := service.ParkService.ListAllParkBelongsTo(userSession.Uid) keyword := c.DefaultQuery("keyword", "")
parks, err := service.ParkService.ListAllParkBelongsTo(userSession.Uid, keyword)
if err != nil { if err != nil {
result.Error(http.StatusInternalServerError, err.Error()) result.Error(http.StatusInternalServerError, err.Error())
return return
@ -60,7 +61,8 @@ func listAllParksUnderSessionUser(c *gin.Context) {
func listAllParksUnderSpecificUser(c *gin.Context) { func listAllParksUnderSpecificUser(c *gin.Context) {
result := response.NewResult(c) result := response.NewResult(c)
requestUserId := c.Param("uid") requestUserId := c.Param("uid")
parks, err := service.ParkService.ListAllParkBelongsTo(requestUserId) keyword := c.DefaultQuery("keyword", "")
parks, err := service.ParkService.ListAllParkBelongsTo(requestUserId, keyword)
if err != nil { if err != nil {
result.Error(http.StatusInternalServerError, err.Error()) result.Error(http.StatusInternalServerError, err.Error())
return return

View File

@ -73,19 +73,29 @@ func (_ParkService) DeletePark(uid, pid string) error {
return nil return nil
} }
func (_ParkService) ListAllParkBelongsTo(uid string) ([]model.Park, error) { func (_ParkService) ListAllParkBelongsTo(uid, keyword string) ([]model.Park, error) {
if parks, _ := cache.RetreiveSearch[[]model.Park]("park", "belong", uid); parks != nil { if parks, _ := cache.RetreiveSearch[[]model.Park]("park", "belong", uid, keyword); parks != nil {
return *parks, nil return *parks, nil
} }
cond := builder.NewCond().And(builder.Eq{"user_id": uid})
if len(keyword) > 0 {
cond = cond.And(
builder.Like{"name", keyword}.
Or(builder.Like{"abbr", keyword}).
Or(builder.Like{"address", keyword}).
Or(builder.Like{"contact", keyword}).
Or(builder.Like{"phone", keyword}),
)
}
parks := make([]model.Park, 0) parks := make([]model.Park, 0)
err := global.DBConn. err := global.DBConn.
Where(builder.Eq{"user_id": uid}). Where(cond).
NoAutoCondition(). NoAutoCondition().
Find(&parks) Find(&parks)
if err != nil { if err != nil {
return make([]model.Park, 0), err return make([]model.Park, 0), err
} }
cache.CacheSearch(parks, []string{"park"}, "park", "belong", uid) cache.CacheSearch(parks, []string{"park"}, "park", "belong", uid, keyword)
return parks, nil return parks, nil
} }