-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ability to add goals and view existing goals
- Loading branch information
1 parent
614c760
commit 92aa623
Showing
10 changed files
with
304 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
const { getCustomError } = require("../../helpers/errorHandler"); | ||
const { getAuthenticatedUser } = require("../../helpers/authenticationHelpers"); | ||
|
||
async function addGoal(req, res, db) { | ||
if ( | ||
!req.body || | ||
!req.body.data || | ||
!req.body.data.goal | ||
) { | ||
return res.status(400).json(getCustomError(400)); | ||
} | ||
|
||
const authenticatedUser = getAuthenticatedUser(req.headers); | ||
|
||
if (!authenticatedUser) { | ||
return res.status(401).json(getCustomError(401)); | ||
} | ||
|
||
const { name, time, startDate, endDate } = req.body.data.goal; | ||
|
||
const record = await db('goals').insert({ | ||
user_email: authenticatedUser.email, | ||
goal_name: name, | ||
goal_times: time, | ||
goal_start_date: startDate, | ||
goal_end_date: endDate | ||
}, ['id']); | ||
|
||
if (!record || !record[0] || !record[0].id) { | ||
return res.status(400).json(getCustomError(400)); | ||
} | ||
|
||
return res.json({ | ||
type: "Goal created", | ||
attributes: { | ||
value: record[0].id, | ||
} | ||
}); | ||
} | ||
|
||
module.exports = addGoal; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
const { getCustomError } = require("../../helpers/errorHandler"); | ||
const { getAuthenticatedUser } = require("../../helpers/authenticationHelpers"); | ||
|
||
async function getGoals(req, res, db) { | ||
const authenticatedUser = getAuthenticatedUser(req.headers); | ||
|
||
if (!authenticatedUser) { | ||
return res.status(401).json(getCustomError(401)); | ||
} | ||
|
||
const records = await db('goals').where({ | ||
user_email: authenticatedUser.email | ||
}); | ||
|
||
if (!records) { | ||
return res.status(400).json(getCustomError(400)); | ||
} | ||
|
||
return res.json({ | ||
type: "Goals fetched", | ||
attributes: { | ||
value: records, | ||
} | ||
}); | ||
} | ||
|
||
module.exports = getGoals; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.