From 9a79d575d5c8b9f24f697fa6ba687b9b72b9899b Mon Sep 17 00:00:00 2001
From: Alexandre Morignot <erdnaxeli@cervoi.se>
Date: Thu, 6 Oct 2016 00:57:52 +0200
Subject: [PATCH] Post works ! Except for tags

---
 bot/collection.go | 13 +++++++++++++
 bot/db.go         | 28 ++++++++++++++++++++++++----
 bot/post.go       | 11 ++++++-----
 3 files changed, 43 insertions(+), 9 deletions(-)

diff --git a/bot/collection.go b/bot/collection.go
index eb40344..5b44615 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 2e182f7..d10c532 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 f4d8d8c..8451b20 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 {
-- 
GitLab