stash/pkg/models/querybuilder_scraped_item.go

114 lines
3.2 KiB
Go
Raw Normal View History

2019-02-09 12:30:49 +00:00
package models
import (
"database/sql"
"github.com/jmoiron/sqlx"
2019-02-14 23:42:52 +00:00
"github.com/stashapp/stash/pkg/database"
2019-02-09 12:30:49 +00:00
)
type ScrapedItemQueryBuilder struct{}
2019-02-09 12:30:49 +00:00
func NewScrapedItemQueryBuilder() ScrapedItemQueryBuilder {
return ScrapedItemQueryBuilder{}
2019-02-09 12:30:49 +00:00
}
func (qb *ScrapedItemQueryBuilder) Create(newScrapedItem ScrapedItem, tx *sqlx.Tx) (*ScrapedItem, error) {
2019-02-09 12:30:49 +00:00
ensureTx(tx)
result, err := tx.NamedExec(
`INSERT INTO scraped_items (title, description, url, date, rating, tags, models, episode, gallery_filename,
gallery_url, video_filename, video_url, studio_id, created_at, updated_at)
VALUES (:title, :description, :url, :date, :rating, :tags, :models, :episode, :gallery_filename,
:gallery_url, :video_filename, :video_url, :studio_id, :created_at, :updated_at)
`,
newScrapedItem,
)
if err != nil {
return nil, err
}
scrapedItemID, err := result.LastInsertId()
if err != nil {
return nil, err
}
if err := tx.Get(&newScrapedItem, `SELECT * FROM scraped_items WHERE id = ? LIMIT 1`, scrapedItemID); err != nil {
return nil, err
}
return &newScrapedItem, nil
}
func (qb *ScrapedItemQueryBuilder) Update(updatedScrapedItem ScrapedItem, tx *sqlx.Tx) (*ScrapedItem, error) {
2019-02-09 12:30:49 +00:00
ensureTx(tx)
_, err := tx.NamedExec(
`UPDATE scraped_items SET `+SQLGenKeys(updatedScrapedItem)+` WHERE scraped_items.id = :id`,
2019-02-09 12:30:49 +00:00
updatedScrapedItem,
)
if err != nil {
return nil, err
}
if err := tx.Get(&updatedScrapedItem, `SELECT * FROM scraped_items WHERE id = ? LIMIT 1`, updatedScrapedItem.ID); err != nil {
return nil, err
}
return &updatedScrapedItem, nil
}
func (qb *ScrapedItemQueryBuilder) Find(id int) (*ScrapedItem, error) {
2019-02-09 12:30:49 +00:00
query := "SELECT * FROM scraped_items WHERE id = ? LIMIT 1"
args := []interface{}{id}
return qb.queryScrapedItem(query, args, nil)
}
2019-05-27 19:34:26 +00:00
func (qb *ScrapedItemQueryBuilder) All() ([]*ScrapedItem, error) {
return qb.queryScrapedItems(selectAll("scraped_items")+qb.getScrapedItemsSort(nil), nil, nil)
2019-02-09 12:30:49 +00:00
}
func (qb *ScrapedItemQueryBuilder) getScrapedItemsSort(findFilter *FindFilterType) string {
2019-02-09 12:30:49 +00:00
var sort string
var direction string
if findFilter == nil {
sort = "id" // TODO studio_id and title
direction = "ASC"
} else {
sort = findFilter.GetSort("id")
direction = findFilter.GetDirection()
}
return getSort(sort, direction, "scraped_items")
}
func (qb *ScrapedItemQueryBuilder) queryScrapedItem(query string, args []interface{}, tx *sqlx.Tx) (*ScrapedItem, error) {
2019-02-09 12:30:49 +00:00
results, err := qb.queryScrapedItems(query, args, tx)
if err != nil || len(results) < 1 {
return nil, err
}
2019-05-27 19:34:26 +00:00
return results[0], nil
2019-02-09 12:30:49 +00:00
}
2019-05-27 19:34:26 +00:00
func (qb *ScrapedItemQueryBuilder) queryScrapedItems(query string, args []interface{}, tx *sqlx.Tx) ([]*ScrapedItem, error) {
2019-02-09 12:30:49 +00:00
var rows *sqlx.Rows
var err error
if tx != nil {
rows, err = tx.Queryx(query, args...)
} else {
rows, err = database.DB.Queryx(query, args...)
}
if err != nil && err != sql.ErrNoRows {
return nil, err
}
defer rows.Close()
2019-05-27 19:34:26 +00:00
scrapedItems := make([]*ScrapedItem, 0)
2019-02-09 12:30:49 +00:00
for rows.Next() {
2019-05-27 19:34:26 +00:00
scrapedItem := ScrapedItem{}
2019-02-09 12:30:49 +00:00
if err := rows.StructScan(&scrapedItem); err != nil {
return nil, err
}
2019-05-27 19:34:26 +00:00
scrapedItems = append(scrapedItems, &scrapedItem)
2019-02-09 12:30:49 +00:00
}
if err := rows.Err(); err != nil {
return nil, err
}
return scrapedItems, nil
}