This repository has been archived on 2023-11-13. You can view files and clone it, but cannot push or open issues or pull requests.
imagini/internal/db/media_items.go
2021-02-08 19:42:20 -05:00

35 lines
1.0 KiB
Go

package db
import (
log "github.com/sirupsen/logrus"
"gorm.io/gorm"
"reichard.io/imagini/graph/model"
)
func (dbm *DBManager) CreateMediaItem(mediaItem *model.MediaItem) error {
log.Debug("[db] Creating media item: ", mediaItem.FileName)
err := dbm.db.Create(mediaItem).Error
return err
}
func (dbm *DBManager) MediaItem(mediaItem *model.MediaItem) (int64, error) {
var count int64
err := dbm.db.Where(mediaItem).First(mediaItem).Count(&count).Error
return count, err
}
// 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
var mediaItems []*model.MediaItem
err := tx.Find(&mediaItems).Error
return mediaItems, pageResponse, err
}