Added schma to validate
This commit is contained in:
		@@ -31,26 +31,36 @@ fastify.get('/:id', async (req, res) => {
 | 
			
		||||
})
 | 
			
		||||
 | 
			
		||||
//Create new urls
 | 
			
		||||
fastify.post('/new', async (req, res) => {
 | 
			
		||||
const newUrlSchema = {
 | 
			
		||||
    body: {
 | 
			
		||||
        type: 'object',
 | 
			
		||||
        properties: {
 | 
			
		||||
            target: { type: 'string' },
 | 
			
		||||
            shortcode: { type: 'string' },
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
fastify.post('/new', { newUrlSchema }, async (req, res) => {
 | 
			
		||||
    const target = req.body?.target;
 | 
			
		||||
    let shortcode = req.body?.shortcode;
 | 
			
		||||
    if (!req.body?.target) {
 | 
			
		||||
    if (!target) {
 | 
			
		||||
        res.statusCode = 400;
 | 
			
		||||
        return "Missing target";
 | 
			
		||||
    }
 | 
			
		||||
    if (!shortcode) {
 | 
			
		||||
        shortcode = nanoid(12);
 | 
			
		||||
    }
 | 
			
		||||
    else{
 | 
			
		||||
    else {
 | 
			
		||||
        try {
 | 
			
		||||
            const exists = await db.get(shortcode);
 | 
			
		||||
            if(exists){
 | 
			
		||||
            if (exists) {
 | 
			
		||||
                res.statusCode = 400;
 | 
			
		||||
                return "Shortcode already exists, please choose another code";
 | 
			
		||||
            }
 | 
			
		||||
        } catch (error) {
 | 
			
		||||
            if(!error.notFound){
 | 
			
		||||
                res.statusCode=500;
 | 
			
		||||
            if (!error.notFound) {
 | 
			
		||||
                res.statusCode = 500;
 | 
			
		||||
                return "Internal Server Error."
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user