Ver a proveniência

profile deep copy API

master
kj1352 há 4 anos
ascendente
cometimento
d8e0a77bd2
2 ficheiros alterados com 90 adições e 1 eliminações
  1. +1
    -1
      src/models/shelf.ts
  2. +89
    -0
      src/user-profile/profile-routes.ts

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

@@ -16,7 +16,7 @@ interface AddedWord {
isArchived: boolean,
}

interface Shelf {
export interface Shelf {
name: string,
description?: string,
viewType: viewPermissionType,


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

@@ -1,6 +1,11 @@
import { ObjectId } from 'bson';
import express from 'express';
import passport from 'passport';
import { MongoShelf } from '../models/shelf';
import { DB_NAME, getDatabaseClient } from '../db-utils';
import { MongoCategory } from '../models/category';
import { MongoUser } from '../models/user';
import { Word } from '../models/word';

export const userProfileRoutes = express.Router();

@@ -18,3 +23,87 @@ userProfileRoutes.get('/profile/', jwtAuthentication, async (request, response)
});
return;
});


userProfileRoutes.get('/profile/deep-copy/', jwtAuthentication, async (request, response) => {
const user: MongoUser = (request.user as any);
const categoryCollection = getDatabaseClient().db(DB_NAME).collection<MongoCategory>('categories');
const shelfCollection = getDatabaseClient().db(DB_NAME).collection<MongoShelf>('shelves');
const wordCollection = getDatabaseClient().db(DB_NAME).collection<Word>('words');

let deepCategories = [];

for (let i = 0; i < user.categories.length; i += 1) {
let deepShelves = [];

const matchedCategory: any = await categoryCollection.findOne({
_id: new ObjectId(user.categories[i])
});

if (matchedCategory && matchedCategory.shelves) {
for (let j = 0; j < matchedCategory.shelves.length; j += 1) {
let deepAddedWords = [];
const matchedShelf: any = await shelfCollection.findOne({
_id: new ObjectId(matchedCategory.shelves[j])
});
if (matchedShelf && matchedShelf.addedWords) {
for (let k = 0; k < matchedShelf.addedWords.length; k += 1) {
const matchedWord = await wordCollection.findOne({
_id: new ObjectId(matchedShelf.addedWords[k].word)
});

if (matchedWord) {
deepAddedWords.push(matchedWord);
}
}
}
matchedShelf.addedWords = deepAddedWords;
deepShelves.push(matchedShelf);
}
}

matchedCategory.shelves = deepShelves;
deepCategories.push(matchedCategory);

}

let deepUncategorisedAddedWords = [];
const matchedUncategoisedShelf: any = await shelfCollection.findOne({
_id: new ObjectId(user.uncategorised)
});
if (matchedUncategoisedShelf && matchedUncategoisedShelf.addedWords) {
for (let k = 0; k < matchedUncategoisedShelf.addedWords.length; k += 1) {
const matchedWord = await wordCollection.findOne({
_id: new ObjectId(matchedUncategoisedShelf.addedWords[k].word)
});

if (matchedWord) {
deepUncategorisedAddedWords.push(matchedWord);
}
}

matchedUncategoisedShelf.addedWords = deepUncategorisedAddedWords;
}

response.json({
_id: user._id,
name: user.name,
email: user.email,
isVerified: user.isVerified,
categories: deepCategories,
uncategorised: matchedUncategoisedShelf
});



return;
});

Carregando…
Cancelar
Guardar