File size: 1,404 Bytes
6f1c297
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { FastifyInstance } from 'fastify';
import * as solutionCacheService from '../services/solutionCache.service.js';

interface BatchGetBody {
	nameList: string[];
}

interface SetBody {
	name: string;
	value: any;
}

interface DeleteBody {
	name: string;
}

export default async function solutionCacheRoutes(fastify: FastifyInstance) {
	// Batch get solutions by hash keys
	fastify.post<{ Body: BatchGetBody }>('/solutions/batchGet', async (request, reply) => {
		const { nameList } = request.body || {};
		if (!Array.isArray(nameList)) {
			reply.code(400);
			return { code: 400, message: 'nameList must be an array' };
		}

		const data = await solutionCacheService.batchGet(nameList);
		return { code: 0, data };
	});

	// Set a solution
	fastify.post<{ Body: SetBody }>('/solutions/set', async (request, reply) => {
		const { name, value } = request.body || {};
		if (!name || typeof name !== 'string') {
			reply.code(400);
			return { code: 400, message: 'name is required' };
		}

		await solutionCacheService.set(name, value);
		return { code: 0, data: { success: true } };
	});

	// Delete a solution
	fastify.post<{ Body: DeleteBody }>('/solutions/delete', async (request) => {
		const { name } = request.body || {};
		if (!name) {
			return { code: 400, message: 'name is required' };
		}

		await solutionCacheService.remove(name);
		return { code: 0, data: { success: true } };
	});
}