Express TS project
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

124 行
3.8 KiB

  1. import express from 'express';
  2. import passport from 'passport';
  3. import { MongoShelf, MongoShelfWord } from '../models/shelf';
  4. import { DB_NAME, getDatabaseClient } from '../db-utils';
  5. import { MongoCategory } from '../models/category';
  6. import { MongoUser } from '../models/user';
  7. import { ObjectId } from 'bson';
  8. import { RecollectionHistory } from '../models/recollection-history';
  9. export const recollectionRoutes = express.Router();
  10. export const jwtAuthentication = passport.authenticate('jwt', { session: false });
  11. recollectionRoutes.get('/questions/', jwtAuthentication, async (request, response) => {
  12. const user: MongoUser = (request.user as any);
  13. const shelfCollection = getDatabaseClient().db(DB_NAME).collection<MongoShelf>('shelves');
  14. const categoryCollection = getDatabaseClient().db(DB_NAME).collection<MongoCategory>('categories');
  15. if (!user.categories || user.categories.length === 0) {
  16. response.status(400);
  17. response.send("No categories found");
  18. return;
  19. }
  20. let categoryIds: Array<ObjectId> = user.categories.map(id => new ObjectId(id));
  21. const categoryDetails = await categoryCollection.find({
  22. _id: { $in : categoryIds }
  23. }).toArray();
  24. const shelfIds: Array<ObjectId> = [];
  25. for (let i = 0; i < categoryDetails.length; i += 1) {
  26. if (categoryDetails[i].shelves) {
  27. for (let j = 0; j < categoryDetails[i].shelves.length; j += 1) {
  28. shelfIds.push(new ObjectId(categoryDetails[i].shelves[j]));
  29. }
  30. }
  31. }
  32. const shelfDetails: any = await shelfCollection.find({
  33. _id: { $in : shelfIds }
  34. }).toArray();
  35. if (!shelfDetails || shelfDetails.length === 0) {
  36. response.status(400);
  37. response.send("No shelves found");
  38. return;
  39. }
  40. interface WordWithShelfId extends MongoShelfWord {
  41. shelfId: string
  42. }
  43. const allWords: Array<WordWithShelfId> = [];
  44. for (let i = 0; i < shelfDetails.length; i += 1) {
  45. if (shelfDetails[i].words) {
  46. for (let j = 0; j < shelfDetails[i].words.length; j += 1) {
  47. allWords.push({
  48. ...shelfDetails[i].words[j],
  49. shelfId: shelfDetails[i]._id
  50. });
  51. }
  52. }
  53. }
  54. if (allWords.length === 0) {
  55. response.status(400);
  56. response.send("No words found");
  57. return;
  58. }
  59. const recollectionWords: Array<MongoShelfWord> = [];
  60. allWords.forEach(word => {
  61. return new Date(word.nextRevisionDateTime) < new Date() ? recollectionWords.push(word): null;
  62. });
  63. response.status(200);
  64. response.json(recollectionWords);
  65. return;
  66. });
  67. recollectionRoutes.post('/success/', jwtAuthentication, async (request, response) => {
  68. const shelfCollection = getDatabaseClient().db(DB_NAME).collection<MongoShelf>('shelves');
  69. const recollectionHistoryCollection = getDatabaseClient().db(DB_NAME).collection<RecollectionHistory>('recollection-history');
  70. if (!request.body.shelfId || !request.body.wordId) {
  71. response.status(400);
  72. response.send("Missing params shelfId or wordId");
  73. return;
  74. }
  75. const matchedShelf = await shelfCollection.findOne({
  76. _id: new ObjectId(request.body.shelfId)
  77. });
  78. if (matchedShelf) {
  79. const matchedWord = matchedShelf.words.find((word) => {
  80. return word.word === request.body.wordId;
  81. });
  82. matchedWord.spaceBetweenRecall *= 2;
  83. let now = new Date();
  84. matchedWord.nextRevisionDateTime = new Date(now.setDate(now.getDate() + matchedWord.spaceBetweenRecall));
  85. response.status(200);
  86. response.send(matchedWord);
  87. } else {
  88. response.status(400);
  89. response.send("Unmatched Shelf");
  90. }
  91. return;
  92. });