Skip to content

Lesson 14 #7

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 13 commits into
base: main
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules/
.env
2 changes: 0 additions & 2 deletions README.md

This file was deleted.

2 changes: 2 additions & 0 deletions backend/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
PORT=4000
MONGO_URI=mongodb+srv://mario:[email protected]/merndb?retryWrites=true&w=majority
98 changes: 98 additions & 0 deletions backend/controllers/workoutController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
const Workout = require('../models/workoutModel')
const mongoose = require('mongoose')

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

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

// get a single 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(404).json({error: 'No such workout'})
}

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

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

let emptyFields = []

if (!title) {
emptyFields.push('title')
}
if (!load) {
emptyFields.push('load')
}
if (!reps) {
emptyFields.push('reps')
}
if (emptyFields.length > 0) {
return res.status(400).json({ error: 'Please fill in all fields', emptyFields })
}

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

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

if (!mongoose.Types.ObjectId.isValid(id)) {
return res.status(400).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(400).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
}
20 changes: 20 additions & 0 deletions backend/models/workoutModel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const mongoose = require('mongoose')

const Schema = mongoose.Schema

const workoutSchema = new Schema({
title: {
type: String,
required: true
},
reps: {
type: Number,
required: true
},
load: {
type: Number,
required: true
}
}, { timestamps: true })

module.exports = mongoose.model('Workout', workoutSchema)
Loading