PostInheritedComments.ts 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import { HttpMethod, Route, StatusCodes } from 'org.crazydoctor.expressts';
  2. import { Request, Response } from 'express';
  3. import ServerApp from '..';
  4. import { CommentsManager } from '../comments/CommentsManager';
  5. class PostInheritedComments extends Route {
  6. protected action = (req: Request, res: Response): any => {
  7. const params = req.body;
  8. const query = JSON.parse(params.query);
  9. if(ServerApp.SourcesUpdating) {
  10. res.status(StatusCodes.CONFLICT).send('Sources are being updated.');
  11. return;
  12. }
  13. const inheritedComments: any = {};
  14. const querySize = Object.keys(query).length;
  15. let index = 0;
  16. for(const className of Object.keys(query)) {
  17. const obj = query[className];
  18. const root = obj.root;
  19. const properties = obj.properties;
  20. CommentsManager.getCommentsByClass(root, className, properties).then((result) => {
  21. if(!result.success) {
  22. res.status(StatusCodes.INTERNAL_SERVER_ERROR).send('Internal server error');
  23. return;
  24. }
  25. const commentsObj: any = {};
  26. result.comments?.forEach((comment) => {
  27. result.comments?.forEach((comment) => {
  28. commentsObj[comment.propertyName] = comment;
  29. });
  30. });
  31. inheritedComments[className] = commentsObj;
  32. index++;
  33. if(index === querySize)
  34. res.status(StatusCodes.OK).send(JSON.stringify(inheritedComments));
  35. });
  36. }
  37. };
  38. protected method = HttpMethod.POST;
  39. protected order = 8;
  40. protected route = '/getInheritedComments';
  41. }
  42. export default PostInheritedComments;