Advertisement
Guest User

Untitled

a guest
May 28th, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.43 KB | None | 0 0
  1. import (
  2. //"github.com/jinzhu/gorm"
  3. _ "github.com/jinzhu/gorm/dialects/postgres"
  4. //"gitlab.com/nordfa/opportunity/app/lib/messages/flows_messages"
  5. //"sort"
  6. "time"
  7. )
  8.  
  9. // models
  10. type ArticleType struct {
  11. ID uint `gorm:"primary_key" json:"-"`
  12. CreatedAt time.Time `json:"created_at"`
  13. UpdatedAt time.Time `json:"updated_at"`
  14.  
  15. Title string `gorm:"size:255;unique_index" json:"title"`
  16.  
  17. Articles []Article `json:"-"`
  18. }
  19.  
  20. type Article struct {
  21. ID uint `gorm:"primary_key" json:"-"`
  22. CreatedAt time.Time `json:"created_at"`
  23. UpdatedAt time.Time `json:"updated_at"`
  24.  
  25. Title string `gorm:"size:255" json:"title"`
  26. SecondTitle string `json:"second_title"`
  27.  
  28. ArticleType ArticleType `json:"article_type"`
  29. ArticleTypeId uint `gorm:"index" json:"-"`
  30.  
  31. Block Block `json:"block"`
  32. BlockId uint `gorm:"index" json:"-"`
  33. }
  34.  
  35. type Block struct {
  36. ID uint `gorm:"primary_key"`
  37. CreatedAt time.Time `json:"created_at"`
  38. UpdatedAt time.Time `json:"updated_at"`
  39. Title string `json:"title"`
  40.  
  41. Articles []Article `json:"collection"`
  42. }
  43.  
  44. func main() {
  45. //db, err := gorm.Open(
  46. // "postgres",
  47. // fmt.Sprintf("host=%s user=%s dbname=%s sslmode=disable password=%d",
  48. // "localhost",
  49. // "apple",
  50. // "mytestsdb",
  51. // "123",
  52. // ))
  53. //
  54. //if err != nil {
  55. // panic(err)
  56. //}
  57. //
  58. //defer db.Close()
  59. //
  60. //db.AutoMigrate(
  61. // &ArticleType{},
  62. // &Article{},
  63. // &Block{},
  64. //)
  65. //
  66. //block := Block{Title: "Block 1"}
  67. //db.Create(&block)
  68. //
  69. //articleType := ArticleType{Title: "news"}
  70. //db.FirstOrCreate(&articleType, ArticleType{Title: "news"})
  71. //
  72. //article := Article{
  73. // Title: "Article Title 1",
  74. // ArticleTypeId: articleType.ID,
  75. // BlockId: block.ID,
  76. //}
  77. //
  78. //db.Create(&article)
  79. //
  80. //article = Article{
  81. // Title: "Article Title 2",
  82. // ArticleTypeId: articleType.ID,
  83. // BlockId: block.ID,
  84. //}
  85. //
  86. //db.Create(&article)
  87. //
  88. //article = Article{
  89. // Title: "Article Title 3",
  90. // ArticleTypeId: articleType.ID,
  91. // BlockId: block.ID,
  92. //}
  93. //
  94. //db.Create(&article)
  95. //
  96. //
  97. //var (
  98. // nonRelatedBlocks []flows_messages.Block
  99. // articles []flows_messages.Article
  100. //)
  101. //
  102. //if err := db.Find(&nonRelatedBlocks).Error; err != nil {
  103. // fmt.Println(err)
  104. //}
  105. //
  106. //blocksmap := make(map[uint]*flows_messages.Block)
  107. //blockIds := []uint{}
  108. //
  109. //for i, block := range nonRelatedBlocks {
  110. // blocksmap[block.ID] = &nonRelatedBlocks[i]
  111. // blockIds = append(blockIds, block.ID)
  112. //}
  113. //
  114. //fields := []string{
  115. // "articles.id", "articles.created_at", "articles.updated_at", "articles.title", "articles.second_title", "articles.block_id",
  116. // "article_types.id", "article_types.title AS at_title",
  117. //}
  118. //
  119. //err = db.Select(fields).
  120. // Joins("JOIN article_types ON articles.article_type_id = article_types.id").
  121. // Where("articles.block_id IN (?)", blockIds).
  122. // Order("articles.updated_at DESC").
  123. // Find(&articles).
  124. // Error
  125. //
  126. //if err != nil {
  127. // fmt.Println(err)
  128. //}
  129. //
  130. //for _, article := range articles {
  131. // if b, ok := blocksmap[article.BlockId]; !ok {
  132. // fmt.Printf("blocksmap not contains article.BlockId: %d", article.BlockId)
  133. // return
  134. // } else {
  135. // b.Articles = append(b.Articles, article)
  136. // }
  137. //}
  138. //
  139. //blocks := []flows_messages.Block{}
  140. //
  141. //for _, block := range blocksmap {
  142. // blocks = append(blocks, *block)
  143. //}
  144. //
  145. //sort.Slice(blocks, func(i, j int) bool {
  146. // return blocks[i].UpdatedAt.UnixNano() > blocks[j].UpdatedAt.UnixNano()
  147. //})
  148. //
  149. //fmt.Println(blocks)
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement