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 logic submitAnswer in api, refactor completeSentence b00tc4mp#182
- Loading branch information
1 parent
688c75b
commit ed1dc70
Showing
14 changed files
with
210 additions
and
29 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
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,22 @@ | ||
import logic from '../logic/index.js' | ||
import jwt from '../util/jsonwebtoken-promised.js' | ||
|
||
const { JWT_SECRET } = process.env | ||
|
||
export default (req, res, next) => { | ||
try { | ||
const token = req.headers.authorization.slice(7) | ||
|
||
const { exerciseId, answer } = req.body | ||
jwt.verify(token, JWT_SECRET) | ||
try { | ||
logic.submitAnswer(userId, exerciseId, answer) | ||
.then(() => res.status(201).json()) | ||
.catch(error => next(error)) | ||
} catch (error) { | ||
next(error) | ||
} | ||
} 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import 'dotenv/config' | ||
import mongoose from 'mongoose' | ||
import bcrypt from 'bcryptjs' | ||
|
||
import { expect } from 'chai' | ||
|
||
import { User, Activity } from '../data/index.js' | ||
import { ContentError, CredentialsError } from 'com/errors.js' | ||
|
||
import createActivity from './createActivity.js' | ||
|
||
const { MONGODB_URL_TEST } = process.env | ||
|
||
describe('createActivity', () => { | ||
before(() => mongoose.connect(MONGODB_URL_TEST)) | ||
|
||
beforeEach(() => Promise.all([User.deleteMany(), Activity.deleteMany()])) | ||
|
||
it('succeds on creating activity', () => | ||
bcrypt.hash('123123123', 8) | ||
.then(hash => User.create({ name: 'Mac', surname: 'Book', email: '[email protected]', username: 'macbook', password: hash, userType: 'teacher' })) | ||
.then(user => | ||
createActivity(user.id, 'Hola', 'Pepe') | ||
.then(() => Activity.findOne()) | ||
.then(activity => { | ||
expect(activity.title).to.equal('Hola') | ||
expect(activity.description).to.equal('Pepe') | ||
}) | ||
) | ||
) | ||
}) |
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,41 @@ | ||
import validate from "com/validate.js"; | ||
import { User, Answer, Excercise } from "../data/index.js"; | ||
import { NotFoundError, SystemError } from "com/errors.js"; | ||
|
||
const submitAnswer = (userId, excerciseId, answer) => { | ||
validate.id(userId, 'userId') | ||
validate.id(excerciseId, 'excerviseId') | ||
validate.text(answer, 'answer') | ||
|
||
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).lean() | ||
.catch(error => { throw new SystemError(error.message) }) | ||
.then(excercise => { | ||
if (!excercise) | ||
throw new NotFoundError('excercise not found') | ||
|
||
const newAnswer = { | ||
student: userId, | ||
exercise: excerciseId, | ||
answer | ||
} | ||
|
||
return Answer.create(newAnswer) | ||
.catch(error => { throw new SystemError(error.message) }) | ||
.then(answer => { | ||
answer.id = answer._id.toString() | ||
|
||
delete answer._id | ||
|
||
return answer.id | ||
}) | ||
}) | ||
}) | ||
} | ||
|
||
export default submitAnswer |
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,17 @@ | ||
import 'dotenv/config' | ||
import mongoose from 'mongoose' | ||
import submitAnswer from './submitAnswer.js' | ||
|
||
const { MONGODB_URL } = process.env | ||
|
||
mongoose.connect(MONGODB_URL) | ||
.then(() => { | ||
try { | ||
submitAnswer('66a94dcb34505782bcd8cfd0', '66b1cdc7debd28877917c736', 'hat') | ||
.then(() => console.log('answer submitted')) | ||
.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
File renamed without changes.
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
64 changes: 64 additions & 0 deletions
64
staff/agustin-birman/app/src/views/components/DoActivity/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 |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { useParams } from "react-router-dom"; | ||
import View from "../../../components/library/View"; | ||
import { useEffect, useState } from "react"; | ||
import Heading from "../../../components/core/Heading"; | ||
import Text from "../../../components/core/Text"; | ||
import logic from "../../../logic"; | ||
import Input from "../../../components/core/Input"; | ||
|
||
let SENTENCE_REGEX = /^(.*?)\s*\(.*?\)\s*(.*?)$/ | ||
|
||
function DoActivity() { | ||
const [exercises, setExercises] = useState([]) | ||
const { activityId } = useParams() | ||
|
||
useEffect(() => { | ||
loadExcercises() | ||
}, []) | ||
|
||
const loadExcercises = () => { | ||
try { | ||
logic.getExcercises(activityId) | ||
.then(exercises => setExercises(exercises)) | ||
.catch(error => { | ||
console.error(error) | ||
|
||
alert(error.message) //TODO hacer un alert mejor | ||
}) | ||
} catch (error) { | ||
console.error(error) | ||
|
||
alert(error.message) | ||
} | ||
} | ||
|
||
const handleSubmittedAnswer = () => { | ||
//TODO | ||
} | ||
|
||
return (<View> | ||
<Heading level='2'></Heading> | ||
{exercises.map(exercise => { | ||
let beforeParentheses = '' | ||
let afterParentheses = '' | ||
|
||
let matches = exercise.sentence.match(SENTENCE_REGEX); | ||
|
||
if (matches) { | ||
beforeParentheses = matches[1].trim() | ||
afterParentheses = matches[2].trim() | ||
} | ||
|
||
return (<View key={exercise.index}> | ||
<Text>{exercise.index + 1} Exercise</Text> | ||
<Text>{beforeParentheses}</Text> | ||
<Input /> | ||
<Text>{afterParentheses}</Text> | ||
</View>) | ||
} | ||
)} | ||
</View > | ||
) | ||
} | ||
|
||
export default DoActivity |
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