-
Notifications
You must be signed in to change notification settings - Fork 0
/
saveModels.ts
100 lines (85 loc) · 2.8 KB
/
saveModels.ts
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/**
* Express router module for handling URL shortening and retrieval of saved models.
* @module saveModels
*/
import dotenv from 'dotenv';
import { Request, Response, Router } from 'express';
import { body, param, validationResult } from 'express-validator';
import { db } from './index';
import shortid from 'shortid';
dotenv.config();
const router = Router();
/**
* POST /shorten-url
* Creates a shortened URL for the provided data.
*
* @route POST /shorten-url
* @param {Object} req.body.data - The data to be associated with the shortened URL.
* @returns {Object} JSON object containing the shortened URL.
* @throws {400} If the request body is invalid.
* @throws {500} If there's an internal server error.
*/
router.post(
'/shorten-url',
[body('data').isObject().notEmpty()],
async (req: Request, res: Response) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(400).json({ errors: errors.array() });
}
const { data } = req.body;
try {
const existingUrl = await db('public.url').select('short_url').where({ data }).first();
if (existingUrl) {
return res.status(200).json({ shortUrl: existingUrl.short_url });
}
const shortUrlId = shortid.generate();
const shortUrl = `${process.env.FE_APP_URL}/${shortUrlId}`;
await db('public.url').insert({
data,
short_url: shortUrl,
});
res.status(200).json({ shortUrl });
} catch (error) {
console.error('Error inserting URL:', error);
res.status(500).json({ error: 'Internal server error' });
}
}
);
/**
* GET /saved-model/:modelId
* Retrieves the data associated with a given model ID.
*
* @route GET /saved-model/:modelId
* @param {string} req.params.modelId - The ID of the model to retrieve.
* @returns {Object} JSON object containing the saved model data.
* @throws {400} If the model ID is invalid.
* @throws {404} If the model is not found.
* @throws {500} If there's an internal server error.
*/
router.get(
'/saved-model/:modelId',
[param('modelId').isLength({ min: 1 }).withMessage('Invalid short URL ID')],
async (req: Request, res: Response) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(400).json({ errors: errors.array() });
}
const { modelId } = req.params;
try {
const result = await db('public.url')
.select('data')
.where('short_url', `${process.env.FE_APP_URL}/${modelId}`)
.first();
if (result) {
res.status(200).json(result.data);
} else {
res.status(404).json({ error: 'URL not found' });
}
} catch (error) {
console.error('Error retrieving URL:', error);
res.status(500).json({ error: 'Internal server error' });
}
}
);
export default router;