2021-01-19 20:50:48 +00:00
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
2021-02-04 10:16:13 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2021-02-09 00:42:20 +00:00
|
|
|
"gorm.io/gorm"
|
2021-01-19 20:50:48 +00:00
|
|
|
|
2021-02-04 10:16:13 +00:00
|
|
|
"reichard.io/imagini/graph/model"
|
2021-01-19 20:50:48 +00:00
|
|
|
)
|
|
|
|
|
2021-02-04 10:16:13 +00:00
|
|
|
func (dbm *DBManager) CreateMediaItem(mediaItem *model.MediaItem) error {
|
|
|
|
log.Debug("[db] Creating media item: ", mediaItem.FileName)
|
|
|
|
err := dbm.db.Create(mediaItem).Error
|
|
|
|
return err
|
2021-01-19 20:50:48 +00:00
|
|
|
}
|
|
|
|
|
2021-02-07 02:11:44 +00:00
|
|
|
func (dbm *DBManager) MediaItem(mediaItem *model.MediaItem) (int64, error) {
|
2021-02-04 10:16:13 +00:00
|
|
|
var count int64
|
2021-02-07 02:11:44 +00:00
|
|
|
err := dbm.db.Where(mediaItem).First(mediaItem).Count(&count).Error
|
|
|
|
return count, err
|
|
|
|
}
|
2021-01-19 20:50:48 +00:00
|
|
|
|
2021-02-09 00:42:20 +00:00
|
|
|
// UserID, Filters, Sort, Page, Delete
|
|
|
|
func (dbm *DBManager) MediaItems(userID string, filters *model.MediaItemFilter, page *model.Page, order *model.Order) ([]*model.MediaItem, model.PageResponse, error) {
|
|
|
|
// Initial User Filter
|
|
|
|
tx := dbm.db.Session(&gorm.Session{}).Model(&model.MediaItem{}).Where("user_id == ?", userID)
|
|
|
|
|
|
|
|
// Dynamically Generate Base Query
|
|
|
|
tx, pageResponse := dbm.generateBaseQuery(tx, filters, page, order)
|
|
|
|
|
|
|
|
// Acquire Results
|
2021-02-07 02:11:44 +00:00
|
|
|
var mediaItems []*model.MediaItem
|
2021-02-09 00:42:20 +00:00
|
|
|
err := tx.Find(&mediaItems).Error
|
|
|
|
return mediaItems, pageResponse, err
|
2021-01-19 20:50:48 +00:00
|
|
|
}
|