ソースを参照

User profile deep copy API

master
kj1352 4年前
コミット
2c32aeaae1
1個のファイルの変更61行の追加1行の削除
  1. +61
    -1
      src/user-profile/profile-routes.ts

+ 61
- 1
src/user-profile/profile-routes.ts ファイルの表示

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

export const userProfileRoutes = express.Router();

@@ -22,6 +24,64 @@ userProfileRoutes.get('/profile/', jwtAuthentication, async (request, response)
return;
});

// Get Deep copy of data
userProfileRoutes.get('/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');

const allDeepCategories: any = [];

const allCategories = await categoryCollection.find({
_id: { $in: user.categories.map(id => new ObjectId(id)) }
}).toArray();


try {
for (let i = 0; i < allCategories.length; i += 1) {
if (allCategories[i].shelves) {
const shelves: any = await shelfCollection.find({
_id: { $in: allCategories[i].shelves.map(id => new ObjectId(id)) }
}).toArray();
for (let j = 0; j < allCategories[i].shelves.length; j += 1) {
if (shelves[j].words) {
const words = await wordCollection.find({
_id: { $in: shelves[j].words.map((word: any) => new ObjectId(word.word)) }
}).toArray();
words.forEach((word, index) => {
shelves[j].words[index].word = word;
});
}
}
allDeepCategories.push({
...allCategories[i],
shelves,
});
}
}
} catch {
response.sendStatus(500);
return;
}

response.status(200);
response.json({
_id: user._id,
name: user.name,
email: user.email,
isVerified: user.isVerified,
categories: allDeepCategories
});

return;
});

// Add favourite word

userProfileRoutes.post('/favourite-words/', jwtAuthentication, async (request, response) => {


読み込み中…
キャンセル
保存