From ab8a9ebc99aa3d7d8f33603012662697033cd447 Mon Sep 17 00:00:00 2001 From: Ruslan Zagitov Date: Fri, 27 Dec 2024 23:15:26 +0300 Subject: [PATCH] feat: add '/units' endpoints; add 'put' and 'delete' requests to '/dictionaries' endpoint --- .../dictionaries/data/dictionaries.json | 2 +- .../eng-it-lean/dictionaries/index.js | 55 ++++++++++++++ .../routers/kfu-m-24-1/eng-it-lean/index.js | 4 +- .../eng-it-lean/units/data/unit-1.md | 71 +++++++++++++++++++ .../eng-it-lean/units/data/units.json | 1 + .../kfu-m-24-1/eng-it-lean/units/index.js | 58 +++++++++++++++ 6 files changed, 189 insertions(+), 2 deletions(-) create mode 100644 server/routers/kfu-m-24-1/eng-it-lean/units/data/unit-1.md create mode 100644 server/routers/kfu-m-24-1/eng-it-lean/units/data/units.json create mode 100644 server/routers/kfu-m-24-1/eng-it-lean/units/index.js diff --git a/server/routers/kfu-m-24-1/eng-it-lean/dictionaries/data/dictionaries.json b/server/routers/kfu-m-24-1/eng-it-lean/dictionaries/data/dictionaries.json index cf5e736..486dfce 100644 --- a/server/routers/kfu-m-24-1/eng-it-lean/dictionaries/data/dictionaries.json +++ b/server/routers/kfu-m-24-1/eng-it-lean/dictionaries/data/dictionaries.json @@ -1,7 +1,7 @@ [ { "id": 0, - "description": "1000 часто используемых", + "description": "10 часто используемых", "imageFilename": "kart1.jpg" }, { diff --git a/server/routers/kfu-m-24-1/eng-it-lean/dictionaries/index.js b/server/routers/kfu-m-24-1/eng-it-lean/dictionaries/index.js index ab86b1b..c203e4b 100644 --- a/server/routers/kfu-m-24-1/eng-it-lean/dictionaries/index.js +++ b/server/routers/kfu-m-24-1/eng-it-lean/dictionaries/index.js @@ -1,3 +1,5 @@ +const fs = require('fs'); +const path = require('path'); const router = require("express").Router(); module.exports = router; @@ -9,6 +11,59 @@ router.get("/", (req, res) => { res.send(data); }); +// Put new dictionary to the array of dictionaries +router.put('/new', (req, res) => { + if (!data || !Array.isArray(data)) { + return res.status(400).send('No array of dictionaries found`'); + } + + const updatedData = req.body; + + if (!updatedData) { + return res.status(400).send('No data to update'); // Bad request + } + + if (!data) { + return res.status(500).send('No data to update'); // Internal server error + } + + indexedUpdatedData = { id: data.length, ...updatedData }; // Add the new dictionary to the array + + data.push(indexedUpdatedData); // Add the new dictionary to the array + + fs.writeFile(path.join(__dirname, 'data/dictionaries.json'), JSON.stringify(data), (err) => { + if (err) { + console.error(err); // Log the error + return res.status(500).send('Error saving data'); + } + res.status(200).json(data); // Send back the updated data + }); +}); + +router.delete('/:id', (req, res) => { + const id = parseInt(req.params.id); // Get the dictionary id from the URL + + if (!id || isNaN(id)) { + return res.status(400).send('Invalid ID'); // Bad request + } + + const index = data.findIndex((dictionary) => dictionary.id === id); + + if (index < 0) { + return res.status(404).send('Not found'); // Not found + } + + data.splice(index, 1); // Remove the dictionary from the array + + fs.writeFile(path.join(__dirname, 'data/dictionaries.json'), JSON.stringify(data), (err) => { + if (err) { + console.error(err); // Log the error + return res.status(500).send('Error saving data'); + } + res.send({ message: `Dictionary with id ${id} deleted` }); + }); +}); + router.get("/:id", (req, res) => { const id = parseInt(req.params.id); const words = wordsData.find((word) => word.id === id); diff --git a/server/routers/kfu-m-24-1/eng-it-lean/index.js b/server/routers/kfu-m-24-1/eng-it-lean/index.js index 702db37..3d10acf 100644 --- a/server/routers/kfu-m-24-1/eng-it-lean/index.js +++ b/server/routers/kfu-m-24-1/eng-it-lean/index.js @@ -1,13 +1,15 @@ const router = require("express").Router(); const dictionariesRouter = require("./dictionaries"); +const unitsRouter = require('./units'); module.exports = router; const delay = - (ms = 1000) => + (ms = 250) => (req, res, next) => { setTimeout(next, ms); }; router.use(delay()); router.use("/dictionaries", dictionariesRouter); +router.use('/units', unitsRouter); diff --git a/server/routers/kfu-m-24-1/eng-it-lean/units/data/unit-1.md b/server/routers/kfu-m-24-1/eng-it-lean/units/data/unit-1.md new file mode 100644 index 0000000..82976dd --- /dev/null +++ b/server/routers/kfu-m-24-1/eng-it-lean/units/data/unit-1.md @@ -0,0 +1,71 @@ +# Unit 1. Multifunctional Verbs: Be, Have, and Do + +## Overview + +This unit focuses on the use of multifunctional verbs in English. These verbs are able to express multiple meanings depending on their use in a sentence. + +## Learning Objectives + +By the end of this unit, you will be able to: + +- Identify the different forms of the main multifunctional verb. +- Explain how these forms can be used interchangeably in sentences. +- Demonstrate the correct usage of the three forms of the multifunctional verb by providing sentences and examples. + +## Vocabulary Review + +| Term | Definition | +| ---- | -------------------------------------------------------- | +| Be | To express a present or ongoing state of being. | +| Have | To express ownership or possession. | +| Do | To express an action to be done, future action or habit. | + +## Activities + +### Activity 1: Identify the Different Forms of the Main Multifunctional Verb + +- Read through each sentence and identify if the verb is used in its present tense (is), past tense (was/were), or future tense (will, would). +- Discuss how this usage can vary depending on context. +- Write down sentences that use different forms to illustrate your points. + +1. **Sentence 1**: "The cat is sleeping." + + - Present tense: The cat is sleeping. + - Past tense: The cat slept. + - Future tense: The cat will sleep. + +2. **Sentence 2**: "I have a dog at home." + + - Present tense: I have a dog. + - Past tense: I had a dog. + - Future tense: I will have a dog. + +3. **Sentence 3**: "We are going on a hike tomorrow." + + - Present tense: We are going on a hike. + - Past tense: We went on a hike. + - Future tense: We will go on a hike. + +4. **Sentence 4**: "He has been studying all day." + + - Present tense: He is studying. + - Past tense: He studied. + - Future tense: He will study. + +5. **Sentence 5**: "We are going to buy some groceries later today." + - Present tense: We are going to buy some groceries. + - Past tense: We bought some groceries. + - Future tense: We will buy some groceries. + +### Activity 2: Explain How These Forms Can Be Used Interchangeably in Sentences + +- Read through a sentence and identify the present, past, and future tense uses. +- In pairs, explain why these forms are used interchangeably. +- Provide examples of sentences that demonstrate this usage. +- Highlight how the context changes the meaning. + +### Activity 3: Correct Usage of the Three Forms of the Multifunctional Verb + +- Read through a sentence and identify which form is being used. +- In pairs, discuss why these forms are used in certain situations. +- Provide sentences that demonstrate the correct usage of the three forms. diff --git a/server/routers/kfu-m-24-1/eng-it-lean/units/data/units.json b/server/routers/kfu-m-24-1/eng-it-lean/units/data/units.json new file mode 100644 index 0000000..88e5812 --- /dev/null +++ b/server/routers/kfu-m-24-1/eng-it-lean/units/data/units.json @@ -0,0 +1 @@ +[{"id":0,"filename":"unit-1","name":"Unit 1: Multifunctional Verbs: Be, Have, and Do"}] \ No newline at end of file diff --git a/server/routers/kfu-m-24-1/eng-it-lean/units/index.js b/server/routers/kfu-m-24-1/eng-it-lean/units/index.js new file mode 100644 index 0000000..8e131a5 --- /dev/null +++ b/server/routers/kfu-m-24-1/eng-it-lean/units/index.js @@ -0,0 +1,58 @@ +const fs = require('fs'); +const path = require('path'); +const router = require('express').Router(); + +module.exports = router; + +const data = require('./data/units.json'); +router.get('/', (req, res) => { + res.send(data); +}); + +router.put('/', (req, res) => { + const newUnit = req.body + + if (!newUnit) { + return res.status(400).send('No new unit to be added') + } + + if (!data) { + return res.status(500).send('No data to be updated') + } + + data.push({ "id": data.length, ...newUnit }) + + fs.writeFileSync(path.join(__dirname, 'data', 'units.json'), JSON.stringify(data)); + res.status(200).send(data); +}); + +router.delete('/:id', (req, res) => { + const id = parseInt(req.params.id); + const index = data.findIndex((unit) => unit.id === id); + + if (index < 0) { + return res.status(404).send('Not found'); + } + + data.splice(index, 1); + fs.writeFileSync(path.join(__dirname, 'data', 'units.json'), JSON.stringify(data)); + res.send({ message: `Unit with ID ${id} deleted` }); +}); + +router.get('/:id', (req, res) => { + const id = parseInt(req.params.id); + const unit = data.find((unit) => unit.id === id); + + if (!unit) { + return res.status(404).send('Not found'); + } + + const unitFilepath = path.join(__dirname, 'data', `${unit.filename}.md`); + const unitContent = fs.readFileSync(unitFilepath, 'utf-8'); + + if (!unitContent) { + return res.status(404).send('Not found'); + } + + res.send({ ...unit, content: unitContent }); +});