diff --git a/src/index.ts b/src/index.ts index 3c190c0..50f5547 100644 --- a/src/index.ts +++ b/src/index.ts @@ -4,10 +4,10 @@ import passport from 'passport'; import authRoutes from './authentication/routes'; import { connectToDatabaseServer } from './db-utils'; import sendGridMail from '@sendgrid/mail'; -import { userProfileRoutes } from './user-profile/routes'; +import { userProfileRoutes } from './user-profile/profile-routes'; import { categoryRoutes } from './user-profile/category-routes'; import { shelfRoutes } from './user-profile/shelf-routes'; -import { wordRoutes } from './user-profile/word-routes'; +import { libraryRoutes } from './library/library-routes'; const SENDGRID_API_KEY = process.env.SENDGRID_API_KEY || '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'; @@ -22,7 +22,7 @@ app.use('/auth/', authRoutes); app.use('/user/', userProfileRoutes); app.use('/category/', categoryRoutes); app.use('/shelf/', shelfRoutes); -app.use('/library/', wordRoutes); +app.use('/library/', libraryRoutes); app.get('/', (request, response) => { response.send('Server running @ port' + app.get('port')); diff --git a/src/user-profile/word-routes.ts b/src/library/library-routes.ts similarity index 86% rename from src/user-profile/word-routes.ts rename to src/library/library-routes.ts index 79d735a..88f3b60 100644 --- a/src/user-profile/word-routes.ts +++ b/src/library/library-routes.ts @@ -3,13 +3,13 @@ import passport from 'passport'; import { DB_NAME, getDatabaseClient } from '../db-utils'; import { Word } from '../models/word'; -export const wordRoutes = express.Router(); +export const libraryRoutes = express.Router(); export const jwtAuthentication = passport.authenticate('jwt', { session: false }); // Get All Words -wordRoutes.get('/all/', jwtAuthentication, async (request, response) => { +libraryRoutes.get('/all/', jwtAuthentication, async (request, response) => { const wordCollection = getDatabaseClient().db(DB_NAME).collection('words'); const allWords = await wordCollection.find().toArray(); @@ -20,7 +20,7 @@ wordRoutes.get('/all/', jwtAuthentication, async (request, response) => { return; }); -wordRoutes.get('/search/', jwtAuthentication, async (request, response) => { +libraryRoutes.get('/search/', jwtAuthentication, async (request, response) => { const wordCollection = getDatabaseClient().db(DB_NAME).collection('words'); if (!request.body.name) { @@ -39,7 +39,7 @@ wordRoutes.get('/search/', jwtAuthentication, async (request, response) => { return; }); -wordRoutes.post('/add/', jwtAuthentication, async (request, response) => { +libraryRoutes.post('/add/', jwtAuthentication, async (request, response) => { const wordCollection = getDatabaseClient().db(DB_NAME).collection('words'); if (!request.body.name || request.body.grammaticalDetails) { diff --git a/src/models/shelf.ts b/src/models/shelf.ts index c34567a..a1e6866 100644 --- a/src/models/shelf.ts +++ b/src/models/shelf.ts @@ -16,7 +16,7 @@ interface AddedWord { isArchived: boolean, } -export interface Shelf { +interface Shelf { name: string, description?: string, viewType: viewPermissionType, @@ -24,7 +24,7 @@ export interface Shelf { addedWords?: Array } -interface MongoAddedWord extends Omit { +export interface MongoAddedWord extends Omit { word: string, } diff --git a/src/user-profile/routes.ts b/src/user-profile/profile-routes.ts similarity index 100% rename from src/user-profile/routes.ts rename to src/user-profile/profile-routes.ts diff --git a/src/user-profile/shelf-routes.ts b/src/user-profile/shelf-routes.ts index 14c0239..fe7a448 100644 --- a/src/user-profile/shelf-routes.ts +++ b/src/user-profile/shelf-routes.ts @@ -1,10 +1,11 @@ -import { ObjectId } from 'bson'; +import { ObjectID, ObjectId } from 'bson'; import express from 'express'; import passport from 'passport'; import { MongoCategory } from '../models/category'; import { DB_NAME } from '../db-utils'; import { getDatabaseClient } from '../db-utils'; -import { MongoShelf } from '../models/shelf'; +import { MongoAddedWord, MongoShelf } from '../models/shelf'; +import { Word } from '../models/word'; export const shelfRoutes = express.Router(); @@ -97,6 +98,8 @@ shelfRoutes.post('/add/', jwtAuthentication, async (request, response) => { // Update shelf shelfRoutes.put('/update/', jwtAuthentication, async (request, response) => { const shelfCollection = getDatabaseClient().db(DB_NAME).collection('shelves'); + const wordCollection = getDatabaseClient().db(DB_NAME).collection('words'); + const updatedAddedWords: Array = request.body.addedWords ? request.body.addedWords : []; const currentShelf = await shelfCollection.findOne({ _id: new ObjectId(request.body._id) @@ -115,6 +118,29 @@ shelfRoutes.put('/update/', jwtAuthentication, async (request, response) => { } } + + for (let i = 0; i < updatedAddedWords.length; i += 1) { + if (!updatedAddedWords[i].word || !updatedAddedWords[i].isArchived || + !updatedAddedWords[i].isFavourite || !updatedAddedWords[i].nextRevisionDateTime || + !updatedAddedWords[i].notes || !updatedAddedWords[i].revisionHistory || !updatedAddedWords[i].spaceBetweenRecall) { + response.status(400); + response.send("Missing key in " + JSON.stringify(updatedAddedWords[i])); + return; + } + + const wordId = updatedAddedWords[i].word; + + const matchedWord = await wordCollection.findOne({ + _id: new ObjectId(wordId) + }); + + if (!matchedWord) { + response.status(400); + response.send("Word with ID " + updatedAddedWords[i] + " is not present in the library" ); + return; + } + } + try { await shelfCollection.updateOne({ _id: new ObjectId(request.body._id), @@ -122,6 +148,7 @@ shelfRoutes.put('/update/', jwtAuthentication, async (request, response) => { $set: { name: request.body.name ? request.body.name : currentShelf.name, description: request.body.description ? request.body.description : currentShelf.description, + addedWords: updatedAddedWords, isArchived: request.body.isArchived !== undefined ? request.body.isArchived : currentShelf.isArchived } });