Skip to content

Commit

Permalink
Merge pull request #22 from tom1dev/create-local-database-feature
Browse files Browse the repository at this point in the history
Create local database feature
  • Loading branch information
LX6T authored Aug 11, 2024
2 parents f8709de + 5236ff5 commit d9bcb36
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 9 deletions.
65 changes: 64 additions & 1 deletion server/controllers/exercises-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
const knex = require('./../db')

// Retrieve all exercises
const workoutsAll = (req, res) => {
const exercisesAll = (req, res) => {
// Get all exercises from database
knex
.select('*') // select all exercises
Expand Down Expand Up @@ -48,3 +48,66 @@ const exerciseByNameDateAndSets = (req, res) => {
)

}


//creates a new exercise
const createExercise = (req, res) => {
const {name,muscleGroup} = req.params


knex('exercises')
.insert({
'name': name,
'muscle_group': muscleGroup
})
//if error occurs then drops insert apon error
.onConflict('name').ignore()
.returning('name')
.then(name => {
if (name.length > 0) {
res.status(201).json({ message: 'Exercise added successfully'});
} else {
res.status(200).json({ message: 'Exercise already exists, no new entry created' });
}
})
.catch(error => {
res.status(500).json({ message: `An error occurred while creating a new exercises`, error: error.message });
});
}

//logs new exercise Set
const logExerciseSet = (req, res) => {
const {name,date,set,weight,rep,score}= req.params


knex('exercises_history')
.insert({
'name': name,
'date': date,
'set': set,
'weight': weight,
'rep':rep,
'score':score
})
//if conflict occurs then drops current insert apon error
.onConflict(['name','date','set']).ignore()
.returning('name')
.then(name => {
if (name.length > 0) {
res.status(201).json({ message: 'Exercise set added successfully'});
} else {
res.status(200).json({ message: 'Conflicting exercise name, date of completion, or set number. no new entry created' });
}
})
.catch(error => {
// Error: Something went wrong
res.status(500).json({ message: `An error occurred while creating a new exercises`, error: error.message });
});
}

module.exports = {
exercisesAll,
exerciseByNameDateAndSets,
createExercise,
logExerciseSet
};
32 changes: 29 additions & 3 deletions server/controllers/routines-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
const knex = require('./../db')

// Retrieve all workouts
const workoutsAll = (req, res) => {
const routinesAll = (req, res) => {
// Get all routines from database
knex
.select('*') // select all routines
Expand Down Expand Up @@ -46,7 +46,33 @@ const routineByNameAndDate = (req, res) => {
)
}

//creates a new routine
const createRoutine = (req, res) => {
const {name,date} = req.params


knex('routines')
.insert({
'name': name,
'date': date
})
//if error occurs then drops insert apon error
.onConflict('name').ignore()
.returning('name')
.then(name => {
if (name.length > 0) {
res.status(201).json({ message: 'routine added successfully'});
} else {
res.status(200).json({ message: 'routine already exists, no new entry created' });
}
})
.catch(error => {
res.status(500).json({ message: `An error occurred while creating a new exercises`, error: error.message });
});
}

module.exports = {
workoutsAll,
routineByNameAndDate
routinesAll,
routineByNameAndDate,
createRoutine
};
29 changes: 28 additions & 1 deletion server/controllers/workouts-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,34 @@ const workoutByDate = (req, res) => {
})
}


//creates a new workout
const createWorkout = (req, res) => {
const {date} = req.params


knex('workouts')
.insert({
'date': date
})
//if error occurs then drops insert apon error
.onConflict('date').ignore()
.returning('date')
.then(date => {
if (date.length > 0) {
res.status(201).json({ message: 'workout added successfully'});
} else {
res.status(200).json({ message: 'workout already exists, no new entry created' });
}
})
.catch(error => {
res.status(500).json({ message: `An error occurred while creating a new exercises`, error: error.message });
});
}

module.exports = {
workoutsAll,
workoutByDate
workoutByDate,
createWorkout

};
11 changes: 9 additions & 2 deletions server/routes/exercises-routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,14 @@ const router = express.Router()
// Get all exercises
router.get('/all', exercisesController.exercisesAll)

// Get exercise by name
router.get('/:name', exercisesController.exerciseByName)
// Get exercise by name, date and set
router.get('/:name/:date/:set', exercisesController.exerciseByNameDateAndSets)

//Create a new exercise
router.post('/create', exercisesController.createExercise)

//Log a set
router.post('/Log/:name/:date/:set/:weight/:rep/:score', exercisesController.logExerciseSet)


module.exports = router;
8 changes: 6 additions & 2 deletions server/routes/routines-routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ const routinesController = require('../controllers/routines-controller');
const router = express.Router();

// Get all routines
router.get('/', routinesController.getRoutines);
router.get('/', routinesController.routinesAll);

// Get a routine by name and date
router.get('/:name/:date', routinesController.getRoutine);
router.get('/:name/:date', routinesController.routineByNameAndDate);

// create a new routine
router.post('/:name/:date', routinesController.createRoutine);


module.exports = router;
2 changes: 2 additions & 0 deletions server/routes/workouts-routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ router.get('/all', workoutsController.workoutsAll)
// Get workout by date, in format 'YYYY-MM-DD'
router.get('/:date', workoutsController.workoutByDate)

router.post('/create/:date', workoutsController.createWorkout)

module.exports = router;
3 changes: 3 additions & 0 deletions server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const cors = require('cors')
const helmet = require('helmet')

const workoutsRouter = require('./routes/workouts-routes')
const exercisesRouter = require('./routes/exercises-routes')

const PORT = 4001

Expand All @@ -30,6 +31,8 @@ app.use(bodyParser.json())
// Implement workouts route
app.use('/workouts', workoutsRouter)

app.use('/exercises', exercisesRouter)

app.get('/', (req, res) => {
res.send('Hello, world!');
});
Expand Down

0 comments on commit d9bcb36

Please sign in to comment.