Advertisement
Guest User

Untitled

a guest
May 19th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. package models
  2.  
  3. import (
  4. "context"
  5. "time"
  6.  
  7. "fmt"
  8.  
  9. "github.com/tikasan/goa"
  10. "github.com/tikasan/goa-simple-sample/app"
  11. )
  12.  
  13. // ListBottle returns an array of view: default.
  14. func (m *BottleDB) ListBottleFullScan(ctx context.Context, accountID int) []*app.BottleRelation {
  15. defer goa.MeasureSince([]string{"goa", "db", "bottle", "listbottle"}, time.Now())
  16.  
  17. var native []*Bottle
  18. //var c []*Category
  19. var objs []*app.BottleRelation
  20.  
  21. //m.Db.Debug().Find(&native).Related(&c, "Categories")
  22. rows, _ := m.Db.Debug().Table(m.TableName()).Select("*").Joins("LEFT JOIN bottle_categories ON bottle_categories.bottle_id = bottles.id").
  23. Joins("LEFT JOIN categories ON bottle_categories.category_id = categories.id").
  24. Rows()
  25.  
  26. for rows.Next() {
  27. fmt.Println(rows)
  28. }
  29.  
  30. for _, v := range native {
  31. for k2, v2 := range v.Categories {
  32. fmt.Println(k2, v2)
  33. }
  34. }
  35.  
  36. err := m.Db.Scopes(BottleFilterByAccount(accountID, m.Db)).
  37. Table(m.TableName()).
  38. Joins("LEFT JOIN bottle_categories ON bottle_categories.bottle_id = bottles.id").
  39. Joins("LEFT JOIN categories ON bottle_categories.category_id = categories.id").
  40. Find(&native).
  41. Error
  42.  
  43. //_ = m.Db.Scopes(BottleFilterByAccount(accountID, m.Db)).
  44. // Table(m.TableName()).
  45. // Preload("BottleCategories").
  46. // Preload("Account").
  47. // Related(&related, "Categories").
  48. // Find(&native).
  49. // Error
  50.  
  51. //for k, v := range native {
  52. // for k2, v2 := range v.BottleCategories {
  53. // native2 := []*BottleCategory{}
  54. // m.Db.Preload("Category").
  55. // Where("bottle_id = ?", v2.BottleID).
  56. // Find(&native2)
  57. // native[k].Categories = append(native[k].Categories, native2[k2].Category)
  58. // }
  59. //}
  60.  
  61. if err != nil {
  62. goa.LogError(ctx, "error listing Bottle", "error", err.Error())
  63. return objs
  64. }
  65.  
  66. for _, t := range native {
  67. objs = append(objs, t.BottleToBottleRelation())
  68. }
  69.  
  70. return objs
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement