Commit e67001ee authored by Christian Pointner's avatar Christian Pointner
Browse files

no pre-defined datatypes for plural form of objects

parent ab4e2799
Pipeline #603 failed with stages
in 1 minute and 46 seconds
......@@ -58,7 +58,7 @@ func (api *API) ListShows(c *gin.Context) {
}
}
shows := store.Shows{}
shows := []store.Show{}
for _, show := range showsMap {
shows = append(shows, show)
}
......@@ -66,7 +66,7 @@ func (api *API) ListShows(c *gin.Context) {
if offset > 0 {
if offset >= len(shows) {
c.JSON(http.StatusOK, ShowsListing{Shows: store.Shows{}})
c.JSON(http.StatusOK, ShowsListing{Shows: []store.Show{}})
return
}
shows = shows[offset:]
......
......@@ -45,17 +45,17 @@ type ErrorResponse struct {
// Shows
type ShowsListing struct {
Shows store.Shows `json:"results"`
Shows []store.Show `json:"results"`
}
// Imports
type JobsListing struct {
Jobs importer.Jobs `json:"results"`
Jobs []*importer.Job `json:"results"`
}
// Files
type FilesListing struct {
Files store.Files `json:"results"`
Files []store.File `json:"results"`
}
type FileCreateRequest struct {
......@@ -64,7 +64,7 @@ type FileCreateRequest struct {
type FileUsageListing struct {
Usage struct {
Playlists store.Playlists `json:"playlists"`
Playlists []store.Playlist `json:"playlists"`
} `json:"results"`
}
......@@ -74,5 +74,5 @@ type FileImportLogs struct {
// Playlists
type PlaylistsListing struct {
Playlists store.Playlists `json:"results"`
Playlists []store.Playlist `json:"results"`
}
......@@ -51,7 +51,7 @@ type Importer struct {
httpC *http.Client
}
func (im *Importer) ListJobs(show string, offset, limit int) (Jobs, error) {
func (im *Importer) ListJobs(show string, offset, limit int) ([]*Job, error) {
return im.jobs.ListJobs(show, offset, limit), nil // for now error is always nil but this might change later
}
......
......@@ -57,8 +57,6 @@ type Job struct {
}
}
type Jobs []*Job
func (job *Job) run() error {
// this function must never-ever hang or we block a worker forever - watch your context kids!!
......
......@@ -49,13 +49,13 @@ type jobInventory struct {
mu sync.RWMutex
}
func (i *jobInventory) ListJobs(show string, offset, limit int) (jobs Jobs) {
func (i *jobInventory) ListJobs(show string, offset, limit int) (jobs []*Job) {
i.mu.RLock()
defer i.mu.RUnlock()
ig, exists := i.shows[show]
if !exists {
return Jobs{}
return []*Job{}
}
for _, job := range ig.jobs {
......@@ -65,7 +65,7 @@ func (i *jobInventory) ListJobs(show string, offset, limit int) (jobs Jobs) {
if offset > 0 {
if offset >= len(jobs) {
return Jobs{}
return []*Job{}
}
jobs = jobs[offset:]
}
......@@ -75,7 +75,7 @@ func (i *jobInventory) ListJobs(show string, offset, limit int) (jobs Jobs) {
return
}
func (i *jobInventory) SubscribeJobs(show string) (jobs Jobs, changed <-chan struct{}) {
func (i *jobInventory) SubscribeJobs(show string) (jobs []*Job, changed <-chan struct{}) {
i.mu.RLock()
defer i.mu.RUnlock()
......
......@@ -35,7 +35,7 @@ func (st *Store) GetFilePath(show string, id uint64) string {
return filepath.Join(st.getShowPath(show), filename)
}
func (st *Store) ListFiles(show string, offset, limit int) (files Files, err error) {
func (st *Store) ListFiles(show string, offset, limit int) (files []File, err error) {
err = st.db.Where("show_name = ?", show).Order("id").Offset(offset).Limit(limit).Find(&files).Error
return
}
......@@ -181,13 +181,13 @@ func (st *Store) UpdateFileSourceHash(show string, id uint64, hash string) (*Fil
return st.updateFile(show, id, "source__hash", hash)
}
func (st *Store) getFileUsage(id uint64, playlists *Playlists) (err error) {
func (st *Store) getFileUsage(id uint64, playlists []Playlist) (err error) {
sub := st.db.Model(PlaylistEntry{}).Select("playlist_id").Where("file_id = ?", id).Group("playlist_id").SubQuery()
err = st.db.Where("id in ?", sub).Find(playlists).Error
return
}
func (st *Store) GetFileUsage(show string, id uint64) (playlists Playlists, err error) {
func (st *Store) GetFileUsage(show string, id uint64) (playlists []Playlist, err error) {
// make sure the file exists and actually belongs to <show> since permissions are enforced
// based on show membership
cnt := 0
......@@ -197,7 +197,7 @@ func (st *Store) GetFileUsage(show string, id uint64) (playlists Playlists, err
if cnt == 0 {
return nil, ErrNotFound
}
err = st.getFileUsage(id, &playlists)
err = st.getFileUsage(id, playlists)
return
}
......@@ -208,7 +208,7 @@ func (st *Store) DeleteFile(show string, id uint64) (err error) {
if err = result.Error; err != nil {
// we assume this is due to a FK constraint -> file in use by playlist_entry
usageErr := &ErrFileInUse{}
if err = st.getFileUsage(id, &(usageErr.Playlists)); err != nil {
if err = st.getFileUsage(id, usageErr.Playlists); err != nil {
return
}
return usageErr
......
......@@ -77,7 +77,7 @@ func (p *Playlist) AfterFind() error {
return nil
}
func (st *Store) ListPlaylists(show string, offset, limit int) (playlists Playlists, err error) {
func (st *Store) ListPlaylists(show string, offset, limit int) (playlists []Playlist, err error) {
err = st.db.Where("show_name = ?", show).Preload("Entries", func(db *gorm.DB) *gorm.DB {
return db.Order("playlist_entries.line_num asc")
}).Preload("Entries.File").Order("id").Offset(offset).Limit(limit).Find(&playlists).Error
......@@ -154,7 +154,7 @@ func (st *Store) DeletePlaylist(show string, id uint64) (err error) {
return
}
func (st *Store) ListPlaylistsAllShows(offset, limit int) (playlists Playlists, err error) {
func (st *Store) ListPlaylistsAllShows(offset, limit int) (playlists []Playlist, err error) {
err = st.db.Preload("Entries", func(db *gorm.DB) *gorm.DB {
return db.Order("playlist_entries.line_num asc")
}).Preload("Entries.File").Order("id").Offset(offset).Limit(limit).Find(&playlists).Error
......
......@@ -64,7 +64,7 @@ func (st *Store) CreateShow(name string) (show *Show, err error) {
func (st *Store) cloneFiles(tx *gorm.DB, name, from string) (fileIDMapping map[uint64]uint64, err error) {
fileIDMapping = make(map[uint64]uint64)
var files Files
var files []File
if err = tx.Where("show_name = ?", from).Find(&files).Error; err != nil {
return
}
......@@ -98,7 +98,7 @@ func (st *Store) cloneFiles(tx *gorm.DB, name, from string) (fileIDMapping map[u
}
func (st *Store) clonePlaylists(tx *gorm.DB, name, from string, fileIDMapping map[uint64]uint64) (err error) {
var playlists Playlists
var playlists []Playlist
err = tx.Where("show_name = ?", from).Preload("Entries", func(db *gorm.DB) *gorm.DB {
return db.Order("playlist_entries.line_num asc")
}).Preload("Entries.File").Find(&playlists).Error
......@@ -216,7 +216,7 @@ func (st *Store) DeleteShow(name string) (err error) {
return
}
func (st *Store) ListShows() (shows Shows, err error) {
func (st *Store) ListShows() (shows []Show, err error) {
err = st.db.Find(&shows).Error
return
}
......@@ -51,7 +51,7 @@ var (
)
type ErrFileInUse struct {
Playlists Playlists `json:"playlists"`
Playlists []Playlist `json:"playlists"`
}
func (e *ErrFileInUse) Error() string {
......@@ -90,8 +90,6 @@ func (g Show) String() string {
return g.Name
}
type Shows []Show
//******* Files
type ImportState int
......@@ -185,8 +183,6 @@ type File struct {
Duration time.Duration `json:"duration"`
}
type Files []File
//******* ImportLogs
type ImportLog struct {
......@@ -222,6 +218,4 @@ type Playlist struct {
Entries []PlaylistEntry `json:"entries,omitempty"`
}
type Playlists []Playlist
//****************
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment