ProjectRouter.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. const Router = require('express')
  2. const routers = new Router();
  3. const authMiddlewere = require('../middleware/auth')
  4. const Project = require('../models/Project')
  5. const User = require("../models/User");
  6. routers.post(
  7. '/save',
  8. async (req,res)=>{
  9. try{
  10. const{title,codeHTML,codeCSS,codeJS,user,email}= req.body
  11. const candidate = await Project.findOne({title})
  12. if(candidate){
  13. return res.status(400).json({message:"this project already exists"})
  14. }
  15. const project = new Project({title,codeHTML,codeCSS,codeJS,user,email})
  16. await project.save()
  17. return res.json({message:"Project created"})
  18. }catch(e){
  19. console.log(e)
  20. res.send("error blyad")
  21. }
  22. }
  23. )
  24. routers.post(
  25. '/reSave',
  26. async (req,res)=>{
  27. try{
  28. const{id,title,codeHTML,codeCSS,codeJS}=req.body
  29. const proj = await Project.findById({_id: id})
  30. proj.updateOne({title},{title},function(err, result){ if(err) return console.log(err);})
  31. proj.updateOne({codeHTML},{codeHTML},function(err, result){ if(err) return console.log(err);})
  32. proj.updateOne({codeCSS},{codeCSS},function(err, result){ if(err) return console.log(err);})
  33. proj.updateOne({codeJS},{codeJS},function(err, result){ if(err) return console.log(err);})
  34. return res.json({message:"Project RESAVE"})
  35. }catch(e){
  36. console.log(e)
  37. res.send("error blyad")
  38. }
  39. }
  40. )
  41. routers.post(
  42. '/userProject',
  43. async (req,res)=>{
  44. try{
  45. const{email}=req.body
  46. const proj = await Project.find({email: email})
  47. let project = new Array
  48. for(let i = 0, l = proj.length; i < l; i++) {
  49. let prId = proj[i]._id
  50. let prTitle = proj[i].title
  51. project.push({prId,prTitle})
  52. }
  53. return res.json({
  54. project : project
  55. })
  56. }catch(e){
  57. console.log(e)
  58. res.send("error blyad")
  59. }
  60. }
  61. )
  62. routers.post(
  63. '/findProject',
  64. async (req,res)=>{
  65. try{
  66. const{id}=req.body
  67. const proj = await Project.findById({_id: id})
  68. return res.json({
  69. title : proj.title,
  70. codeHTML : proj.codeHTML,
  71. codeCSS : proj.codeCSS,
  72. codeJS : proj.codeJS,
  73. user : proj.user
  74. })
  75. }catch(e){
  76. console.log(e)
  77. res.send("error blyad")
  78. }
  79. }
  80. )
  81. routers.post(
  82. '/allProject',
  83. async (req,res)=>{
  84. try{
  85. const proj = await Project.find()
  86. let project = new Array
  87. for(let i = 0, l = proj.length; i < l; i++) {
  88. let prId = proj[i]._id
  89. let prTitle = proj[i].title
  90. let email = proj[i].email
  91. project.push({prId,prTitle,email})
  92. }
  93. return res.json({
  94. project : project
  95. })
  96. }catch(e){
  97. console.log(e)
  98. res.send("error blyad")
  99. }
  100. }
  101. )
  102. routers.post(
  103. '/deleteProject',
  104. async (req,res)=>{
  105. try{
  106. const{id}=req.body
  107. const proj = await Project.deleteOne({_id: id})
  108. return res.json({message:"Project deleted"})
  109. }catch(e){
  110. console.log(e)
  111. res.send("error blyad")
  112. }
  113. }
  114. )
  115. module.exports = routers