Add server map route

This commit is contained in:
jeffvli 2022-11-15 13:31:11 -08:00
parent 8d5a05c329
commit 758e9d4d2c
8 changed files with 58 additions and 8 deletions

View File

@ -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<typeof validation.servers.detail>,
res: Response
@ -351,6 +357,7 @@ export const serversController = {
fullScanServer,
getServerDetail,
getServerList,
getServerListMap,
quickScanServer,
refreshServer,
updateServer,

View File

@ -21,6 +21,8 @@ router
controller.servers.createServer
);
router.route('/map').get(controller.servers.getServerListMap);
router
.route('/:serverId')
.get(

View File

@ -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<string, { name: string; type: string }>);
};
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,

View File

@ -18,6 +18,7 @@ export const queryKeys = {
},
servers: {
list: (params?: any) => ['servers', 'list', params] as const,
map: () => ['servers', 'map'] as const,
root: ['servers'],
},
tasks: {

View File

@ -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<ServerMap>;
const getServerMap = async (signal?: AbortSignal) => {
const { data } = await ax.get<ServerMapResponse>('/servers/map', {
signal,
});
return data;
};
export type ServerListResponse = BaseResponse<Server[]>;
const getServerList = async (
@ -238,6 +248,7 @@ export const serversApi = {
enableUrl,
fullScan,
getServerList,
getServerMap,
quickScan,
updateServer,
updateServerPermission,

View File

@ -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',

View File

@ -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());
},
});
};

View File

@ -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<ServerMapResponse>) => {
return useQuery<ServerMapResponse>({
cacheTime: Infinity,
queryFn: ({ signal }) => api.servers.getServerMap(signal),
queryKey: queryKeys.servers.map(),
staleTime: Infinity,
...options,
});
};