rewrite get all endpoint with stores

This commit is contained in:
cwilvx
2024-07-15 00:50:18 +03:00
parent 58c90d95b1
commit 88a72763df
3 changed files with 26 additions and 9 deletions
+9 -8
View File
@@ -1,13 +1,9 @@
from flask import Blueprint
from flask_openapi3 import Tag
from flask_openapi3 import APIBlueprint
from pydantic import BaseModel, Field
from datetime import datetime
from app.api.apischemas import GenericLimitSchema
from app.db.libdata import ArtistTable
from app.db.libdata import AlbumTable
from app.store.albums import AlbumStore
from app.store.artists import ArtistStore
@@ -70,9 +66,9 @@ def get_all_items(path: GetAllItemsPath, query: GetAllItemsQuery):
is_artists = path.itemtype == "artists"
if is_albums:
items = AlbumTable.get_all()
items = AlbumStore.get_flat_list()
elif is_artists:
items = ArtistTable.get_all()
items = ArtistStore.get_flat_list()
total = len(items)
@@ -95,12 +91,17 @@ def get_all_items(path: GetAllItemsPath, query: GetAllItemsQuery):
sort_is_artist_albumcount = is_artists and sort == "albumcount"
lambda_sort = lambda x: getattr(x, sort)
lambda_sort_casefold = lambda x: getattr(x, sort).casefold()
if sort_is_artist:
lambda_sort = lambda x: getattr(x, sort)[0]["name"]
lambda_sort = lambda x: getattr(x, sort)[0]["name"].casefold()
try:
sorted_items = sorted(items, key=lambda_sort_casefold, reverse=reverse)
except AttributeError:
sorted_items = sorted(items, key=lambda_sort, reverse=reverse)
items = sorted_items[start : start + limit]
items = sorted_items[start : start + limit]
album_list = []
for item in items:
+8
View File
@@ -8,6 +8,7 @@ from app.db.sqlite.albumcolors import SQLiteAlbumMethods as aldb
from app.lib.tagger import create_albums
from app.models import Album, Track
from app.store.artists import ArtistStore
from app.utils import flatten
from app.utils.customlist import CustomList
from app.utils.remove_duplicates import remove_duplicates
@@ -76,6 +77,13 @@ class AlbumStore:
print("Done!")
@classmethod
def get_flat_list(cls):
"""
Returns a flat list of all albums.
"""
return [a.album for a in cls.albummap.values()]
@classmethod
def add_album(cls, album: Album):
"""
+8
View File
@@ -4,6 +4,7 @@ from typing import Iterable
from app.db.sqlite.artistcolors import SQLiteArtistMethods as ardb
from app.lib.tagger import create_artists
from app.models import Artist
from app.utils import flatten
from app.utils.bisection import use_bisection
from app.utils.customlist import CustomList
from app.utils.progressbar import tqdm
@@ -57,6 +58,13 @@ class ArtistStore:
# cls.map_artist_color(artist)
@classmethod
def get_flat_list(cls):
"""
Returns a flat list of all artists.
"""
return [a.artist for a in cls.artistmap.values()]
@classmethod
def map_artist_color(cls, artist_tuple: tuple):
"""