Merge branch 'feature/fav_for_siblings' into feature/fav_collection

This commit is contained in:
zurdi
2024-06-28 14:24:10 +02:00
9 changed files with 290 additions and 47 deletions

View File

@@ -1,3 +1,5 @@
from datetime import datetime
from decorators.auth import protected_route
from endpoints.responses import MessageResponse
from endpoints.responses.platform import PlatformSchema
@@ -74,6 +76,8 @@ def get_supported_platforms(request: Request) -> list[PlatformSchema]:
"logo_path": "",
"roms": [],
"rom_count": 0,
"created_at": datetime.now(),
"updated_at": datetime.now(),
}
if platform["name"] in db_platforms_map:
@@ -96,7 +100,15 @@ def get_platform(request: Request, id: int) -> PlatformSchema:
PlatformSchema: Platform
"""
return db_platform_handler.get_platforms(id)
platform = db_platform_handler.get_platforms(id)
if platform:
return platform
else:
msg = f"Platform with {id} not found"
log.critical(msg)
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=msg
)
@protected_route(router.put, "/platforms/{id}", ["platforms.write"])

View File

@@ -0,0 +1,32 @@
from datetime import datetime
from models.base import BaseModel
from models.rom import Rom
from sqlalchemy import DateTime, String, Text, func, select
from sqlalchemy.orm import Mapped, column_property, mapped_column, relationship
class Collection(BaseModel):
__tablename__ = "collections"
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
name: Mapped[str | None] = mapped_column(String(length=400))
description: Mapped[str | None] = mapped_column(Text)
logo_path: Mapped[str | None] = mapped_column(String(length=1000), default="")
roms: Mapped[list["Rom"]] = relationship(back_populates="platform")
created_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True), server_default=func.now()
)
updated_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True), server_default=func.now(), onupdate=func.now()
)
# This runs a subquery to get the count of roms for the platform
rom_count = column_property(
select(func.count(Rom.id)).where(Rom.platform_id == id).scalar_subquery()
)
def __repr__(self) -> str:
return self.name

View File

@@ -1,12 +1,20 @@
<script setup lang="ts">
import storeNavigation from "@/stores/navigation";
import { storeToRefs } from "pinia";
import storeCollections from "@/stores/collections";
import { useDisplay } from "vuetify";
// Props
const navigationStore = storeNavigation();
const { smAndDown } = useDisplay();
const collectionsStore = storeCollections();
const { filteredCollections, searchText } = storeToRefs(collectionsStore);
const { activeCollectionsDrawer } = storeToRefs(navigationStore);
// Functions
function clear() {
searchText.value = "";
}
</script>
<template>
<v-navigation-drawer
@@ -16,28 +24,36 @@ const { activeCollectionsDrawer } = storeToRefs(navigationStore);
v-model="activeCollectionsDrawer"
class="bg-terciary"
>
<template #prepend>
<v-list>
<v-list-item>
<v-btn
variant="outlined"
color="romm-accent-1"
prepend-icon="mdi-plus"
block
>Add Collection</v-btn
>
</v-list-item>
<v-list-item>
<v-text-field
v-model="searchText"
prepend-inner-icon="mdi-filter-outline"
clearable
hide-details
@click:clear="clear"
@update:model-value=""
single-line
label="Search collection"
density="compact"
variant="outlined"
></v-text-field>
</v-list-item>
</v-list>
</template>
<v-list lines="two" rounded="0" class="pa-0">
<v-list-item
v-for="collection in [
{
name: 'Favourites',
description: 'My favourites collection',
rom_count: 10,
id: 0,
},
{
name: 'Nintendo',
description: 'Nintendo collection',
rom_count: 7,
id: 1,
},
{
name: 'Sony',
description: 'Sony collection',
rom_count: 20,
id: 2,
},
]"
v-for="collection in filteredCollections"
:to="{ name: 'collection', params: { collection: collection.id } }"
:value="collection"
>

View File

@@ -1,27 +1,52 @@
<script setup lang="ts">
import { useDisplay } from "vuetify";
import { storeToRefs } from "pinia";
import PlatformListItem from "@/components/common/Platform/ListItem.vue";
import storeNavigation from "@/stores/navigation";
import storePlatforms from "@/stores/platforms";
import PlatformListItem from "@/components/common/Platform/ListItem.vue";
import { storeToRefs } from "pinia";
import { useDisplay } from "vuetify";
// Props
const navigationStore = storeNavigation();
const { smAndDown } = useDisplay();
const platformsStore = storePlatforms();
const { filteredPlatforms, searchText } = storeToRefs(platformsStore);
const { activePlatformsDrawer } = storeToRefs(navigationStore);
// Functions
function clear() {
searchText.value = "";
}
</script>
<template>
<v-navigation-drawer
:location="smAndDown ? 'top' : 'left'"
mobile
@update:model-value="clear"
width="400"
v-model="activePlatformsDrawer"
class="bg-terciary"
>
<template #prepend>
<v-list>
<v-list-item>
<v-text-field
v-model="searchText"
prepend-inner-icon="mdi-filter-outline"
clearable
hide-details
@click:clear="clear"
@update:model-value=""
single-line
label="Search platform"
density="compact"
variant="outlined"
></v-text-field>
</v-list-item>
</v-list>
</template>
<v-list lines="two" rounded="0" class="pa-0">
<platform-list-item
v-for="platform in platformsStore.filledPlatforms"
v-for="platform in filteredPlatforms"
:key="platform.slug"
:platform="platform"
class="py-4"

View File

@@ -0,0 +1,148 @@
import { defineStore } from "pinia";
import type { CollectionSchema } from "@/__generated__";
import { uniqBy } from "lodash";
export type Collection = CollectionSchema;
export default defineStore("collections", {
state: () => {
return {
all: [
{
name: "Favourites",
description: "My favourites collection",
rom_count: 10,
id: 3,
},
{
name: "Nintendo",
description: "Nintendo collection",
rom_count: 7,
id: 1,
},
{
name: "Sony",
description: "Sony collection",
rom_count: 20,
id: 2,
},
{
name: "Sega",
description: "Sega collection",
rom_count: 0,
id: 2,
},
{
name: "Favourites",
description: "My favourites collection",
rom_count: 10,
id: 0,
},
{
name: "Nintendo",
description: "Nintendo collection",
rom_count: 7,
id: 1,
},
{
name: "Sony",
description: "Sony collection",
rom_count: 20,
id: 2,
},
{
name: "Sega",
description: "Sega collection",
rom_count: 0,
id: 2,
},
{
name: "Favourites",
description: "My favourites collection",
rom_count: 10,
id: 0,
},
{
name: "Nintendo",
description: "Nintendo collection",
rom_count: 7,
id: 1,
},
{
name: "Sony",
description: "Sony collection",
rom_count: 20,
id: 2,
},
{
name: "Sega",
description: "Sega collection",
rom_count: 0,
id: 2,
},
{
name: "Favourites",
description: "My favourites collection",
rom_count: 10,
id: 0,
},
{
name: "Nintendo",
description: "Nintendo collection",
rom_count: 7,
id: 1,
},
{
name: "Sony",
description: "Sony collection",
rom_count: 20,
id: 2,
},
{
name: "Sega",
description: "Sega collection",
rom_count: 0,
id: 2,
},
] as Collection[],
searchText: "" as string,
};
},
getters: {
totalGames: ({ all: value }) =>
value.reduce((count, p) => count + p.rom_count, 0),
filledCollections: ({ all }) => all.filter((p) => p.rom_count > 0),
filteredCollections: ({ all, searchText }) =>
all.filter(
(p) =>
p.rom_count > 0 &&
p.name.toLowerCase().includes(searchText.toLowerCase()),
),
},
actions: {
_reorder() {
this.all = this.all.sort((a, b) => {
return a.name.localeCompare(b.name);
});
this.all = uniqBy(this.all, "id");
},
set(collections: Collection[]) {
this.all = collections;
},
add(collection: Collection) {
this.all.push(collection);
// this._reorder();
},
exists(collection: Collection) {
return this.all.filter((p) => p.fs_slug == collection.fs_slug).length > 0;
},
remove(collection: Collection) {
this.all = this.all.filter((p) => {
return p.slug !== collection.slug;
});
},
get(collectionId: number) {
return this.all.find((p) => p.id === collectionId);
},
},
});

View File

@@ -7,38 +7,45 @@ export type Platform = PlatformSchema;
export default defineStore("platforms", {
state: () => {
return {
value: [] as Platform[],
all: [] as Platform[],
searchText: "" as string,
};
},
getters: {
totalGames: ({ value }) =>
totalGames: ({ all: value }) =>
value.reduce((count, p) => count + p.rom_count, 0),
filledPlatforms: ({ value }) => value.filter((p) => p.rom_count > 0),
filledPlatforms: ({ all }) => all.filter((p) => p.rom_count > 0),
filteredPlatforms: ({ all, searchText }) =>
all.filter(
(p) =>
p.rom_count > 0 &&
p.name.toLowerCase().includes(searchText.toLowerCase()),
),
},
actions: {
_reorder() {
this.value = this.value.sort((a, b) => {
this.all = this.all.sort((a, b) => {
return a.name.localeCompare(b.name);
});
this.value = uniqBy(this.value, "id");
this.all = uniqBy(this.all, "id");
},
set(platforms: Platform[]) {
this.value = platforms;
this.all = platforms;
},
add(platform: Platform) {
this.value.push(platform);
this.all.push(platform);
this._reorder();
},
exists(platform: Platform) {
return this.value.filter((p) => p.fs_slug == platform.fs_slug).length > 0;
return this.all.filter((p) => p.fs_slug == platform.fs_slug).length > 0;
},
remove(platform: Platform) {
this.value = this.value.filter((p) => {
this.all = this.all.filter((p) => {
return p.slug !== platform.slug;
});
},
get(platformId: number) {
return this.value.find((p) => p.id === platformId);
return this.all.find((p) => p.id === platformId);
},
},
});

View File

@@ -215,12 +215,13 @@ onMounted(async () => {
} else {
romsStore.setCurrentPlatform(routePlatform);
}
resetGallery();
await fetchRoms();
setFilters();
window.addEventListener("wheel", onScroll);
window.addEventListener("scroll", onScroll);
if (!noPlatformError.value) {
resetGallery();
await fetchRoms();
setFilters();
window.addEventListener("wheel", onScroll);
window.addEventListener("scroll", onScroll);
}
});
onBeforeRouteUpdate(async (to, from) => {

View File

@@ -215,12 +215,14 @@ onMounted(async () => {
} else {
romsStore.setCurrentPlatform(routePlatform);
}
resetGallery();
await fetchRoms();
setFilters();
window.addEventListener("wheel", onScroll);
window.addEventListener("scroll", onScroll);
if (!noPlatformError.value) {
resetGallery();
await fetchRoms();
setFilters();
window.addEventListener("wheel", onScroll);
window.addEventListener("scroll", onScroll);
}
});
onBeforeRouteUpdate(async (to, from) => {

View File

@@ -107,7 +107,7 @@ async function stopScan() {
v-model="platformsToScan"
label="Platforms"
item-title="name"
:items="platforms.value"
:items="platforms.all"
variant="outlined"
density="comfortable"
multiple