소스 검색

Create route for creating a session.
Write documentation for creating a session route.

Lee Morgan 1 년 전
부모
커밋
f44db21d54
4개의 변경된 파일95개의 추가작업 그리고 1개의 파일을 삭제
  1. 2 0
      app.js
  2. 43 0
      controllers/session.js
  3. 41 1
      documentation/data.js
  4. 9 0
      routes/session.js

+ 2 - 0
app.js

@@ -10,6 +10,7 @@ import {catchError} from "./HttpError.js";
 import otherRoutes from "./routes/other.js";
 import userRoutes from "./routes/user.js";
 import workoutRoutes from "./routes/workout.js";
+import sessionRoutes from "./routes/session.js";
 
 let mongoString = "mongodb://127.0.0.1/workout";
 if(process.env.NODE_ENV === "production"){
@@ -40,6 +41,7 @@ app.use(express.json());
 otherRoutes(app);
 userRoutes(app);
 workoutRoutes(app);
+sessionRoutes(app);
 
 app.use(catchError);
 

+ 43 - 0
controllers/session.js

@@ -0,0 +1,43 @@
+import Session from "../models/Session.js";
+
+const createRoute = async (req, res, next)=>{
+    try{
+        const session = createSession(req.body);
+        await session.save();
+        res.json(responseSession(session));
+    }catch(e){next(e)}
+}
+
+/*
+ Create a new Session object
+ @param {Object} - Body data from the object
+ @return {Session} - Newly created Session object
+ */
+const createSession = (data)=>{
+    return new Session({
+        workout: data.workout,
+        start: new Date(data.start),
+        end: new Date(data.end),
+        notes: data.notes,
+        exercises: data.exercises
+    });
+}
+
+/*
+ Create modified Session for sending to frontend
+ @param {Session} - Session object
+ @return {Object} - Modified session object
+ */
+const responseSession = (session)=>{
+    return {
+        workout: session.workout,
+        start: session.start,
+        end: session.end,
+        notes: session.notes,
+        exercises: session.exercises
+    };
+}
+
+export {
+    createRoute
+}

+ 41 - 1
documentation/data.js

@@ -204,7 +204,7 @@ window.data = [
             },
             {
                 name: "notes",
-                type: "String (optional)",
+                type: "String",
                 desc: "Notes from the user about the session"
             },
             {
@@ -213,5 +213,45 @@ window.data = [
                 desc: "List of flexible objects. Structure of each object based on the type of exercise"
             }
         ]
+    },
+    {
+        type: "route",
+        id: "createSession",
+        title: "Create",
+        url: "POST /session",
+        auth: true,
+        description: "Create a new workout session",
+        requestBody: [
+            {
+                name: "workout",
+                type: "String",
+                desc: "ID of the workout this is part of"
+            },
+            {
+                name: "start",
+                type: "Date",
+                desc: "Date/Time of session start"
+            },
+            {
+                name: "end",
+                type: "Date",
+                desc: "Date/Time of session end"
+            },
+            {
+                name: "notes",
+                type: "String, (optional)",
+                desc: "Any notes from user about the session"
+            },
+            {
+                name: "exercises",
+                type: "[Object]",
+                desc: "List of flexible objects. Structure of each object based on the type of exercise"
+            }
+        ],
+        responeBody: [{
+            name: "N/A",
+            type: "Session",
+            desc: "Session object"
+        }]
     }
 ]

+ 9 - 0
routes/session.js

@@ -0,0 +1,9 @@
+import {
+    createRoute
+} from "../controllers/session.js";
+
+import {userAuth} from "../auth.js";
+
+export default (app)=>{
+    app.post("/session", userAuth, createRoute);
+}