@@ -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')); | |||
@@ -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<Word>('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<Word>('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<Word>('words'); | |||
if (!request.body.name || request.body.grammaticalDetails) { |
@@ -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<AddedWord> | |||
} | |||
interface MongoAddedWord extends Omit<AddedWord, "word"> { | |||
export interface MongoAddedWord extends Omit<AddedWord, "word"> { | |||
word: string, | |||
} | |||
@@ -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<MongoShelf>('shelves'); | |||
const wordCollection = getDatabaseClient().db(DB_NAME).collection<Word>('words'); | |||
const updatedAddedWords: Array<MongoAddedWord> = 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 | |||
} | |||
}); | |||