Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import (
- //"github.com/jinzhu/gorm"
- _ "github.com/jinzhu/gorm/dialects/postgres"
- //"gitlab.com/nordfa/opportunity/app/lib/messages/flows_messages"
- //"sort"
- "time"
- )
- // models
- type ArticleType struct {
- ID uint `gorm:"primary_key" json:"-"`
- CreatedAt time.Time `json:"created_at"`
- UpdatedAt time.Time `json:"updated_at"`
- Title string `gorm:"size:255;unique_index" json:"title"`
- Articles []Article `json:"-"`
- }
- type Article struct {
- ID uint `gorm:"primary_key" json:"-"`
- CreatedAt time.Time `json:"created_at"`
- UpdatedAt time.Time `json:"updated_at"`
- Title string `gorm:"size:255" json:"title"`
- SecondTitle string `json:"second_title"`
- ArticleType ArticleType `json:"article_type"`
- ArticleTypeId uint `gorm:"index" json:"-"`
- Block Block `json:"block"`
- BlockId uint `gorm:"index" json:"-"`
- }
- type Block struct {
- ID uint `gorm:"primary_key"`
- CreatedAt time.Time `json:"created_at"`
- UpdatedAt time.Time `json:"updated_at"`
- Title string `json:"title"`
- Articles []Article `json:"collection"`
- }
- func main() {
- //db, err := gorm.Open(
- // "postgres",
- // fmt.Sprintf("host=%s user=%s dbname=%s sslmode=disable password=%d",
- // "localhost",
- // "apple",
- // "mytestsdb",
- // "123",
- // ))
- //
- //if err != nil {
- // panic(err)
- //}
- //
- //defer db.Close()
- //
- //db.AutoMigrate(
- // &ArticleType{},
- // &Article{},
- // &Block{},
- //)
- //
- //block := Block{Title: "Block 1"}
- //db.Create(&block)
- //
- //articleType := ArticleType{Title: "news"}
- //db.FirstOrCreate(&articleType, ArticleType{Title: "news"})
- //
- //article := Article{
- // Title: "Article Title 1",
- // ArticleTypeId: articleType.ID,
- // BlockId: block.ID,
- //}
- //
- //db.Create(&article)
- //
- //article = Article{
- // Title: "Article Title 2",
- // ArticleTypeId: articleType.ID,
- // BlockId: block.ID,
- //}
- //
- //db.Create(&article)
- //
- //article = Article{
- // Title: "Article Title 3",
- // ArticleTypeId: articleType.ID,
- // BlockId: block.ID,
- //}
- //
- //db.Create(&article)
- //
- //
- //var (
- // nonRelatedBlocks []flows_messages.Block
- // articles []flows_messages.Article
- //)
- //
- //if err := db.Find(&nonRelatedBlocks).Error; err != nil {
- // fmt.Println(err)
- //}
- //
- //blocksmap := make(map[uint]*flows_messages.Block)
- //blockIds := []uint{}
- //
- //for i, block := range nonRelatedBlocks {
- // blocksmap[block.ID] = &nonRelatedBlocks[i]
- // blockIds = append(blockIds, block.ID)
- //}
- //
- //fields := []string{
- // "articles.id", "articles.created_at", "articles.updated_at", "articles.title", "articles.second_title", "articles.block_id",
- // "article_types.id", "article_types.title AS at_title",
- //}
- //
- //err = db.Select(fields).
- // Joins("JOIN article_types ON articles.article_type_id = article_types.id").
- // Where("articles.block_id IN (?)", blockIds).
- // Order("articles.updated_at DESC").
- // Find(&articles).
- // Error
- //
- //if err != nil {
- // fmt.Println(err)
- //}
- //
- //for _, article := range articles {
- // if b, ok := blocksmap[article.BlockId]; !ok {
- // fmt.Printf("blocksmap not contains article.BlockId: %d", article.BlockId)
- // return
- // } else {
- // b.Articles = append(b.Articles, article)
- // }
- //}
- //
- //blocks := []flows_messages.Block{}
- //
- //for _, block := range blocksmap {
- // blocks = append(blocks, *block)
- //}
- //
- //sort.Slice(blocks, func(i, j int) bool {
- // return blocks[i].UpdatedAt.UnixNano() > blocks[j].UpdatedAt.UnixNano()
- //})
- //
- //fmt.Println(blocks)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement