Skip to content

Commit

Permalink
feat: added an option to add multi exercise/categories by comma sep (#…
Browse files Browse the repository at this point in the history
…154)

feat: added an option to add multi exercise/categories by comma sep
  • Loading branch information
wajeht authored Aug 8, 2022
1 parent 2d72be3 commit 0b753a9
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,20 @@ export async function getExerciseCategories(req, res) {
*/
export async function postExerciseCategory(req, res) {
const body = req.body;
const created = await ExerciseCategoriesQueries.createExerciseCategory(body);
const ec = req.body.name;
const uid = req.body.user_id;

const data = ec
.split(',')
.filter((curr) => curr.length != 0)
.map((curr) => {
return {
name: curr.trim(),
user_id: uid,
};
});

const created = await ExerciseCategoriesQueries.createExerciseCategory(data);

if (!created.length) throw new CustomError.BadRequestError(`Something went wrong while creating a exercise categories for User ID: ${body.user_id}!`); // prettier-ignore

Expand Down
13 changes: 11 additions & 2 deletions src/apps/api/v1/exercise-categories/exercise-categories.queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,15 @@ export function searchExerciseCategoryName(name, uid) {
* @param [body] - The body of the request.
* @returns The exercise category that was created.
*/
export function createExerciseCategory(body = { name, user_id }) {
return db.insert(body).into('exercise_categories').returning('*');
// export function createExerciseCategory(body = { name, user_id }) {
// return db.insert(body).into('exercise_categories').returning('*');
// }

/**
* It takes in an object, inserts it into the database, and returns the object
* @param data - an object containing the data to be inserted into the database
* @returns The data that was inserted into the exercise_categories table.
*/
export function createExerciseCategory(data) {
return db.insert(data).into('exercise_categories').returning('*');
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ export const getExerciseCategories = [
const user = await UserQueries.findUserById(user_id);
if (user.length === 0) throw new Error('User does not exist!');
return true;
}),
})
.toInt(),
];

/* Checking the body of the request to make sure it has the required fields. */
Expand All @@ -41,7 +42,8 @@ export const postExerciseCategory = [
const user = await UserQueries.findUserById(user_id);
if (user.length === 0) throw new Error('User does not exist!');
return true;
}),
})
.toInt(),
body('name')
.trim()
.notEmpty()
Expand Down
14 changes: 13 additions & 1 deletion src/apps/api/v1/exercises/exercises.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,19 @@ export async function getExercises(req, res) {
*/
export async function postExercise(req, res) {
const body = req.body;
const created = await ExercisesQueries.createExercise(body);

const e = body.name
.split(',')
.filter((curr) => curr.length != 0)
.map((curr) => {
return {
name: curr.trim(),
exercise_category_id: body.exercise_category_id,
user_id: body.user_id,
};
});

const created = await ExercisesQueries.createExercise(e);

if (!created.length) throw new CustomError.BadRequestError(`Something went wrong while creating a exercise for User ID: ${body.user_id}!`); // prettier-ignore

Expand Down
20 changes: 17 additions & 3 deletions src/apps/api/v1/exercises/exercises.queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ export function getExerciseById(id) {
* @returns An array of objects
*/
export function getExerciseByUserId(uid) {
return db.select('*').from('exercises').where({ user_id: uid }).andWhere({ deleted: false });
return db
.select('*')
.from('exercises')
.where({ user_id: uid })
.andWhere({ deleted: false })
.orderBy('id', 'desc');
}

/**
Expand Down Expand Up @@ -60,8 +65,17 @@ export function searchExerciseName(name, uid, ecid) {
* @param [body] - an object with the following keys: name, exercise_category_id, user_id
* @returns The exercise that was just created.
*/
export function createExercise(body = { name, exercise_category_id, user_id }) {
return db.insert(body).into('exercises').returning('*');
// export function createExercise(body = { name, exercise_category_id, user_id }) {
// return db.insert(body).into('exercises').returning('*');
// }

/**
* It takes in an object, inserts it into the database, and returns the object
* @param data - an object containing the data to be inserted into the database
* @returns The data that was inserted into the database.
*/
export function createExercise(data) {
return db.insert(data).into('exercises').returning('*');
}

/**
Expand Down
11 changes: 10 additions & 1 deletion src/apps/ui/pages/dashboard/sessions/Categories.vue
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,16 @@ async function addAExerciseCategory() {
}
}
categories.value.unshift(category);
const data = category.name
.split(',')
.filter((c) => c.length != 0)
.map((c) => {
return { name: c, user_id: category.user_id };
});
data.forEach((c) => {
categories.value.unshift(c);
});
clearDataAndDismissModal();
Expand Down
15 changes: 14 additions & 1 deletion src/apps/ui/pages/dashboard/sessions/Exercises.vue
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,20 @@ async function addAExercise() {
}
}
exercises.value.unshift(exercise);
const data = exercise.name
.split(',')
.filter((c) => c.length != 0)
.map((c) => {
return {
name: c.trim(),
exercise_category_id: exercise.exercise_category_id,
user_id: exercise.user_id,
};
});
data.forEach((c) => {
exercises.value.unshift(c);
});
clearDataAndDismissModal();
Expand Down

0 comments on commit 0b753a9

Please sign in to comment.