game.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package controller
  2. import (
  3. "designs/app/common/request"
  4. "designs/app/common/response"
  5. "designs/global"
  6. "designs/model"
  7. "github.com/gin-gonic/gin"
  8. )
  9. func GameList(c *gin.Context) {
  10. form := request.Check(c, &struct {
  11. Gid string `form:"gid" binding:""`
  12. Pid string `form:"pid" binding:""`
  13. }{})
  14. query := global.App.DB.Table(model.TableGame)
  15. if form.Gid != "" {
  16. query = query.Where("gid", form.Gid)
  17. }
  18. if form.Pid != "" {
  19. query = query.Where("pid", form.Pid)
  20. }
  21. var data []model.Game
  22. err := query.Select("gid", "pid", "gameName").Scan(&data).Error
  23. if err != nil {
  24. response.Fail(c, 1001, err.Error())
  25. return
  26. }
  27. type game struct {
  28. Pid string `json:"pid" gorm:"column:pid; "`
  29. Gid string `json:"gid" gorm:"column:gid; "`
  30. GameName string `json:"gameName" gorm:"column:gameName; "`
  31. }
  32. res := make(map[string][]game)
  33. for _, v := range data {
  34. res[v.Pid] = append(res[v.Pid], game{
  35. Pid: v.Pid,
  36. Gid: v.Gid,
  37. GameName: v.GameName,
  38. })
  39. }
  40. response.Success(c, gin.H{
  41. "data": res,
  42. })
  43. }