mirror of
https://github.com/Drop-OSS/drop.git
synced 2026-01-31 15:37:09 +01:00
Update index.post.ts to implement saving collections functionality Update index.get.ts to verify if collection exists and if user can access it Update index.delete.ts to ask questions and not be so nonchalant Update entry.post.ts Update entry.delete.ts to do it better Update index.vue to add functionality to the add to library button + fidgit with image Update index.vue to also add add to library functionality, but no fidget :( Update entry.post.ts to infact not remove it Update index.ts Update index.vue to manage collections from store page Update index.ts to restrut for ahhhh Update index.vue too add collection control to carosel Update index.vue fix minor issue Update index.vue to fix dropdown modal bug Create library.vue for page layout Create index.vue for library game details pane Create index.vue for viewing collections pane Create DeleteCollectionModal.vue component Create CreateCollectionModal.vue component Update AddLibraryButton.vue with dropdown :D Update index.vue to use new components Update index.vue for more components :O Update entry.post.ts to not not return success, it'll figure it out Update entry.delete.ts to not return...
126 lines
3.0 KiB
TypeScript
126 lines
3.0 KiB
TypeScript
/*
|
|
Handles managing collections
|
|
*/
|
|
|
|
import prisma from "../db/database";
|
|
|
|
class UserLibraryManager {
|
|
// Caches the user's core library
|
|
private userCoreLibraryCache: { [key: string]: string } = {};
|
|
|
|
constructor() {}
|
|
|
|
private async fetchUserLibrary(userId: string) {
|
|
if (this.userCoreLibraryCache[userId])
|
|
return this.userCoreLibraryCache[userId];
|
|
|
|
let collection = await prisma.collection.findFirst({
|
|
where: {
|
|
userId,
|
|
isDefault: true,
|
|
},
|
|
});
|
|
|
|
if (!collection)
|
|
collection = await prisma.collection.create({
|
|
data: {
|
|
name: "Library",
|
|
userId,
|
|
isDefault: true,
|
|
},
|
|
});
|
|
|
|
this.userCoreLibraryCache[userId] = collection.id;
|
|
|
|
return collection.id;
|
|
}
|
|
|
|
async libraryAdd(gameId: string, userId: string) {
|
|
const userLibraryId = await this.fetchUserLibrary(userId);
|
|
await this.collectionAdd(gameId, userLibraryId);
|
|
}
|
|
|
|
async libraryRemove(gameId: string, userId: string) {
|
|
const userLibraryId = await this.fetchUserLibrary(userId);
|
|
await this.collectionRemove(gameId, userLibraryId);
|
|
}
|
|
|
|
async fetchLibrary(userId: string) {
|
|
const userLibraryId = await this.fetchUserLibrary(userId);
|
|
const userLibrary = await prisma.collection.findUnique({
|
|
where: { id: userLibraryId },
|
|
include: { entries: { include: { game: true } } },
|
|
});
|
|
if (!userLibrary) throw new Error("Failed to load user library");
|
|
return userLibrary;
|
|
}
|
|
|
|
async fetchCollection(collectionId: string) {
|
|
return await prisma.collection.findUnique({
|
|
where: { id: collectionId },
|
|
include: { entries: { include: { game: true } } },
|
|
});
|
|
}
|
|
|
|
async fetchCollections(userId: string) {
|
|
await this.fetchUserLibrary(userId); // Ensures user library exists, doesn't have much performance impact due to caching
|
|
return await prisma.collection.findMany({
|
|
where: { userId },
|
|
include: {
|
|
entries: true,
|
|
_count: { select: { entries: true } }
|
|
}
|
|
});
|
|
}
|
|
|
|
async collectionAdd(gameId: string, collectionId: string) {
|
|
await prisma.collectionEntry.upsert({
|
|
where: {
|
|
collectionId_gameId: {
|
|
collectionId,
|
|
gameId,
|
|
},
|
|
},
|
|
create: {
|
|
collectionId,
|
|
gameId,
|
|
},
|
|
update: {},
|
|
});
|
|
}
|
|
|
|
async collectionRemove(gameId: string, collectionId: string) {
|
|
// Delete if exists
|
|
return (
|
|
(
|
|
await prisma.collectionEntry.deleteMany({
|
|
where: {
|
|
collectionId,
|
|
gameId,
|
|
},
|
|
})
|
|
).count > 0
|
|
);
|
|
}
|
|
|
|
async collectionCreate(name: string, userId: string) {
|
|
return await prisma.collection.create({
|
|
data: {
|
|
name,
|
|
userId: userId,
|
|
},
|
|
});
|
|
}
|
|
|
|
async deleteCollection(collectionId: string) {
|
|
await prisma.collection.delete({
|
|
where: {
|
|
id: collectionId,
|
|
},
|
|
});
|
|
}
|
|
}
|
|
|
|
export const userLibraryManager = new UserLibraryManager();
|
|
export default userLibraryManager;
|