show json array inside of a json object using gorm

huangapple go评论112阅读模式
英文:

show json array inside of a json object using gorm

问题

我正在尝试使用gorm进行查询。我有一个名为users的模型,就像这样:

  1. type Users struct {
  2. gorm.Model
  3. ID uint `gorm:"autoIncrement;unique" json:"id"`
  4. PhoneNumber string `gorm:"primaryKey" json:"phone_number"`
  5. Name string `gorm:"default:dear user" json:"name"`
  6. Rank uint `json:"rank"`
  7. Score uint `json:"score"`
  8. Image string `json:"image"`
  9. Email string `json:"email"`
  10. Address string `json:"address"`
  11. Birthday string `json:"birthday"`
  12. Biography string `json:"biography"`
  13. }

还有另一个模型,表示用户购买的课程。

  1. type UserCourse struct {
  2. CourseID uint `gorm:"primaryKey" json:"course_id"`
  3. UserPhoneNumber string `gorm:"primaryKey" json:"user_phone_number"`
  4. Progress uint `json:"progress"`
  5. CreatedAt time.Time
  6. UpdatedAt time.Time
  7. }

现在,我正在寻找一种方法,根据用户的分数和他们购买的课程返回前100名用户。换句话说,期望的JSON对象如下所示:

  1. {
  2. "users":[
  3. {
  4. "id":1,
  5. "phoneNumber":"99999999",
  6. "name":"test",
  7. "rank":1,
  8. "score":123456789,
  9. "image":"http://...",
  10. "email":"test@test.com",
  11. "address":"test",
  12. "birthday":"2021-01-01",
  13. "biography":"test here",
  14. "courses": [
  15. {
  16. "course_id":1,
  17. "user_phone_number":"99999999",
  18. "progress": 53,
  19. "created_at": "2021-01-01",
  20. "updated_at": "2021-01-01"
  21. },
  22. {
  23. "course_id":2,
  24. "user_phone_number":"99999999",
  25. "progress":100,
  26. "created_at":"2021-02-01",
  27. "updated_at":"2021-03-01"
  28. }
  29. ]
  30. }
  31. ]
  32. }

我知道我需要使用以下查询来获取前100名用户:

  1. database.myDatabase.Order("rank asc").Limit(100).Find(users)

但不幸的是,我不知道如何编写适用于所述输出的gorm查询。

英文:

I'm trying to use gorm for my queries. I have a model called users just like this:

  1. type Users struct {
  2. gorm.Model
  3. ID uint `gorm:"autoIncrement;unique" json:"id"`
  4. PhoneNumber string `gorm:"primaryKey" json:"phone_number"`
  5. Name string `gorm:"default:dear user" json:"name"`
  6. Rank uint `json:"rank"`
  7. Score uint `json:"score"`
  8. Image string `json:"image"`
  9. Email string `json:"email"`
  10. Address string `json:"address"`
  11. Birthday string `json:"birthday"`
  12. Biography string `json:"biography"`
  13. }

and another model which represents the courses that the user has purchased.

  1. type UserCourse struct {
  2. CourseID uint `gorm:"primaryKey" json:"course_id"`
  3. UserPhoneNumber string `gorm:"primaryKey" json:"user_phone_number"`
  4. Progress uint `json:"progress"`
  5. CreatedAt time.Time
  6. UpdatedAt time.Time
  7. }

now I am looking for a way to return top 100 users based on their score with the courses they have purchased. in the other word, the below JSON object is desirable:

  1. {
  2. "users":[
  3. {
  4. "id":1,
  5. "phoneNumber":"99999999",
  6. "name":"test",
  7. "rank":1,
  8. "score":123456789,
  9. "image":"http://...",
  10. "email":"test@test.com",
  11. "address":"test",
  12. "birthday":"2021-01-01",
  13. "biography":"test here",
  14. "courses": [
  15. {
  16. "course_id":1,
  17. "user_phone_number":"99999999",
  18. "progress": 53,
  19. "created_at": "2021-01-01",
  20. "updated_at": "2021-01-01",
  21. } ,
  22. {
  23. "course_id":2,
  24. "user_phone_number":"99999999",
  25. "progress":100,
  26. "created_at":"2021-02-01",
  27. "updated_at":"2021-03-01",
  28. }
  29. ]
  30. }
  31. ]
  32. }

I know I have to use the below query to get top 100 users:

  1. database.myDatabase.Order("rank asc").Limit(100).Find(users)

but unfortunately, I have no idea how to write the gorm suitable for the mentioned output.

答案1

得分: 1

如果你将Users模型扩展如下:

  1. type Users struct {
  2. gorm.Model
  3. ID uint `gorm:"autoIncrement;unique" json:"id"`
  4. PhoneNumber string `gorm:"primaryKey" json:"phone_number"`
  5. Name string `gorm:"default:dear user" json:"name"`
  6. Rank uint `json:"rank"`
  7. Score uint `json:"score"`
  8. Image string `json:"image"`
  9. Email string `json:"email"`
  10. Address string `json:"address"`
  11. Birthday string `json:"birthday"`
  12. Biography string `json:"biography"`
  13. Courses []*UserCourse `gorm:"foreignKey:UserPhoneNumber;references:PhoneNumber" json:"courses"`
  14. }

你可以通过以下方式将课程预加载到用户结构中:

  1. database.myDatabase.Preload("Courses").Order("rank asc").Limit(100).Find(users)
英文:

If you extend your Users model as such:

  1. type Users struct {
  2. gorm.Model
  3. ID uint `gorm:"autoIncrement;unique" json:"id"`
  4. PhoneNumber string `gorm:"primaryKey" json:"phone_number"`
  5. Name string `gorm:"default:dear user" json:"name"`
  6. Rank uint `json:"rank"`
  7. Score uint `json:"score"`
  8. Image string `json:"image"`
  9. Email string `json:"email"`
  10. Address string `json:"address"`
  11. Birthday string `json:"birthday"`
  12. Biography string `json:"biography"`
  13. Courses []*UserCourse `gorm:"foreignKey:UserPhoneNumber;references:PhoneNumber" json:"courses"`
  14. }

you can then preload the courses into the user struct by using:

  1. database.myDatabase.Preload("Courses").Order("rank asc").Limit(100).Find(users)

huangapple
  • 本文由 发表于 2021年8月18日 13:02:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/68826817.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定