forked from b00tc4mp/isdi-parttime-202403
-
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.
Add getRandomWorkout logic in api and app with test; add WorkoutDetai…
…ls view; modify Workout data model in README b00tc4mp#178
- Loading branch information
Showing
32 changed files
with
523 additions
and
278 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
staff/debora-garcia/project/api/handlers/getRandomWorkoutHandler.js
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,32 @@ | ||
import "dotenv/config" | ||
import logic from "../logic/index.js" | ||
import jwt from "../utils/jsonwebtoken-promised.js" | ||
import { CredentialsError } from "com/errors.js" | ||
|
||
const { JWT_SECRET } = process.env | ||
const getRandomWorkoutHandler = (req, res, next) => { | ||
try { | ||
const token = req.headers.authorization.slice(7) | ||
|
||
jwt.verify(token, JWT_SECRET) | ||
.then(payload => { | ||
const { sub: userId } = payload | ||
const { workoutType } = req.params | ||
|
||
try { | ||
logic.getRandomWorkout(userId, workoutType) | ||
.then(randomWorkout => res.json(randomWorkout)) | ||
.catch(error => next(error)) | ||
} catch (error) { | ||
next(error) | ||
} | ||
|
||
}) | ||
.catch(error => next(new CredentialsError(error.message))) | ||
|
||
} catch (error) { | ||
next(error) | ||
} | ||
} | ||
|
||
export default getRandomWorkoutHandler |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { User, Workout } from "../data/index.js" | ||
import { SystemError, NotFoundError } from "com/errors.js" | ||
import validate from "com/validate.js" | ||
|
||
const getRandomWorkout = (userId, workoutType) => { | ||
validate.id(userId, "userId") | ||
validate.type(workoutType, "workoutType") | ||
|
||
return User.findById(userId).lean() | ||
.catch(error => { throw new SystemError(error.message) }) | ||
.then(user => { | ||
if (!user) throw new NotFoundError("user not found") | ||
|
||
return Workout.find({ workoutType }).select("-__v").populate("movements").lean() | ||
.catch(error => { throw new SystemError(error.message) }) | ||
.then(workouts => { | ||
if (!workouts) throw new NotFoundError("workouts not found") | ||
|
||
const randomNumber = Math.floor(Math.random() * workouts.length) | ||
|
||
const randomWorkout = workouts[randomNumber] | ||
|
||
if (!randomWorkout) throw new NotFoundError("workout not found") | ||
|
||
randomWorkout.id = randomWorkout._id.toString() | ||
|
||
delete randomWorkout._id | ||
|
||
return randomWorkout | ||
}) | ||
|
||
}) | ||
} | ||
export default getRandomWorkout |
18 changes: 18 additions & 0 deletions
18
staff/debora-garcia/project/api/logic/getRandomWorkout.test.js
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,18 @@ | ||
import "dotenv/config" | ||
import mongoose from "mongoose" | ||
|
||
import getRandomWorkout from "./getRandomWorkout.js" | ||
const { MONGODB_URL } = process.env | ||
|
||
mongoose.connect(MONGODB_URL) | ||
.then(() => { | ||
try { | ||
getRandomWorkout("66b0b8ff337c2b614a26583b", "emom") | ||
.then(randomWorkout => console.log(randomWorkout)) | ||
.catch(error => console.error(error)) | ||
|
||
} catch (error) { | ||
console.error(error) | ||
} | ||
}) | ||
.catch(error => console.error(error)) |
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 @@ | ||
curl http://localhost:8080/workouts/emom -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI2NmFiZTA1NjcxM2E4YmU5MmY4ZTU4NDQiLCJpYXQiOjE3MjI3OTUwMjAsImV4cCI6MTcyMzM5OTgyMH0.9JIikHMaqlttsnSqRe_YCT_1tYl9cEDfo3o6DFIePO8" -v |
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
2 changes: 1 addition & 1 deletion
2
staff/debora-garcia/project/app/src/components/Button/index.jsx
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import "./index.css" | ||
|
||
export default function Button({ type, className, onClick, children }) { | ||
return <button className={`Button ${className ? className : ""}`} onClick={onClick}>{children}</button> | ||
return <button className={`Button ${className ? className : ""}`} type={type} onClick={onClick}>{children}</button> | ||
|
||
} |
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
30 changes: 30 additions & 0 deletions
30
staff/debora-garcia/project/app/src/logic/getRandomWorkout.js
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,30 @@ | ||
import errors, { SystemError } from "com/errors" | ||
import validate from "com/validate" | ||
|
||
const getRandomWorkout = (workoutType) => { | ||
validate.type(workoutType, "workoutType") | ||
return fetch(`${import.meta.env.VITE_API_URL}/workouts/${workoutType}`, { | ||
method: "GET", | ||
headers: { | ||
Authorization: `Bearer ${sessionStorage.token}` | ||
} | ||
}) | ||
.catch(() => { throw new SystemError("server error") }) | ||
.then(response => { | ||
if (response.status === 200) { | ||
return response.json() | ||
.catch(() => { throw new SystemError("server error") }) | ||
.then(workoutRandom => workoutRandom) | ||
} | ||
return response.json() | ||
.catch(() => { throw new SystemError("server error") }) | ||
.then(body => { | ||
const { error, message } = body | ||
const constructor = errors[error] | ||
throw new constructor(message) | ||
}) | ||
}) | ||
} | ||
|
||
|
||
export default getRandomWorkout |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { useNavigate, Link, Routes, Route, useParams } from "react-router-dom" | ||
import { useState, useEffect, } from "react" | ||
import logic from "../logic" | ||
|
||
import Button from "../components/Button" | ||
|
||
export default function Amrap() { | ||
const { amrap } = useParams() | ||
const [workout, setWorkout] = useState({}) | ||
|
||
useEffect(() => { | ||
console.log("Benchmark ->render") | ||
try { | ||
logic.getRandomWorkout(amrap) | ||
.then((amrapkWorkout) => { | ||
setWorkout(amrapkWorkout) | ||
console.log(amrapkWorkout) | ||
}) | ||
.catch(error => alert(error.message)) | ||
} catch (error) { | ||
alert(error.message) | ||
} | ||
}, []) | ||
|
||
return ( | ||
<div> | ||
{workout?.workoutType && <p>{workout.workoutType}</p>} | ||
{workout?.title && <p>{workout.title}</p>} | ||
{workout?.duration && <p>{workout.duration}</p>} | ||
</div> | ||
); | ||
} | ||
|
Oops, something went wrong.