diff --git a/bot/collection.go b/bot/collection.go index eb40344c9d7bc06f5991e2adc8edcf4b1243344a..5b44615132ab19ebdda4101847c58344144d0834 100644 --- a/bot/collection.go +++ b/bot/collection.go @@ -33,6 +33,19 @@ func (pbc *PlayBotCollection) GetByUrl(url string) (*site.Content, error) { } func (pbc *PlayBotCollection) InsertPost(post *Post) error { + err := pbc.Db.Insert(post.Content).Error() + if err != nil { + // TODO:Â check error type + var content site.Content + err = pbc.Db.Where(&site.Content{Url: post.Content.Url}).First(&content).Error() + + if err != nil { + return err + } + + post.Content = &content + } + pbc.Db.Insert(post) return nil } diff --git a/bot/db.go b/bot/db.go index 2e182f7d4bad440fabbc6f549d95732d5f15572e..d10c532b71ef8e79cbb51f4d2d3cd54fa10be87c 100644 --- a/bot/db.go +++ b/bot/db.go @@ -17,7 +17,10 @@ type DbParams struct { } type Db interface { - Insert(interface{}) error + Insert(interface{}) Db + Error() error + First(interface{}) Db + Where(interface{}) Db } type PlayBotDb struct { @@ -45,7 +48,24 @@ func NewPlayBotDb(dbParams DbParams) (Db, error) { return &PlayBotDb{db}, nil } -func (pdb *PlayBotDb) Insert(value interface{}) error { - pdb.db.Create(value) - return nil +func (pdb *PlayBotDb) Insert(value interface{}) Db { + return &PlayBotDb{ + pdb.db.Create(value), + } +} + +func (pdb *PlayBotDb) Error() error { + return pdb.db.Error +} + +func (pdb *PlayBotDb) First(value interface{}) Db { + return &PlayBotDb{ + pdb.db.First(value), + } +} + +func (pdb *PlayBotDb) Where(value interface{}) Db { + return &PlayBotDb{ + pdb.db.Where(value), + } } diff --git a/bot/post.go b/bot/post.go index f4d8d8c221b7b8c20b6d429a9639b6a06dce5e6a..8451b2044b27bd814496c6707365b9479d9806cd 100644 --- a/bot/post.go +++ b/bot/post.go @@ -7,11 +7,12 @@ import ( ) type Post struct { - Id int - Date time.Time - Author string `gorm:"column:sender_irc"` - Source string `gorm:"column:chan"` - Content *site.Content + Id int + Date time.Time + Author string `gorm:"column:sender_irc"` + Source string `gorm:"column:chan"` + Content *site.Content + ContentId int `gorm:"column:content"` } func (Post) TableName() string {