Skip to content

Commit

Permalink
Add Genre filters to UI
Browse files Browse the repository at this point in the history
  • Loading branch information
deluan committed Jul 20, 2021
1 parent c56c7c8 commit 20b7e5c
Show file tree
Hide file tree
Showing 13 changed files with 108 additions and 32 deletions.
6 changes: 3 additions & 3 deletions model/genres.go → model/genre.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package model

type Genre struct {
ID string `json:"id" orm:"column(id)"`
Name string
SongCount int `json:"-"`
AlbumCount int `json:"-"`
Name string `json:"name"`
SongCount int `json:"-"`
AlbumCount int `json:"-"`
}

type Genres []Genre
Expand Down
7 changes: 6 additions & 1 deletion persistence/album_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,12 @@ func artistFilter(field string, value interface{}) Sqlizer {
}

func (r *albumRepository) CountAll(options ...model.QueryOptions) (int64, error) {
return r.count(r.selectAlbum(), options...)
sql := r.selectAlbum()
sql = sql.LeftJoin("album_genres ag on album.id = ag.album_id").
LeftJoin("genre on ag.genre_id = genre.id").
GroupBy("album.id")

return r.count(sql, options...)
}

func (r *albumRepository) Exists(id string) (bool, error) {
Expand Down
6 changes: 5 additions & 1 deletion persistence/artist_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ func (r *artistRepository) selectArtist(options ...model.QueryOptions) SelectBui
}

func (r *artistRepository) CountAll(options ...model.QueryOptions) (int64, error) {
return r.count(r.newSelectWithAnnotation("artist.id"), options...)
sql := r.newSelectWithAnnotation("artist.id")
sql = sql.LeftJoin("artist_genres ag on artist.id = ag.artist_id").
LeftJoin("genre on ag.genre_id = genre.id").
GroupBy("artist.id")
return r.count(sql, options...)
}

func (r *artistRepository) Exists(id string) (bool, error) {
Expand Down
3 changes: 3 additions & 0 deletions persistence/genre_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ func NewGenreRepository(ctx context.Context, o orm.Ormer) model.GenreRepository
r.ctx = ctx
r.ormer = o
r.tableName = "genre"
r.filterMappings = map[string]filterFunc{
"name": containsFilter,
}
return r
}

Expand Down
6 changes: 5 additions & 1 deletion persistence/mediafile_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ func NewMediaFileRepository(ctx context.Context, o orm.Ormer) *mediaFileReposito
}

func (r *mediaFileRepository) CountAll(options ...model.QueryOptions) (int64, error) {
return r.count(r.newSelectWithAnnotation("media_file.id"), options...)
sql := r.newSelectWithAnnotation("media_file.id")
sql = sql.LeftJoin("media_file_genres mfg on media_file.id = mfg.media_file_id").
LeftJoin("genre on mfg.genre_id = genre.id").
GroupBy("media_file.id")
return r.count(sql, options...)
}

