Skip to content

Commit

Permalink
Merge pull request #2058 from edenmind/YunusAndreasson/issue2057
Browse files Browse the repository at this point in the history
refactor(api): ♻️ Increase robustness in api
  • Loading branch information
YunusAndreasson authored Apr 8, 2023
2 parents 636b0b2 + c58ce7b commit 6c64bba
Showing 30 changed files with 181 additions and 150 deletions.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified api/.yarn/install-state.gz
Binary file not shown.
Empty file added api/.yarn/versions/47b98512.yml
Empty file.
1 change: 0 additions & 1 deletion api/controllers/readiness.js
Original file line number Diff line number Diff line change
@@ -11,7 +11,6 @@ function getReadiness(request, reply) {
axios.get('http://127.0.0.1:3030/texts'),
axios.get('http://127.0.0.1:3030/authors'),
axios.get('http://127.0.0.1:3030/images'),
axios.get('http://127.0.0.1:3030/words'),
axios.get('http://127.0.0.1:3030/health'),
axios.get('http://127.0.0.1:3030/')
])
71 changes: 32 additions & 39 deletions api/controllers/words.js
Original file line number Diff line number Diff line change
@@ -5,31 +5,43 @@
/* eslint-disable operator-linebreak */

const COLLECTIONS = require('../constants/collections.js')
const { capitalizeFirstLetter, shuffleArray, convertToLowerCase, getNumberFromString } = require('../services/texts')
const { capitalizeFirstLetter, shuffleArray, convertToLowerCase, getAllWordsFromTexts } = require('../services/texts')

async function getWordId(request, reply) {
const { wordId, sentenceId, textId } = request.params
try {
const { wordId, sentenceId, textId } = request.params

const texts = this.mongo.db.collection(COLLECTIONS.TEXTS)
const textDocument = await texts.findOne({ textGuid: textId })
// eslint-disable-next-line putout/nonblock-statement-body-newline
if (textDocument) {
const sentence = textDocument.sentences.find((s) => s.id === sentenceId)
// eslint-disable-next-line putout/nonblock-statement-body-newline
if (sentence) {
const word = sentence.words.find((w) => w.id === wordId)

const updatedWord = {
...word,
englishSentence: sentence.english,
arabicSentence: sentence.arabic
// Validate input parameters
if (!wordId || !sentenceId || !textId) {
return reply.code(400).send('Missing required parameters.')
}

const texts = this.mongo.db.collection(COLLECTIONS.TEXTS)
const textDocument = await texts.findOne({ textGuid: textId })

if (textDocument) {
const sentence = textDocument.sentences.find((s) => s.id === sentenceId)
if (sentence) {
const word = sentence.words.find((w) => w.id === wordId)
if (word) {
const updatedWord = {
...word,
englishSentence: sentence.english,
arabicSentence: sentence.arabic
}

return reply.code(200).send(updatedWord)
}

return reply.code(404).send('Word not found.')
}

return reply.code(200).send(updatedWord)
return reply.code(404).send('Sentence not found.')
}
return reply.code(404).send('Text not found.')
} catch {
return reply.code(500).send('An error occurred while processing the request.')
}

return reply.code(404).send('Word not found.')
}

async function getWordTranslation(request, reply) {
@@ -47,28 +59,8 @@ async function getWordTranslation(request, reply) {
async function getWords(request, reply) {
const { query } = request
const { numberOfWordsToPractice, difficultyLevel } = query

// loop through all texts in COLLECTIONS.TEXT and add all words in all sentences to an array
const allWords = []
const texts = this.mongo.db.collection(COLLECTIONS.TEXTS)
const allTexts = await texts.find({}).toArray()
for (const text of allTexts) {
for (const sentence of text.sentences) {
for (const word of sentence.words) {
const updatedWord = {
id: word.id,
...word,
textId: text.textGuid,
sentenceId: sentence.id,
wordId: word.id,
arabicSentence: sentence.arabic,
englishSentence: sentence.english,
categoryLevel: getNumberFromString(text.category)
}
allWords.push(updatedWord)
}
}
}
const allWords = await getAllWordsFromTexts(texts)

// if numberOfWordsToPractice and difficultyLevel are not provided then return all words
if (!numberOfWordsToPractice && !difficultyLevel) {
@@ -79,6 +71,7 @@ async function getWords(request, reply) {

//get all words where categoryLevel is equal to the difficultyLevel
const wordsFilteredByDifficultyLevel = allWords.filter((word) => word.categoryLevel === difficultyLevelNumber)
//get random words because we do not want to practice in the same order
const randomWords = wordsFilteredByDifficultyLevel.sort(() => Math.random() - 0.5).slice(0, numberOfWordsToPractice)

const allWordsWithAlternative = randomWords.map((word) => {
10 changes: 5 additions & 5 deletions api/package.json
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@
"license": "MIT",
"homepage": "https://openarabic.io",
"repository": "https://github.com/edenmind/OpenArabic",
"version": "1444.4.179",
"version": "1444.4.180",
"authors": [
"Yunus Andreasson <yunus@edenmind.com> (https://github.com/YunusAndreasson)"
],
@@ -20,7 +20,7 @@
"lint": "eslint '**/*.js' --ignore-pattern node_modules/"
},
"dependencies": {
"@aws-sdk/client-s3": "^3.306.0",
"@aws-sdk/client-s3": "^3.309.0",
"@fastify/autoload": "^5.7.1",
"@fastify/cors": "^8.2.1",
"@fastify/env": "^4.2.0",
@@ -44,14 +44,14 @@
"uuid": "^9.0.0"
},
"devDependencies": {
"eslint": "^8.37.0",
"eslint": "^8.38.0",
"eslint-plugin-import": "^2.27.5",
"eslint-plugin-prettier": "^4.2.1",
"eslint-plugin-putout": "^17.2.1",
"eslint-plugin-putout": "^17.3.0",
"eslint-plugin-security": "^1.7.1",
"eslint-plugin-unicorn": "^46.0.0",
"prettier": "^2.8.7",
"putout": "^29.2.1",
"putout": "^29.2.3",
"tap": "^16.3.4"
}
}
21 changes: 21 additions & 0 deletions api/services/texts.js
Original file line number Diff line number Diff line change
@@ -267,8 +267,29 @@ function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1)
}

async function getAllWordsFromTexts(textsCollection) {
const allTexts = await textsCollection.find({}).toArray()

return allTexts.flatMap((text) => {
return text.sentences.flatMap((sentence) => {
return sentence.words.map((word) => {
return {
id: word.id,
...word,
textId: text.textGuid,
sentenceId: sentence.id,
wordId: word.id,
arabicSentence: sentence.arabic,
englishSentence: sentence.english,
categoryLevel: getNumberFromString(text.category)
}
})
})
})
}
module.exports = {
generateAudio,
getAllWordsFromTexts,
batchGenerateAudio,
generateGuidForSentencesAndWords,
addGuidToArray,
Loading

0 comments on commit 6c64bba

Please sign in to comment.