Ver a proveniência

Words API Complete

master
kj1352 há 4 anos
ascendente
cometimento
f062482fc0
3 ficheiros alterados com 66 adições e 0 eliminações
  1. +2
    -0
      src/index.ts
  2. +0
    -0
      src/library/route.ts
  3. +64
    -0
      src/user-profile/word-routes.ts

+ 2
- 0
src/index.ts Ver ficheiro

@@ -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'));


+ 0
- 0
src/library/route.ts Ver ficheiro


+ 64
- 0
src/user-profile/word-routes.ts Ver ficheiro

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

Carregando…
Cancelar
Guardar