Ver a proveniência

moved and renamed folder structure to make the routes contextual, update shelf API now accepts words and checks for the presence against the DB

master
kj1352 há 4 anos
ascendente
cometimento
df29d53bca
5 ficheiros alterados com 38 adições e 11 eliminações
  1. +3
    -3
      src/index.ts
  2. +4
    -4
      src/library/library-routes.ts
  3. +2
    -2
      src/models/shelf.ts
  4. +0
    -0
      src/user-profile/profile-routes.ts
  5. +29
    -2
      src/user-profile/shelf-routes.ts

+ 3
- 3
src/index.ts Ver ficheiro

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


src/user-profile/word-routes.ts → src/library/library-routes.ts Ver ficheiro

@@ -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) {

+ 2
- 2
src/models/shelf.ts Ver ficheiro

@@ -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,
}



src/user-profile/routes.ts → src/user-profile/profile-routes.ts Ver ficheiro


+ 29
- 2
src/user-profile/shelf-routes.ts Ver ficheiro

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


Carregando…
Cancelar
Guardar