From f062482fc0706b37a12e9756d59e822dcc7480ed Mon Sep 17 00:00:00 2001 From: kj1352 Date: Tue, 19 Oct 2021 20:04:56 +0530 Subject: [PATCH] Words API Complete --- src/index.ts | 2 ++ src/library/route.ts | 0 src/user-profile/word-routes.ts | 64 +++++++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+) delete mode 100644 src/library/route.ts create mode 100644 src/user-profile/word-routes.ts diff --git a/src/index.ts b/src/index.ts index 149eaeb..066e15f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -7,6 +7,7 @@ import sendGridMail from '@sendgrid/mail'; import { userProfileRoutes } from './user-profile/routes'; import { categoryRoutes } from './user-profile/category-routes'; import { shelfRoutes } from './user-profile/shelf-routes'; +import { wordRoutes } from './user-profile/word-routes'; const SENDGRID_API_KEY = process.env.SENDGRID_API_KEY || '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'; @@ -21,6 +22,7 @@ app.use('/', authRoutes); app.use('/', userProfileRoutes); app.use('/', categoryRoutes); app.use('/', shelfRoutes); +app.use('/', wordRoutes); app.get('/', (request, response) => { response.send('Server running @ port' + app.get('port')); diff --git a/src/library/route.ts b/src/library/route.ts deleted file mode 100644 index e69de29..0000000 diff --git a/src/user-profile/word-routes.ts b/src/user-profile/word-routes.ts new file mode 100644 index 0000000..8b5adef --- /dev/null +++ b/src/user-profile/word-routes.ts @@ -0,0 +1,64 @@ +import express from 'express'; +import passport from 'passport'; +import { DB_NAME, getDatabaseClient } from '../db-utils'; +import { Word } from '../models/word'; + +export const wordRoutes = express.Router(); + +export const jwtAuthentication = passport.authenticate('jwt', { session: false }); + +// Get All Words + +wordRoutes.get('/all-words/', jwtAuthentication, async (request, response) => { + const wordCollection = getDatabaseClient().db(DB_NAME).collection('words'); + + const allWords = await wordCollection.find().toArray(); + + response.status(200); + response.json(allWords); + + return; +}); + +wordRoutes.get('/word/', jwtAuthentication, async (request, response) => { + const wordCollection = getDatabaseClient().db(DB_NAME).collection('words'); + + if (!request.body.name) { + response.status(400); + response.send("Fill in the word to search for"); + return; + } + + const allWords = await wordCollection.find({ + name: {'$regex':'^' + request.body.name.toString().toUpperCase() + '*'} + }).toArray(); + + response.status(200); + response.json(allWords); + + return; +}); + +wordRoutes.post('/word/', jwtAuthentication, async (request, response) => { + const wordCollection = getDatabaseClient().db(DB_NAME).collection('words'); + + if (!request.body.name || request.body.grammaticalDetails) { + response.status(400); + response.send("Missing name or grammatical details"); + return; + } + + if (!request.body.grammaticalDetails.type || !request.body.grammaticalDetails.description || !request.body.grammaticalDetails.context || !request.body.grammaticalDetails.examples) { + response.status(400); + response.send("Missing params in grammatical details"); + return; + } + + await wordCollection.insertOne({ + name: request.body.name, + languageType: 'ENGLISH', + grammaticalDetails: request.body.grammaticalDetails, + }); + + return; +});