Skip to content

Commit

Permalink
add editExcercise logic in front and back, improve deleteExcercise to…
Browse files Browse the repository at this point in the history
… reassign indexes b00tc4mp#182
  • Loading branch information
AgusBirman committed Aug 6, 2024
1 parent 5797022 commit 688c75b
Show file tree
Hide file tree
Showing 16 changed files with 288 additions and 47 deletions.
31 changes: 31 additions & 0 deletions staff/agustin-birman/api/handlers/editExcerciseHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import jwt from '../util/jsonwebtoken-promised.js'

import logic from '../logic/index.js'

import { CredentialsError } from 'com/errors.js'

const { JWT_SECRET } = process.env

export default (req, res, next) => {
try {
const token = req.headers.authorization.slice(7)

jwt.verify(token, JWT_SECRET)
.then(payload => {
const { sub: userId } = payload

const { excerciseId, sentence } = req.body

try {
logic.editExcercise(userId, excerciseId, sentence)
.then(() => res.status(200).send())
.catch(error => next(error))
} catch (error) {
next(error)
}
})
.catch(error => next(new CredentialsError(error.message)))
} catch (error) {
next(error)
}
}
4 changes: 3 additions & 1 deletion staff/agustin-birman/api/handlers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import editActivityHandler from './editActivityHandler.js'

import getExcercisesHandler from './getExcercisesHandler.js'
import deleteExcerciseHandler from './deleteExcerciseHandler.js'
import editExcerciseHandler from './editExcerciseHandler.js'

export {
registerUserHandler,
Expand All @@ -29,5 +30,6 @@ export {
editActivityHandler,

getExcercisesHandler,
deleteExcerciseHandler
deleteExcerciseHandler,
editExcerciseHandler
}
5 changes: 4 additions & 1 deletion staff/agustin-birman/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import {
deleteActivityHandler,
getExcercisesHandler,
deleteExcerciseHandler,
editActivityHandler
editActivityHandler,
editExcerciseHandler
} from './handlers/index.js'

const { MONGODB_URL, PORT } = process.env
Expand Down Expand Up @@ -57,6 +58,8 @@ mongoose.connect(MONGODB_URL)

api.delete('/excercise/:excerciseId', deleteExcerciseHandler)

api.patch('/excercise/:excerciseId', jsonBodyParser, editExcerciseHandler)

api.use(errorHandler)

api.listen(PORT, () => console.log(`API is running on PORT ${PORT}`))
Expand Down
14 changes: 13 additions & 1 deletion staff/agustin-birman/api/logic/deleteExcercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,19 @@ const deleteExcercise = (userId, excerciseId) => {

return Excercise.deleteOne({ _id: new ObjectId(excerciseId) })
.catch(error => { throw new SystemError(error.message) })
.then(() => { })
.then(() => {

return Excercise.find({ activity: excercise.activity, index: { $gt: excercise.index } }).sort({ index: 1 })
.catch(error => { throw new SystemError(error.message) })
.then(excercises => {
const updatePromises = excercises.map((_excercise, index) => {
return Excercise.updateOne({ _id: new ObjectId(_excercise._id) }, { $set: { index: excercise.index + index } })
})
return Promise.all(updatePromises)
.catch(error => { throw new SystemError(error.message) })
.then(() => { })
})
})
})
})
})
Expand Down
2 changes: 1 addition & 1 deletion staff/agustin-birman/api/logic/deleteExcercise.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const { MONGODB_URL } = process.env
mongoose.connect(MONGODB_URL)
.then(() => {
try {
deleteExcercise('66a94dcb34505782bcd8cfd0', '66afc3b7f25abf38240bc9ac')
deleteExcercise('66a94dcb34505782bcd8cfd0', '66b259c0ec1e53453e51a4be')
.then(() => console.log('excercise deleted'))
.catch(error => console.error(error))
} catch (error) {
Expand Down
36 changes: 36 additions & 0 deletions staff/agustin-birman/api/logic/editExcercise.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import validate from "com/validate.js"
import { Excercise, User } from "../data/index.js"
import { NotFoundError, SystemError } from "com/errors.js"
import { Types } from "mongoose"

const { ObjectId } = Types

const editExcercise = (userId, excerciseId, sentence) => {
validate.id(userId, 'userId')
validate.id(excerciseId, 'excerciseId')
validate.textCompleteSentence(sentence, 'sentence', 50)

return User.findById(userId).lean()
.catch(error => { throw new SystemError(error.message) })
.then(user => {
if (!user)
throw new NotFoundError('user not found')

return Excercise.findById(excerciseId)
.catch(error => { throw new SystemError(error.message) })
.then(excercise => {
if (!excercise)
throw new NotFoundError('excercise not found')

const update = {}
update.sentence = sentence

return Excercise.updateOne({ _id: new ObjectId(excerciseId) }, { $set: update })
.catch(error => { throw new SystemError(error.message) })
.then(() => { })
})
})
}

export default editExcercise

19 changes: 19 additions & 0 deletions staff/agustin-birman/api/logic/editExcercise.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

import mongoose from 'mongoose'

import 'dotenv/config'
import editExcercise from './editExcercise.js'

const { MONGODB_URL } = process.env

mongoose.connect(MONGODB_URL)
.then(() => {
try {
editExcercise('66a94dcb34505782bcd8cfd0', '66b259e1ec1e53453e51a4ce', 'alex (eats) bananaaaaa')
.then(() => console.log('actvity edited'))
.catch(error => console.error(error))
} catch (error) {
console.error(error)
}
})
.catch(error => console.error(error))
4 changes: 3 additions & 1 deletion staff/agustin-birman/api/logic/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import deleteActivity from './deleteActivity.js'
import editActivity from './editActivity.js'
import getExcercises from './getExcercises.js'
import deleteExcercise from './deleteExcercise.js'
import editExcercise from './editExcercise.js'

const logic = {
registerUser,
Expand All @@ -21,7 +22,8 @@ const logic = {
editActivity,
deleteActivity,
getExcercises,
deleteExcercise
deleteExcercise,
editExcercise
}

export default logic
33 changes: 33 additions & 0 deletions staff/agustin-birman/app/src/logic/editExcercise.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import errors, { SystemError } from "com/errors"
import validate from "com/validate"

const editExcercise = (excerciseId, sentence) => {
validate.id(excerciseId, 'excerciseId')
validate.textCompleteSentence(sentence, 'sentence')

return fetch(`${import.meta.env.VITE_API_URL}/excercise/${excerciseId}`, {
method: 'PATCH',
headers: {
Authorization: `Bearer ${sessionStorage.token}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({ excerciseId, sentence })
})
.catch(() => { throw new SystemError('server error') })
.then(response => {
if (response.status === 200)
return

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 editExcercise
4 changes: 3 additions & 1 deletion staff/agustin-birman/app/src/logic/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import deleteActivity from './deleteActivity'
import editActivity from './editActivity'
import getExcercises from './getExcercises'
import deleteExcercise from './deleteExcercise'
import editExcercise from './editExcercise'

const logic = {
registerUser,
Expand All @@ -25,7 +26,8 @@ const logic = {
deleteActivity,
editActivity,
getExcercises,
deleteExcercise
deleteExcercise,
editExcercise
}

export default logic
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,9 @@ h2{
border: 1px solid black;
border-radius: 10px;
margin: 0 5px;
}

.Exercise{
width: 30%;
height: auto;
}
Loading

0 comments on commit 688c75b

Please sign in to comment.