Created
October 7, 2018 06:41
-
-
Save stephenhuh/a08528ed2635307bb4d58eaddb68916c to your computer and use it in GitHub Desktop.
scratch
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
basicSelect = () => { | |
return knex('workouts') | |
.select('*') | |
} | |
basicFilter = (knex) => { | |
return knex | |
.where('workout_id', 1) | |
} | |
//res = await basicFilter(basicSelect()); | |
f = await compose(basicFilter, basicSelect)(); | |
const getSets = (exerciseIds) => { | |
return knex('sets') | |
.select('*') | |
.whereIn('exercise_id',exerciseIds) | |
.then(res => { | |
logger.info(`${JSON.stringify(res)}`) ; | |
return res; | |
}) | |
} | |
const getExercises = (workoutId) => { | |
return knex('exercises') | |
.select('exercise_id') | |
.then(res => { | |
logger.info(`res ${JSON.stringify(res)}`) | |
return res; | |
}) | |
} | |
//Top-down approach | |
const getSetsAcc = (exerciseIds, acc) => { | |
return knex('sets') | |
.select('*') | |
.whereIn('exercise_id', exerciseIds) | |
.tap((sets) => { | |
sets.forEach((set) => { | |
const idx = findIndex(propEq('exercise_id', set.exercise_id))(acc); | |
acc[idx].sets.push(set); | |
}) | |
}) | |
.then(res => { | |
return res; | |
}) | |
.catch(e => { | |
throw e; | |
}) | |
} | |
const getExercisesAcc = (workoutId, acc) => { | |
return knex('exercises') | |
.select('*') | |
.where('workout_id', workoutId) | |
.tap((exercises) => { | |
exercises.forEach(exercise => { | |
const {exercise_name, exercise_id} = exercise; | |
const jsonObj = {}; | |
jsonObj.exercise_name = exercise_name; | |
jsonObj.exercise_id = exercise_id; | |
jsonObj.sets = []; //initialize sets array | |
acc.push(o); | |
}) | |
}) | |
.then(exercises => { | |
return exercises; | |
}) | |
.catch(e => { | |
throw e; | |
}) | |
} | |
const cGetExercisesAcc = curry(getExercisesAcc); | |
const cGetSetsAcc = curry(getSetsAcc); | |
const ogAcc = []; | |
const pluckIds = pluck('exercise_id'); | |
const getWorkout = await composeP(cGetSetsAcc(__, ogAcc), pluckIds, cGetExercisesAcc(__, ogAcc)); | |
//Bottom-up approach, could work, but needs workoutId to know which exerciseIds to pick. | |
const getSetsAccUp = (exerciseIds, acc) => { | |
return knex('sets') | |
.select('*') | |
.whereIn('exercise_id', exerciseIds) | |
.tap((sets) => { | |
groupWith((a, b) => { | |
}, sets) | |
}) | |
.then(res => { | |
return res; | |
}) | |
.catch(e => { | |
throw e; | |
}) | |
} | |
//Bottom-up approach | |
//const b = await getExercisesAcc(44, ogAcc); | |
//console.log('plucked ids', JSON.stringify(b)); | |
//const c = await getSetsAcc(b, ogAcc); | |
//console.log('c', JSON.stringify(c)); | |
console.log('ogacc', JSON.stringify(ogAcc)); | |
//console.log('sameRes', JSON.stringify(sameRes)); | |
//const pipeline = composeP(getSets,pluckIds,getExercises) | |
//const pip = await getSetsAcc([8], accum) | |
//res2 = await composeP(basicFilter,basicSelect)(); | |
/* | |
return basicSelect() | |
.where('workout_id', 1) | |
*/ | |
//const result = await pipeline(1); | |
//logger.info(`${JSON.stringify(result)}`) | |
//logger.info(`${JSON.stringify(accum)}`) | |
// | |
const getSetsForExercise = (exerciseId) => { | |
if (exerciseId === undefined || typeof exerciseId !== 'number') { | |
//TODO: review with someone who knows better on this error handling. | |
const e = new Error(`Error getting sets for exercises: invalid params were passed`); | |
return Promise.reject(e); | |
} | |
return knex('sets') | |
.where('exercise_id', exerciseId) | |
.andWhere('deleted', false) | |
.select('weight', 'reps', 'notes', 'set_id', 'completed') | |
.orderBy('set_id', 'asc') | |
.then((res) => { | |
return res; | |
}) | |
.catch((err) => { | |
logger.error(`Error getting sets for an exercise: ${err}`) ; | |
return 'er'; | |
}) | |
} | |
const getExercises = (workoutId) => { | |
//exercises: [{exerciseTitle: "blah", sets: [{}]...}] | |
if (workoutId === undefined || typeof workoutId !== 'number') { | |
const e = new Error('getExercisesForWorkout: Invalid params'); | |
return Promise.reject(e); | |
} | |
const getExercisesForWorkout = (workoutId) => { | |
if (workoutId === undefined || typeof workoutId !== 'number') { | |
const e = new Error('Error getting exercise for workout, invalid args') ; | |
return Promise.reject(e); | |
} | |
return knex('exercises') | |
.where('workout_id', workoutId) | |
.andWhere('deleted', false) | |
.select('*') | |
.then((exercises)=>{ | |
logger.info(`Selecting from Workouts: ${JSON.stringify(exercises)}`) | |
return exercises; | |
}) | |
.catch((err) => { | |
logger.error(`Error selecting from workouts in getWorkout: ${err.stack}`) ; | |
}) | |
} | |
let exercisesResponse = []; | |
getExercisesForWorkout(workoutId) | |
.then((exercises) => { | |
/* Start building up the response here */ | |
exercises.forEach(({exercise_name, exercise_id}) => { | |
exercisesResponse.push({ | |
exerciseTitle: exercise_name, | |
exerciseId : exercise_id | |
}); | |
}) | |
return exercises; | |
}) | |
.then(pluckIds) | |
.then((exerciseIds) => { | |
let promises = []; | |
exerciseIds.forEach((exercise_id) => { | |
let p = new Promise((resolve) => { | |
getSetsForExercise(exercise_id) | |
.then((sets) => { | |
let idx = exercisesResponse.findIndex((el) => { | |
return el.exerciseId === exercise_id | |
}) | |
exercisesResponse[idx].sets = sets; | |
return exercisesResponse; | |
}) | |
.then((res) => { | |
resolve(res); | |
return res; | |
}) | |
}); | |
promises.push(p); | |
}); | |
return Promise.all(promises); | |
}) | |
.then((res) => { | |
logger.info("FINAL THEN", JSON.stringify(res)); | |
//TODO: rewrite -- get help or focus on this problem | |
return res[res.length-1]; | |
}) | |
.catch((err) => { | |
logger.error(`Something went wrong in getExercisesForWorkout: ${err.stack}`); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment