Initial release
This commit is contained in:
94
database/article.go
Normal file
94
database/article.go
Normal file
@@ -0,0 +1,94 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"TheAdversary/schema"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type ArticleQueryOptions struct {
|
||||
Name bool
|
||||
Title bool
|
||||
Summary bool
|
||||
From int64
|
||||
To int64
|
||||
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 {
|
||||
return nil, sql.ErrNoRows
|
||||
}
|
||||
return article, err
|
||||
}
|
||||
|
||||
func (db *Database) GetArticles(query string, options ArticleQueryOptions) ([]*schema.Article, error) {
|
||||
request := db.gormDB.Table("article")
|
||||
var where bool
|
||||
if options.Name {
|
||||
if where {
|
||||
request.Or("name LIKE ?", fmt.Sprintf("%%%s%%", query))
|
||||
} else {
|
||||
request.Where("name LIKE ?", fmt.Sprintf("%%%s%%", query))
|
||||
where = true
|
||||
}
|
||||
}
|
||||
if options.Title {
|
||||
if where {
|
||||
request.Or("title LIKE ?", fmt.Sprintf("%%%s%%", query))
|
||||
} else {
|
||||
request.Where("title LIKE ?", fmt.Sprintf("%%%s%%", query))
|
||||
where = true
|
||||
}
|
||||
}
|
||||
if options.Summary {
|
||||
if where {
|
||||
request.Or("summary LIKE ?", fmt.Sprintf("%%%s%%", query))
|
||||
} else {
|
||||
request.Where("summary LIKE ?", fmt.Sprintf("%%%s%%", query))
|
||||
where = true
|
||||
}
|
||||
}
|
||||
if !(options.From == 0 || options.To == 0) {
|
||||
var from, to int64
|
||||
if options.From != 0 {
|
||||
from = options.From
|
||||
}
|
||||
if options.To != 0 {
|
||||
to = options.To
|
||||
}
|
||||
request.Where("added BETWEEN ? AND ?", from, to)
|
||||
}
|
||||
if options.Limit > 0 {
|
||||
request.Limit(options.Limit)
|
||||
}
|
||||
rows, err := request.Rows()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var articles []*schema.Article
|
||||
for rows.Next() {
|
||||
article := &schema.Article{}
|
||||
if err = db.gormDB.ScanRows(rows, article); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
articles = append(articles, article)
|
||||
}
|
||||
|
||||
return articles, nil
|
||||
}
|
||||
|
||||
func (db *Database) AddArticle(article *schema.Article) error {
|
||||
return db.gormDB.Table("article").Create(article).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) DeleteArticlesByNames(names ...string) error {
|
||||
return db.gormDB.Table("article").Where("name IN (?)", names).Delete("*").Error
|
||||
}
|
||||
33
database/database.go
Normal file
33
database/database.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"gorm.io/driver/sqlite"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
var globDatabase *Database
|
||||
|
||||
type Database struct {
|
||||
gormDB *gorm.DB
|
||||
}
|
||||
|
||||
func newDatabaseConnection(dialector gorm.Dialector) (*Database, error) {
|
||||
db, err := gorm.Open(dialector)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &Database{db}, nil
|
||||
}
|
||||
|
||||
func NewSqlite3Connection(databaseFile string) (*Database, error) {
|
||||
return newDatabaseConnection(sqlite.Open(databaseFile))
|
||||
}
|
||||
|
||||
func GetDB() *Database {
|
||||
return globDatabase
|
||||
}
|
||||
|
||||
func SetGlobDB(database *Database) {
|
||||
globDatabase = database
|
||||
}
|
||||
Reference in New Issue
Block a user