func (r *mediaFileRepository) Exists(id string) (bool, error) {
Expand Down
2 changes: 2 additions & 0 deletions persistence/persistence.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ func (s *SQLStore) Resource(ctx context.Context, m interface{}) model.ResourceRe
return s.Album(ctx).(model.ResourceRepository)
case model.MediaFile:
return s.MediaFile(ctx).(model.ResourceRepository)
case model.Genre:
return s.Genre(ctx).(model.ResourceRepository)
case model.Playlist:
return s.Playlist(ctx).(model.ResourceRepository)
case model.Share:
Expand Down
1 change: 1 addition & 0 deletions server/nativeapi/native_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func (n *Router) routes() http.Handler {
n.R(r, "/song", model.MediaFile{}, true)
n.R(r, "/album", model.Album{}, true)
n.R(r, "/artist", model.Artist{}, true)
n.R(r, "/genre", model.Genre{}, true)
n.R(r, "/player", model.Player{}, true)
n.R(r, "/playlist", model.Playlist{}, true)
n.R(r, "/transcoding", model.Transcoding{}, conf.Server.EnableTranscodingConfig)
Expand Down
1 change: 1 addition & 0 deletions ui/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ const Admin = (props) => {
<Resource name="transcoding" />
),
<Resource name="translation" />,
<Resource name="genre" />,
<Resource name="playlistTrack" />,
<Resource name="keepalive" />,
<Player />,
Expand Down
9 changes: 9 additions & 0 deletions ui/src/album/AlbumList.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@ const AlbumFilter = (props) => {
>
<AutocompleteInput emptyText="-- None --" />
</ReferenceInput>
<ReferenceInput
label={translate('resources.album.fields.genre')}
source="genre_id"
reference="genre"
sort={{ field: 'name', order: 'ASC' }}
filterToQuery={(searchText) => ({ name: [searchText] })}
>
<AutocompleteInput emptyText="-- None --" />
</ReferenceInput>
<NullableBooleanInput source="compilation" />
<NumberInput source="year" />
{config.enableFavourites && (
Expand Down
39 changes: 27 additions & 12 deletions ui/src/artist/ArtistList.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import React, { useMemo } from 'react'
import { useHistory } from 'react-router-dom'
import {
AutocompleteInput,
Datagrid,
Filter,
NumberField,
ReferenceInput,
SearchInput,
TextField,
useTranslate,
} from 'react-admin'
import { useMediaQuery, withWidth } from '@material-ui/core'
import FavoriteIcon from '@material-ui/icons/Favorite'
Expand Down Expand Up @@ -49,18 +52,30 @@ const useStyles = makeStyles({
},
})

const ArtistFilter = (props) => (
<Filter {...props} variant={'outlined'}>
<SearchInput source="name" alwaysOn />
{config.enableFavourites && (
<QuickFilter
source="starred"
label={<FavoriteIcon fontSize={'small'} />}
defaultValue={true}
/>
)}
</Filter>
)
const ArtistFilter = (props) => {
const translate = useTranslate()
return (
<Filter {...props} variant={'outlined'}>
<SearchInput source="name" alwaysOn />
<ReferenceInput
label={translate('resources.artist.fields.genre')}
source="genre_id"
reference="genre"
sort={{ field: 'name', order: 'ASC' }}
filterToQuery={(searchText) => ({ name: [searchText] })}
>
<AutocompleteInput emptyText="-- None --" />
</ReferenceInput>
{config.enableFavourites && (
<QuickFilter
source="starred"
label={<FavoriteIcon fontSize={'small'} />}
defaultValue={true}
/>
)}
</Filter>
)
}

const ArtistListView = ({ hasShow, hasEdit, hasList, width, ...rest }) => {
const classes = useStyles()
Expand Down
20 changes: 18 additions & 2 deletions ui/src/artist/ArtistListActions.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,29 @@
import React from 'react'
import React, { cloneElement } from 'react'
import { sanitizeListRestProps, TopToolbar } from 'react-admin'
import { useMediaQuery } from '@material-ui/core'
import { ToggleFieldsMenu } from '../common'

const ArtistListActions = ({ className, ...rest }) => {
const ArtistListActions = ({
className,
filters,
resource,
showFilter,
displayedFilters,
filterValues,
...rest
}) => {
const isNotSmall = useMediaQuery((theme) => theme.breakpoints.up('sm'))

return (
<TopToolbar className={className} {...sanitizeListRestProps(rest)}>
{filters &&
cloneElement(filters, {
resource,
showFilter,
displayedFilters,
filterValues,
context: 'button',
})}
{isNotSmall && <ToggleFieldsMenu resource="artist" />}
</TopToolbar>
)
Expand Down
1 change: 1 addition & 0 deletions ui/src/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
"albumCount": "Album Count",
"songCount": "Song Count",
"playCount": "Plays",
"genre": "Genre",
"rating": "Rating"
}
},
Expand Down
39 changes: 27 additions & 12 deletions ui/src/song/SongList.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import React from 'react'
import {
AutocompleteInput,
Filter,
FunctionField,
NumberField,
ReferenceInput,
SearchInput,
TextField,
useTranslate,
} from 'react-admin'
import { useMediaQuery } from '@material-ui/core'
import FavoriteIcon from '@material-ui/icons/Favorite'
Expand Down Expand Up @@ -55,18 +58,30 @@ const useStyles = makeStyles({
},
})

const SongFilter = (props) => (
<Filter {...props} variant={'outlined'}>
<SearchInput source="title" alwaysOn />
{config.enableFavourites && (
<QuickFilter
source="starred"
label={<FavoriteIcon fontSize={'small'} />}
defaultValue={true}
/>
)}
</Filter>
)
const SongFilter = (props) => {
const translate = useTranslate()
return (
<Filter {...props} variant={'outlined'}>
<SearchInput source="title" alwaysOn />
<ReferenceInput
label={translate('resources.song.fields.genre')}
source="genre_id"
reference="genre"
sort={{ field: 'name', order: 'ASC' }}
filterToQuery={(searchText) => ({ name: [searchText] })}
>
<AutocompleteInput emptyText="-- None --" />
</ReferenceInput>
{config.enableFavourites && (
<QuickFilter
source="starred"
label={<FavoriteIcon fontSize={'small'} />}
defaultValue={true}
/>
)}
</Filter>
)
}

const SongList = (props) => {
const classes = useStyles()
Expand Down

0 comments on commit 20b7e5c

Please sign in to comment.