소스 검색

Create route for creating workouts.

Lee Morgan 1 년 전
부모
커밋
c33e685de9
5개의 변경된 파일67개의 추가작업 그리고 4개의 파일을 삭제
  1. 29 1
      controllers/workout.js
  2. 10 1
      documentation/bruno/User/Create.bru
  3. 26 0
      documentation/bruno/Workout/Create.bru
  4. 1 1
      models/Workout.js
  5. 1 1
      routes/workout.js

+ 29 - 1
controllers/workout.js

@@ -1,9 +1,37 @@
+import Workout from "../models/Workout.js";
+
 const createRoute = async (req, res, next)=>{
     try{
-        return null;
+        const workout = createWorkout(req.body, res.locals.user._id);
+        await workout.save();
+        res.json(responseWorkout(workout));
     }catch(e){next(e)}
 }
 
+/*
+ Create a new Workout object
+ @param {Object} - Body data from the request
+ @param {ObjectId} userId - ID of the user creating the workout
+ @return {Workout} - Workout object
+ */
+const createWorkout = (data, userId)=>{
+    return new Workout({
+        user: userId,
+        exercises: data.exercises
+    });
+}
+
+/*
+ Create a new workout object for sending to the frontend
+ @param {Workout} workout - Workout object
+ @return {Object} - Modified Workout object
+ */
+const responseWorkout = (workout)=>{
+    return {
+        exercises: workout.exercises
+    };
+}
+
 export {
     createRoute
 }

+ 10 - 1
documentation/bruno/User/Create.bru

@@ -6,6 +6,15 @@ meta {
 
 post {
   url: http://localhost:8000/user
-  body: none
+  body: json
   auth: none
 }
+
+body:json {
+  {
+    "name": "Lee Morgan",
+    "email": "lee@leemorgan.dev",
+    "pass": "leerobertmorgan",
+    "confirmPass": "leerobertmorgan"
+  }
+}

+ 26 - 0
documentation/bruno/Workout/Create.bru

@@ -0,0 +1,26 @@
+meta {
+  name: Create
+  type: http
+  seq: 1
+}
+
+post {
+  url: http://localhost:8000/workout
+  body: json
+  auth: none
+}
+
+body:json {
+  {
+    "exercises": [
+      {
+        "name": "First",
+        "type": "weights"
+      },
+      {
+        "name": "Second exercise",
+        "type": "weights"
+      }
+    ]
+  }
+}

+ 1 - 1
models/Workout.js

@@ -3,7 +3,7 @@ import mongoose from "mongoose";
 const WorkoutSchema = new mongoose.Schema({
     user: {
         type: mongoose.Schema.Types.ObjectId,
-        required: true
+        required: true,
     },
     exercises: [{
         name: {

+ 1 - 1
routes/workout.js

@@ -5,5 +5,5 @@ import {
 import {userAuth} from "../auth.js";
 
 export default (app)=>{
-    app.get("/workout", userAuth, createRoute);
+    app.post("/workout", userAuth, createRoute);
 }