mirror of
https://github.com/jeffvli/feishin.git
synced 2026-05-10 04:30:25 +02:00
Add genres route
This commit is contained in:
@@ -0,0 +1,23 @@
|
|||||||
|
import { Response } from 'express';
|
||||||
|
import { toApiModel } from '@helpers/api-model';
|
||||||
|
import { service } from '@services/index';
|
||||||
|
import { ApiSuccess } from '@utils/api-success';
|
||||||
|
import { getSuccessResponse } from '@utils/get-success-response';
|
||||||
|
import { validation } from '@validations/index';
|
||||||
|
import { TypedRequest } from '@validations/shared.validation';
|
||||||
|
|
||||||
|
const getList = async (
|
||||||
|
req: TypedRequest<typeof validation.genres.list>,
|
||||||
|
res: Response
|
||||||
|
) => {
|
||||||
|
const { serverId } = req.params;
|
||||||
|
|
||||||
|
const data = await service.genres.findManyByServer({ serverId });
|
||||||
|
|
||||||
|
const success = ApiSuccess.ok({ data: toApiModel.genres(data) });
|
||||||
|
return res.status(success.statusCode).json(getSuccessResponse(success));
|
||||||
|
};
|
||||||
|
|
||||||
|
export const genresController = {
|
||||||
|
getList,
|
||||||
|
};
|
||||||
@@ -2,6 +2,7 @@ import { albumArtistsController } from '@controllers/album-artists.controller';
|
|||||||
import { albumsController } from '@controllers/albums.controller';
|
import { albumsController } from '@controllers/albums.controller';
|
||||||
import { artistsController } from '@controllers/artists.controller';
|
import { artistsController } from '@controllers/artists.controller';
|
||||||
import { authController } from '@controllers/auth.controller';
|
import { authController } from '@controllers/auth.controller';
|
||||||
|
import { genresController } from '@controllers/genres.controller';
|
||||||
import { serversController } from '@controllers/servers.controller';
|
import { serversController } from '@controllers/servers.controller';
|
||||||
import { songsController } from '@controllers/songs.controller';
|
import { songsController } from '@controllers/songs.controller';
|
||||||
import { tasksController } from '@controllers/tasks.controller';
|
import { tasksController } from '@controllers/tasks.controller';
|
||||||
@@ -12,6 +13,7 @@ export const controller = {
|
|||||||
albums: albumsController,
|
albums: albumsController,
|
||||||
artists: artistsController,
|
artists: artistsController,
|
||||||
auth: authController,
|
auth: authController,
|
||||||
|
genres: genresController,
|
||||||
servers: serversController,
|
servers: serversController,
|
||||||
songs: songsController,
|
songs: songsController,
|
||||||
tasks: tasksController,
|
tasks: tasksController,
|
||||||
|
|||||||
@@ -210,6 +210,28 @@ const relatedGenres = (items: Genre[]) => {
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const genres = (items: (Genre & { _count?: any })[]) => {
|
||||||
|
return (
|
||||||
|
items?.map((item) => {
|
||||||
|
const totalCount = Object.keys(item._count)
|
||||||
|
.map((key) => item._count[key])
|
||||||
|
.reduce((a, b) => a + b, 0);
|
||||||
|
|
||||||
|
return {
|
||||||
|
/* eslint-disable sort-keys-fix/sort-keys-fix */
|
||||||
|
id: item.id,
|
||||||
|
name: item.name,
|
||||||
|
songCount: item._count?.songs,
|
||||||
|
albumCount: item._count?.albums,
|
||||||
|
artistCount: item._count?.artists,
|
||||||
|
albumArtistCount: item._count?.albumArtists,
|
||||||
|
totalCount,
|
||||||
|
/* eslint-enable sort-keys-fix/sort-keys-fix */
|
||||||
|
};
|
||||||
|
}) || []
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
const relatedServerFolders = (items: ServerFolder[]) => {
|
const relatedServerFolders = (items: ServerFolder[]) => {
|
||||||
const serverFolders = items?.map((item) => {
|
const serverFolders = items?.map((item) => {
|
||||||
return {
|
return {
|
||||||
|
|||||||
@@ -0,0 +1,12 @@
|
|||||||
|
import express, { Router } from 'express';
|
||||||
|
import { controller } from '@controllers/index';
|
||||||
|
import { validation } from '@validations/index';
|
||||||
|
import { validateRequest } from '@validations/shared.validation';
|
||||||
|
|
||||||
|
export const router: Router = express.Router({ mergeParams: true });
|
||||||
|
|
||||||
|
router.get(
|
||||||
|
'/',
|
||||||
|
validateRequest(validation.genres.list),
|
||||||
|
controller.genres.getList
|
||||||
|
);
|
||||||
@@ -5,6 +5,7 @@ import { router as albumArtistsRouter } from './album-artists.route';
|
|||||||
import { router as albumsRouter } from './albums.route';
|
import { router as albumsRouter } from './albums.route';
|
||||||
import { router as artistsRouter } from './artists.route';
|
import { router as artistsRouter } from './artists.route';
|
||||||
import { router as authRouter } from './auth.route';
|
import { router as authRouter } from './auth.route';
|
||||||
|
import { router as genresRouter } from './genres.route';
|
||||||
import { router as serversRouter } from './servers.route';
|
import { router as serversRouter } from './servers.route';
|
||||||
import { router as songsRouter } from './songs.route';
|
import { router as songsRouter } from './songs.route';
|
||||||
import { router as tasksRouter } from './tasks.route';
|
import { router as tasksRouter } from './tasks.route';
|
||||||
@@ -44,4 +45,5 @@ routes.param('serverId', (req, _res, next, serverId) => {
|
|||||||
routes.use('/api/servers/:serverId/album-artists', albumArtistsRouter);
|
routes.use('/api/servers/:serverId/album-artists', albumArtistsRouter);
|
||||||
routes.use('/api/servers/:serverId/artists', artistsRouter);
|
routes.use('/api/servers/:serverId/artists', artistsRouter);
|
||||||
routes.use('/api/servers/:serverId/albums', albumsRouter);
|
routes.use('/api/servers/:serverId/albums', albumsRouter);
|
||||||
|
routes.use('/api/servers/:serverId/genres', genresRouter);
|
||||||
routes.use('/api/servers/:serverId/songs', songsRouter);
|
routes.use('/api/servers/:serverId/songs', songsRouter);
|
||||||
|
|||||||
@@ -0,0 +1,59 @@
|
|||||||
|
import { prisma } from '@lib/prisma';
|
||||||
|
import { ApiError } from '@utils/api-error';
|
||||||
|
|
||||||
|
const findManyByServer = async (options: { serverId: string }) => {
|
||||||
|
const { serverId } = options;
|
||||||
|
|
||||||
|
const genres = await prisma.genre.findMany({
|
||||||
|
include: {
|
||||||
|
_count: {
|
||||||
|
select: {
|
||||||
|
albumArtists: { where: { serverId } },
|
||||||
|
albums: { where: { serverId } },
|
||||||
|
artists: { where: { serverId } },
|
||||||
|
songs: { where: { serverId } },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
where: {
|
||||||
|
OR: [
|
||||||
|
{ albumArtists: { some: { serverId } } },
|
||||||
|
{ albums: { some: { serverId } } },
|
||||||
|
{ artists: { some: { serverId } } },
|
||||||
|
{ songs: { some: { serverId } } },
|
||||||
|
],
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!genres) {
|
||||||
|
throw ApiError.notFound('');
|
||||||
|
}
|
||||||
|
|
||||||
|
return genres;
|
||||||
|
};
|
||||||
|
|
||||||
|
const findMany = async () => {
|
||||||
|
const genres = await prisma.genre.findMany({
|
||||||
|
include: {
|
||||||
|
_count: {
|
||||||
|
select: {
|
||||||
|
albumArtists: true,
|
||||||
|
albums: true,
|
||||||
|
artists: true,
|
||||||
|
songs: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!genres) {
|
||||||
|
throw ApiError.notFound('');
|
||||||
|
}
|
||||||
|
|
||||||
|
return genres;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const genresService = {
|
||||||
|
findMany,
|
||||||
|
findManyByServer,
|
||||||
|
};
|
||||||
@@ -2,6 +2,7 @@ import { albumArtistsService } from './album-artists.service';
|
|||||||
import { albumsService } from './albums.service';
|
import { albumsService } from './albums.service';
|
||||||
import { artistsService } from './artists.service';
|
import { artistsService } from './artists.service';
|
||||||
import { authService } from './auth.service';
|
import { authService } from './auth.service';
|
||||||
|
import { genresService } from './genres.service';
|
||||||
import { serversService } from './servers.service';
|
import { serversService } from './servers.service';
|
||||||
import { usersService } from './users.service';
|
import { usersService } from './users.service';
|
||||||
|
|
||||||
@@ -10,6 +11,7 @@ export const service = {
|
|||||||
albums: albumsService,
|
albums: albumsService,
|
||||||
artists: artistsService,
|
artists: artistsService,
|
||||||
auth: authService,
|
auth: authService,
|
||||||
|
genres: genresService,
|
||||||
servers: serversService,
|
servers: serversService,
|
||||||
users: usersService,
|
users: usersService,
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -0,0 +1,12 @@
|
|||||||
|
import { z } from 'zod';
|
||||||
|
import { idValidation } from '@validations/shared.validation';
|
||||||
|
|
||||||
|
const list = {
|
||||||
|
body: z.object({}),
|
||||||
|
params: z.object({ ...idValidation('serverId') }),
|
||||||
|
query: z.object({}),
|
||||||
|
};
|
||||||
|
|
||||||
|
export const genresValidation = {
|
||||||
|
list,
|
||||||
|
};
|
||||||
@@ -2,6 +2,7 @@ import { albumArtistsValidation } from '@validations/album-artists.validation';
|
|||||||
import { albumsValidation } from '@validations/albums.validation';
|
import { albumsValidation } from '@validations/albums.validation';
|
||||||
import { artistsValidation } from '@validations/artists.validation';
|
import { artistsValidation } from '@validations/artists.validation';
|
||||||
import { authValidation } from '@validations/auth.validation';
|
import { authValidation } from '@validations/auth.validation';
|
||||||
|
import { genresValidation } from '@validations/genres.validation';
|
||||||
import { serversValidation } from '@validations/servers.validation';
|
import { serversValidation } from '@validations/servers.validation';
|
||||||
import { songsValidation } from '@validations/songs.validation';
|
import { songsValidation } from '@validations/songs.validation';
|
||||||
import { tasksValidation } from '@validations/tasks.validation';
|
import { tasksValidation } from '@validations/tasks.validation';
|
||||||
@@ -14,6 +15,7 @@ export const validation = {
|
|||||||
albums: albumsValidation,
|
albums: albumsValidation,
|
||||||
artists: artistsValidation,
|
artists: artistsValidation,
|
||||||
auth: authValidation,
|
auth: authValidation,
|
||||||
|
genres: genresValidation,
|
||||||
servers: serversValidation,
|
servers: serversValidation,
|
||||||
songs: songsValidation,
|
songs: songsValidation,
|
||||||
tasks: tasksValidation,
|
tasks: tasksValidation,
|
||||||
|
|||||||
Reference in New Issue
Block a user