Moved away from local files to complete database driven articles
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"TheAdversary/schema"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"gorm.io/gorm/clause"
|
||||
)
|
||||
|
||||
type ArticleQueryOptions struct {
|
||||
@@ -15,16 +15,15 @@ type ArticleQueryOptions struct {
|
||||
Limit int
|
||||
}
|
||||
|
||||
func (db *Database) GetArticleByName(name string) (*schema.Article, error) {
|
||||
article := &schema.Article{}
|
||||
err := db.gormDB.Table("article").Where("name = ?", name).Scan(article).Error
|
||||
if article.Added == 0 {
|
||||
func (db *Database) GetArticleByName(name string) (article *Article, err error) {
|
||||
err = db.gormDB.Table("article").Where("name = ?", name).Scan(&article).Error
|
||||
if article == nil {
|
||||
return nil, sql.ErrNoRows
|
||||
}
|
||||
return article, err
|
||||
}
|
||||
|
||||
func (db *Database) GetArticles(query string, options ArticleQueryOptions) ([]*schema.Article, error) {
|
||||
func (db *Database) GetArticles(query string, options ArticleQueryOptions) ([]*Article, error) {
|
||||
request := db.gormDB.Table("article")
|
||||
var where bool
|
||||
if options.Name {
|
||||
@@ -69,9 +68,9 @@ func (db *Database) GetArticles(query string, options ArticleQueryOptions) ([]*s
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var articles []*schema.Article
|
||||
var articles []*Article
|
||||
for rows.Next() {
|
||||
article := &schema.Article{}
|
||||
article := &Article{}
|
||||
if err = db.gormDB.ScanRows(rows, article); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -81,12 +80,23 @@ func (db *Database) GetArticles(query string, options ArticleQueryOptions) ([]*s
|
||||
return articles, nil
|
||||
}
|
||||
|
||||
func (db *Database) AddArticle(article *schema.Article) error {
|
||||
return db.gormDB.Table("article").Create(article).Error
|
||||
func (db *Database) AddArticle(article Article, tags []Tag) error {
|
||||
if err := db.gormDB.Table("article").Create(&article).Select("id", &article.ID).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return db.gormDB.Table("article_tags").Create(ArticleTagsFromTagSlice(article, tags)).Error
|
||||
}
|
||||
|
||||
func (db *Database) UpdateArticle(name string, article *schema.Article) error {
|
||||
return db.gormDB.Table("article").Where("name = ?", name).Save(article).Error
|
||||
func (db *Database) UpdateArticle(article Article, tags []Tag) error {
|
||||
if err := db.gormDB.Table("article").Where("id = ?", article.ID).Save(article).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return db.gormDB.Table("article_tags").Clauses(clause.OnConflict{
|
||||
Columns: []clause.Column{{Name: "article_id"}, {Name: "tag_id"}},
|
||||
DoNothing: true,
|
||||
}).Create(ArticleTagsFromTagSlice(article, tags)).Error
|
||||
}
|
||||
|
||||
func (db *Database) DeleteArticlesByNames(names ...string) error {
|
||||
|
||||
38
database/schema.go
Normal file
38
database/schema.go
Normal file
@@ -0,0 +1,38 @@
|
||||
package database
|
||||
|
||||
import "fmt"
|
||||
|
||||
type Article struct {
|
||||
ID int
|
||||
Name string
|
||||
Title string
|
||||
Summary string
|
||||
Image string
|
||||
Added int64
|
||||
Modified int64
|
||||
Markdown string
|
||||
Html string
|
||||
}
|
||||
|
||||
func (a Article) ToArticleSummary() ArticleSummary {
|
||||
return ArticleSummary{
|
||||
Title: a.Title,
|
||||
Summary: a.Summary,
|
||||
Image: a.Image,
|
||||
Link: fmt.Sprintf("/article/%s", a.Name),
|
||||
}
|
||||
}
|
||||
|
||||
type ArticleSummary struct {
|
||||
Title string `json:"title"`
|
||||
Summary string `json:"summary"`
|
||||
Image string `json:"image"`
|
||||
Link string `json:"link"`
|
||||
}
|
||||
|
||||
type Author struct {
|
||||
ID int
|
||||
Name string
|
||||
Email string
|
||||
Password string
|
||||
}
|
||||
54
database/tag.go
Normal file
54
database/tag.go
Normal file
@@ -0,0 +1,54 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"gorm.io/gorm/clause"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Tag struct {
|
||||
ID int
|
||||
Name string
|
||||
}
|
||||
|
||||
func ArticleTagsFromTagSlice(article Article, tags []Tag) (tagsTable []ArticleTags) {
|
||||
for _, tag := range tags {
|
||||
tagsTable = append(tagsTable, ArticleTags{
|
||||
ArticleID: article.ID,
|
||||
TagID: tag.ID,
|
||||
})
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
type ArticleTags struct {
|
||||
ArticleID int
|
||||
TagID int
|
||||
}
|
||||
|
||||
func (db *Database) AddOrGetTags(names []string) (tags []Tag, err error) {
|
||||
for i, name := range names {
|
||||
names[i] = strings.ToLower(name)
|
||||
}
|
||||
|
||||
err = db.gormDB.Table("tag").Clauses(clause.OnConflict{
|
||||
Columns: []clause.Column{{Name: "name"}},
|
||||
DoNothing: true,
|
||||
}).Select("name").Create(&names).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = db.gormDB.Table("tag").Find(&tags).Where("name in (?)", &names).Error
|
||||
return
|
||||
}
|
||||
|
||||
func (db *Database) SetTags(article Article, tags []Tag) error {
|
||||
return db.gormDB.Table("tags").Clauses(clause.OnConflict{
|
||||
Columns: []clause.Column{{Name: "article_id"}, {Name: "tag_id"}},
|
||||
DoNothing: true,
|
||||
}).Create(ArticleTagsFromTagSlice(article, tags)).Error
|
||||
}
|
||||
|
||||
func (db *Database) RemoveTags(article Article, tags []Tag) error {
|
||||
return db.gormDB.Table("tags").Delete(ArticleTagsFromTagSlice(article, tags)).Error
|
||||
}
|
||||
Reference in New Issue
Block a user