session.js 989 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import Session from "../models/Session.js";
  2. const createRoute = async (req, res, next)=>{
  3. try{
  4. const session = createSession(req.body);
  5. await session.save();
  6. res.json(responseSession(session));
  7. }catch(e){next(e)}
  8. }
  9. /*
  10. Create a new Session object
  11. @param {Object} - Body data from the object
  12. @return {Session} - Newly created Session object
  13. */
  14. const createSession = (data)=>{
  15. return new Session({
  16. workout: data.workout,
  17. start: new Date(data.start),
  18. end: new Date(data.end),
  19. notes: data.notes,
  20. exercises: data.exercises
  21. });
  22. }
  23. /*
  24. Create modified Session for sending to frontend
  25. @param {Session} - Session object
  26. @return {Object} - Modified session object
  27. */
  28. const responseSession = (session)=>{
  29. return {
  30. workout: session.workout,
  31. start: session.start,
  32. end: session.end,
  33. notes: session.notes,
  34. exercises: session.exercises
  35. };
  36. }
  37. export {
  38. createRoute
  39. }