Redo server functionality

This commit is contained in:
jeffvli
2022-10-24 21:41:47 -07:00
parent db8a7d6a63
commit 4a3ce02805
40 changed files with 1986 additions and 952 deletions
+23 -43
View File
@@ -1,14 +1,10 @@
import { User } from '@prisma/client';
import { Request } from 'express';
import { prisma } from '../lib';
import { OffsetPagination, User } from '../types/types';
import {
ApiError,
ApiSuccess,
folderPermissions,
splitNumberString,
} from '../utils';
import { OffsetPagination } from '../types/types';
import { ApiError, folderPermissions } from '../utils';
const findById = async (options: { id: number; user: User }) => {
const findById = async (options: { id: string; user: User }) => {
const { id, user } = options;
const artist = await prisma.artist.findUnique({
@@ -16,19 +12,16 @@ const findById = async (options: { id: number; user: User }) => {
where: { id },
});
if (!artist) {
throw ApiError.notFound('');
}
if (!artist) throw ApiError.notFound('');
const serverFolderIds = artist.serverFolders.map(
(serverFolder) => serverFolder.id
);
if (!(await folderPermissions(serverFolderIds, user))) {
if (!(await folderPermissions(serverFolderIds, user)))
throw ApiError.forbidden('');
}
return ApiSuccess.ok({ data: artist });
return artist;
};
const findMany = async (
@@ -36,41 +29,28 @@ const findMany = async (
options: { serverFolderIds: string; user: User } & OffsetPagination
) => {
const { user, skip, take, serverFolderIds: rServerFolderIds } = options;
const serverFolderIds = splitNumberString(rServerFolderIds);
const serverFolderIds = rServerFolderIds.split(',');
if (!(await folderPermissions(serverFolderIds!, user))) {
if (!(await folderPermissions(serverFolderIds!, user)))
throw ApiError.forbidden('');
}
const serverFoldersFilter = serverFolderIds!.map((serverFolderId: number) => {
return {
serverFolders: {
some: {
id: { equals: Number(serverFolderId) },
},
},
};
});
const serverFoldersFilter = serverFolderIds!.map((serverFolderId) => ({
serverFolders: { some: { id: { equals: serverFolderId } } },
}));
const totalEntries = await prisma.artist.count({
where: { OR: serverFoldersFilter },
});
const artists = await prisma.artist.findMany({
include: { genres: true },
skip,
take,
where: { OR: serverFoldersFilter },
});
return ApiSuccess.ok({
data: artists,
paginationItems: {
const [totalEntries, artists] = await prisma.$transaction([
prisma.artist.count({
where: { OR: serverFoldersFilter },
}),
prisma.artist.findMany({
include: { genres: true },
skip,
take,
totalEntries,
url: req.originalUrl,
},
});
where: { OR: serverFoldersFilter },
}),
]);
return { data: artists, totalEntries };
};
export const artistsService = {