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 editExcercise logic in front and back, improve deleteExcercise to…
… reassign indexes b00tc4mp#182
- Loading branch information
1 parent
5797022
commit 688c75b
Showing
16 changed files
with
288 additions
and
47 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
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) | ||
} | ||
} |
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
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,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 | ||
|
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,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)) |
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 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 |
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 |
---|---|---|
|
@@ -16,4 +16,9 @@ h2{ | |
border: 1px solid black; | ||
border-radius: 10px; | ||
margin: 0 5px; | ||
} | ||
|
||
.Exercise{ | ||
width: 30%; | ||
height: auto; | ||
} |
Oops, something went wrong.