Skip to content

hahaha #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: lesson-5
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
2 changes: 0 additions & 2 deletions .gitignore

This file was deleted.

2 changes: 1 addition & 1 deletion backend/.env
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
PORT=4000
MONGO_URI=mongodb+srv://mario:test12345@mernapp.2susood.mongodb.net/merndb?retryWrites=true&w=majority
MONGO_URI = mongodb+srv://hadayxinchao:test1234@mernapp.fhipyba.mongodb.net/?retryWrites=true&w=majority
85 changes: 85 additions & 0 deletions backend/controllers/workoutController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
const Workout = require('../models/workoutModel')
const mongoose = require('mongoose')

//get all workout
const getWorkouts = async (req, res) => {
const workouts = await Workout.find({}).sort({createAt: -1})

res.status(200).json(workouts)
}

//get singler workout
const getWorkout = async (req, res) => {
const {id} = req.params

if (!mongoose.Types.ObjectId.isValid(id)) {
return res.status(404).json({error: 'No such workout'})
}

const workout = await Workout.findById(id)

if (!workout) {
return res.status(400).json({error: 'No such workout'})
}

res.status(200).json(workout)
}

//create new workout
const createWorkout = async (req, res) => {
const {title, load, reps} = req.body

//add doc to db
try {
const workout = await Workout.create({title, load, reps})
res.status(200).json(workout)
} catch (error) {
res.status(400).json({error: error.message})
}

res.json({mssg: 'POST a new workout'})
}

//delete a workout
const deleteWorkout = async (req, res) => {
const { id } = req.params

if (!mongoose.Types.ObjectId.isValid(id)) {
return res.status(404).json({error: 'No such workout'})
}

const workout = await Workout.findOneAndDelete({_id: id})

if (!workout) {
return res.status(400).json({error: 'No such workout'})
}

res.status(200).json(workout)
}

//update a workout
const updateWorkout = async (req, res) => {
const { id } = req.params

if (!mongoose.Types.ObjectId.isValid(id)) {
return res.status(404).json({error: 'No such workout'})
}

const workout = await Workout.findOneAndUpdate({_id: id}, {
...req.body
})

if (!workout) {
return res.status(400).json({error: 'No such workout'})
}

res.status(200).json(workout)
}

module.exports = {
getWorkouts,
getWorkout,
createWorkout,
deleteWorkout,
updateWorkout
}
2 changes: 1 addition & 1 deletion backend/models/workoutModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ const workoutSchema = new Schema({
}
}, { timestamps: true })

module.exports = mongoose.model('Workout', workoutSchema)
module.exports = mongoose.model('Workout', workoutSchema)
12 changes: 12 additions & 0 deletions backend/node_modules/.bin/mime

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions backend/node_modules/.bin/mime.cmd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions backend/node_modules/.bin/mime.ps1

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading