diff --git a/server/controllers/servers.controller.ts b/server/controllers/servers.controller.ts index 23e7cc17..cfd66840 100644 --- a/server/controllers/servers.controller.ts +++ b/server/controllers/servers.controller.ts @@ -1,10 +1,16 @@ import { ServerType } from '@prisma/client'; -import { Response } from 'express'; +import { Request, Response } from 'express'; import { ApiError, ApiSuccess, getSuccessResponse } from '@/utils'; import { toApiModel } from '@helpers/api-model'; import { service } from '@services/index'; import { TypedRequest, validation } from '@validations/index'; +const getServerListMap = async (req: Request, res: Response) => { + const data = await service.servers.getServerListMap(); + const success = ApiSuccess.ok({ data }); + return res.status(success.statusCode).json(getSuccessResponse(success)); +}; + const getServerDetail = async ( req: TypedRequest, res: Response @@ -351,6 +357,7 @@ export const serversController = { fullScanServer, getServerDetail, getServerList, + getServerListMap, quickScanServer, refreshServer, updateServer, diff --git a/server/routes/servers.route.ts b/server/routes/servers.route.ts index 1696704f..0bf76b5c 100644 --- a/server/routes/servers.route.ts +++ b/server/routes/servers.route.ts @@ -21,6 +21,8 @@ router controller.servers.createServer ); +router.route('/map').get(controller.servers.getServerListMap); + router .route('/:serverId') .get( diff --git a/server/services/servers.service.ts b/server/services/servers.service.ts index 44f1b37b..8aa10be4 100644 --- a/server/services/servers.service.ts +++ b/server/services/servers.service.ts @@ -76,6 +76,18 @@ const remoteServerLogin = async (options: { throw ApiError.badRequest('Server type invalid.'); }; +const getServerListMap = async () => { + const servers = await prisma.server.findMany({}); + + return servers.reduce((acc, server) => { + acc[server.id] = { + name: server.name, + type: server.type, + }; + return acc; + }, {} as Record); +}; + const findById = async (user: AuthUser, options: { id: string }) => { const { id } = options; @@ -626,6 +638,7 @@ export const serversService = { findServerUrlById, findUrlById, fullScan, + getServerListMap, refresh, remoteServerLogin, update, diff --git a/src/renderer/api/query-keys.ts b/src/renderer/api/query-keys.ts index 965ff38f..8db218b9 100644 --- a/src/renderer/api/query-keys.ts +++ b/src/renderer/api/query-keys.ts @@ -18,6 +18,7 @@ export const queryKeys = { }, servers: { list: (params?: any) => ['servers', 'list', params] as const, + map: () => ['servers', 'map'] as const, root: ['servers'], }, tasks: { diff --git a/src/renderer/api/servers.api.ts b/src/renderer/api/servers.api.ts index 71ddaff1..27bfa5a8 100644 --- a/src/renderer/api/servers.api.ts +++ b/src/renderer/api/servers.api.ts @@ -2,12 +2,22 @@ import { BaseResponse, NullResponse, Server, + ServerMap, ServerPermissionType, ServerType, ServerUrl, } from '@/renderer/api/types'; import { ax } from '@/renderer/lib/axios'; +export type ServerMapResponse = BaseResponse; + +const getServerMap = async (signal?: AbortSignal) => { + const { data } = await ax.get('/servers/map', { + signal, + }); + return data; +}; + export type ServerListResponse = BaseResponse; const getServerList = async ( @@ -238,6 +248,7 @@ export const serversApi = { enableUrl, fullScan, getServerList, + getServerMap, quickScan, updateServer, updateServerPermission, diff --git a/src/renderer/api/types.ts b/src/renderer/api/types.ts index 56be0a87..9413a309 100644 --- a/src/renderer/api/types.ts +++ b/src/renderer/api/types.ts @@ -4,6 +4,13 @@ export enum ServerType { SUBSONIC = 'SUBSONIC', } +export type ServerMap = { + [key: string]: { + name: string; + type: ServerType; + }; +}; + export enum ServerPermissionType { ADMIN = 'ADMIN', EDITOR = 'EDITOR', diff --git a/src/renderer/features/servers/mutations/use-create-server.ts b/src/renderer/features/servers/mutations/use-create-server.ts index b7dd4fb5..4f86c726 100644 --- a/src/renderer/features/servers/mutations/use-create-server.ts +++ b/src/renderer/features/servers/mutations/use-create-server.ts @@ -24,13 +24,7 @@ export const useCreateServer = () => { }, onSettled: () => { queryClient.invalidateQueries(queryKeys.servers.list()); - }, - onSuccess: (data) => { - toast.show({ - message: `${data.data.name} was added successfully`, - title: 'Server added', - type: 'success', - }); + queryClient.invalidateQueries(queryKeys.servers.map()); }, }); }; diff --git a/src/renderer/features/servers/queries/get-server-map.ts b/src/renderer/features/servers/queries/get-server-map.ts new file mode 100644 index 00000000..30b9a363 --- /dev/null +++ b/src/renderer/features/servers/queries/get-server-map.ts @@ -0,0 +1,15 @@ +import { useQuery } from '@tanstack/react-query'; +import { api } from '@/renderer/api'; +import { queryKeys } from '@/renderer/api/query-keys'; +import { ServerMapResponse } from '@/renderer/api/servers.api'; +import { QueryOptions } from '@/renderer/lib/react-query'; + +export const useServerMap = (options?: QueryOptions) => { + return useQuery({ + cacheTime: Infinity, + queryFn: ({ signal }) => api.servers.getServerMap(signal), + queryKey: queryKeys.servers.map(), + staleTime: Infinity, + ...options, + }); +};