Articles and tags are now inserted into DB correctly
This commit is contained in:
		@@ -9,7 +9,6 @@ type Article struct {
 | 
			
		||||
	Created   time.Time
 | 
			
		||||
	Desc      string
 | 
			
		||||
	Content   string
 | 
			
		||||
	Tags      []string
 | 
			
		||||
	Published bool
 | 
			
		||||
	ID        int64
 | 
			
		||||
	AuthorID  int64
 | 
			
		||||
 
 | 
			
		||||
@@ -35,6 +35,18 @@ func OpenDB(dbName string) (*DB, error) {
 | 
			
		||||
	return &db, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (db *DB) UpdateAttribute(table string, id int64, attribute string, val interface{}) error {
 | 
			
		||||
	query := `
 | 
			
		||||
    UPDATE ?
 | 
			
		||||
    SET ? = ?
 | 
			
		||||
    WHERE id = ?
 | 
			
		||||
    `
 | 
			
		||||
	if _, err := db.Exec(query, table, attribute, val, id); err != nil {
 | 
			
		||||
		return fmt.Errorf("error updating article in DB: %v", err)
 | 
			
		||||
	}
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (db *DB) AddUser(user *User, pass string) error {
 | 
			
		||||
	hashedPass, err := bcrypt.GenerateFromPassword([]byte(pass), bcrypt.DefaultCost)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
@@ -173,7 +185,7 @@ func (db *DB) GetUser(id int64) (*User, error) {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (db *DB) AddTag(tagName string) error {
 | 
			
		||||
	query := "INSERT INTO tags name VALUES ?"
 | 
			
		||||
	query := "INSERT INTO tags (name) VALUES (?)"
 | 
			
		||||
	if _, err := db.Exec(query, tagName); err != nil {
 | 
			
		||||
		return fmt.Errorf("error inserting tag into DB: %v", err)
 | 
			
		||||
	}
 | 
			
		||||
@@ -195,21 +207,31 @@ func (db *DB) GetTagList() ([]Tag, error) {
 | 
			
		||||
		}
 | 
			
		||||
		tagList = append(tagList, tag)
 | 
			
		||||
	}
 | 
			
		||||
	if err = rows.Err(); err != nil {
 | 
			
		||||
		return nil, fmt.Errorf("error iterating through rows: %v", err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return tagList, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (db *DB) AddArticle(a *Article) error {
 | 
			
		||||
func (db *DB) AddArticle(a *Article) (int64, error) {
 | 
			
		||||
	query := `
 | 
			
		||||
    INSERT INTO articles
 | 
			
		||||
        (title, description, content, published, author_id)
 | 
			
		||||
    VALUES
 | 
			
		||||
        (?, ?, ?, ?)
 | 
			
		||||
        (?, ?, ?, ?, ?)
 | 
			
		||||
    `
 | 
			
		||||
	if _, err := db.Exec(query, a.Title, a.Desc, a.Content, a.Published, a.AuthorID); err != nil {
 | 
			
		||||
		return fmt.Errorf("error inserting article into DB: %v", err)
 | 
			
		||||
 | 
			
		||||
	result, err := db.Exec(query, a.Title, a.Desc, a.Content, a.Published, a.AuthorID)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return 0, fmt.Errorf("error inserting article into DB: %v", err)
 | 
			
		||||
	}
 | 
			
		||||
	return nil
 | 
			
		||||
	id, err := result.LastInsertId()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return 0, fmt.Errorf("error retrieving last ID: %v", err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return id, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (db *DB) GetArticle(id int64) (*Article, error) {
 | 
			
		||||
@@ -254,14 +276,27 @@ func (db *DB) GetUnpublishedArticles() ([]Article, error) {
 | 
			
		||||
	return articleList, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (db *DB) UpdateArticle(id int64, attribute string, val interface{}) error {
 | 
			
		||||
	query := `
 | 
			
		||||
    UPDATE articles
 | 
			
		||||
    SET ? = ?
 | 
			
		||||
    WHERE id = ?
 | 
			
		||||
    `
 | 
			
		||||
	if _, err := db.Exec(query, attribute, val, id); err != nil {
 | 
			
		||||
		return fmt.Errorf("error updating article in DB: %v", err)
 | 
			
		||||
func (db *DB) WriteArticleTags(articleID int64, tagIDs []int64) error {
 | 
			
		||||
	tx, err := db.Begin()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return fmt.Errorf("error starting transaction: %v", err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	for _, tagID := range tagIDs {
 | 
			
		||||
		query := `
 | 
			
		||||
        INSERT INTO articles_tags (article_id, tag_id)
 | 
			
		||||
        VALUES (?, ?)
 | 
			
		||||
        `
 | 
			
		||||
		if _, err := tx.Exec(query, articleID, tagID); err != nil {
 | 
			
		||||
			if rollbackErr := tx.Rollback(); rollbackErr != nil {
 | 
			
		||||
				log.Fatalf("error: transaction error: %v, rollback error: %v", err, rollbackErr)
 | 
			
		||||
			}
 | 
			
		||||
			return fmt.Errorf("error inserting into articles_tags: %v", err)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if err = tx.Commit(); err != nil {
 | 
			
		||||
		return fmt.Errorf("error committing transaction: %v", err)
 | 
			
		||||
	}
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,6 +1,6 @@
 | 
			
		||||
package data
 | 
			
		||||
 | 
			
		||||
type Tag struct {
 | 
			
		||||
	ID   int64
 | 
			
		||||
	Name string
 | 
			
		||||
	ID   int64
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -48,7 +48,8 @@ func main() {
 | 
			
		||||
	store := data.NewCookieStore(key)
 | 
			
		||||
 | 
			
		||||
	mux := http.NewServeMux()
 | 
			
		||||
	mux.Handle("/web/static/", http.StripPrefix("/web/static/", http.FileServer(http.Dir("web/static/"))))
 | 
			
		||||
	mux.Handle("/web/static/", http.StripPrefix("/web/static/",
 | 
			
		||||
		http.FileServer(http.Dir("web/static/"))))
 | 
			
		||||
	mux.HandleFunc("/", ui.HomePage(db, store))
 | 
			
		||||
 | 
			
		||||
	mux.HandleFunc("GET /create-tag/", ui.CreateTag)
 | 
			
		||||
 
 | 
			
		||||
@@ -65,9 +65,6 @@ func FinishArticle(db *data.DB, s *data.CookieStore) http.HandlerFunc {
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		r.ParseForm()
 | 
			
		||||
		article.Tags = append(article.Tags, r.Form["tags"]...)
 | 
			
		||||
 | 
			
		||||
		session, err := s.Get(r, "cookie")
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			tmpl, err := template.ParseFiles("web/templates/login.html")
 | 
			
		||||
@@ -76,7 +73,29 @@ func FinishArticle(db *data.DB, s *data.CookieStore) http.HandlerFunc {
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		article.AuthorID = session.Values["id"].(int64)
 | 
			
		||||
		db.AddArticle(article)
 | 
			
		||||
		article.ID, err = db.AddArticle(article)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			log.Println(err)
 | 
			
		||||
			http.Error(w, err.Error(), http.StatusInternalServerError)
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		r.ParseForm()
 | 
			
		||||
		tags := make([]int64, 0)
 | 
			
		||||
		for _, tag := range r.Form["tags"] {
 | 
			
		||||
			tagID, err := strconv.ParseInt(tag, 10, 64)
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				log.Println(err)
 | 
			
		||||
				http.Error(w, err.Error(), http.StatusInternalServerError)
 | 
			
		||||
				return
 | 
			
		||||
			}
 | 
			
		||||
			tags = append(tags, tagID)
 | 
			
		||||
		}
 | 
			
		||||
		if err = db.WriteArticleTags(article.ID, tags); err != nil {
 | 
			
		||||
			log.Println(err)
 | 
			
		||||
			http.Error(w, err.Error(), http.StatusInternalServerError)
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		tmpl, err := template.ParseFiles("web/templates/hub.html")
 | 
			
		||||
		tmpl = template.Must(tmpl, err)
 | 
			
		||||
@@ -135,7 +154,7 @@ func PublishArticle(db *data.DB, c *data.Channel, s *data.CookieStore) http.Hand
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		if err = db.UpdateArticle(id, "published", true); err != nil {
 | 
			
		||||
		if err = db.UpdateAttribute("articles", id, "published", true); err != nil {
 | 
			
		||||
			log.Println(err)
 | 
			
		||||
			http.Error(w, err.Error(), http.StatusInternalServerError)
 | 
			
		||||
			return
 | 
			
		||||
@@ -160,7 +179,7 @@ func PublishArticle(db *data.DB, c *data.Channel, s *data.CookieStore) http.Hand
 | 
			
		||||
			PubDate:     article.Created.Format(time.RFC1123Z),
 | 
			
		||||
			Description: article.Desc,
 | 
			
		||||
			Content:     &rss.Content{Value: article.Content},
 | 
			
		||||
			Categories:  article.Tags,
 | 
			
		||||
			// Categories:  article.Tags,
 | 
			
		||||
		})
 | 
			
		||||
		c.Save("tmp/rss.gob")
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user