mirror of
https://github.com/jeffvli/feishin.git
synced 2026-06-11 06:42:38 +02:00
Redo server functionality
This commit is contained in:
@@ -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 = {
|
||||
|
||||
Reference in New Issue
Block a user