Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions HW9(http)/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
17 changes: 17 additions & 0 deletions HW9(http)/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!DOCTYPE html>
<html>

<head>
<title>HTTP request methods</title>
<meta name='viewport' content='width=device-width, initial-scale=1'>
<link rel='stylesheet' href='./css/style.css'>

</head>

<body>

<h1>Your API</h1>
<script src='./js/app.js' type='module'></script>
</body>

</html>
11 changes: 11 additions & 0 deletions HW9(http)/js/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const express = require('express');

const app = express();
const fs = require('fs');

const routes = require('./routes.js')(app, fs);
const port = 5501;

const server = app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`);
});
21 changes: 21 additions & 0 deletions HW9(http)/js/controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const model = require('./model.js');
class Controller {
async getAllInFile() {
return await model.getAll();
}

async addInFile({ id, title }) {
return await model.addInto(id, title);
}

async updateItemInFile({ id, title }) {
return await model.updateItem(id, title);
}

async deleteItemInFile({ id }) {
return await model.deleteItem(id);
}
}

const controller = new Controller();
module.exports = controller;
18 changes: 18 additions & 0 deletions HW9(http)/js/db.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[
{
"id": "1",
"title": "Star Wars"
},
{
"id": "2",
"title": "Lord of the Rings"
},
{
"id": "3",
"title": "Matrix"
},
{
"id": "4",
"title": "Hobbit"
}
]
69 changes: 69 additions & 0 deletions HW9(http)/js/model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@

const fs = require('fs').promises;
class Model {
async getAll() {
const fileContentGet = await fs.readFile('./db.json', 'utf8', (err, data) => {
try {
fileContentGet = JSON.parse(data).films;
return ('Content is: ', fileContentGet.address);
} catch (err) {
return ('Error parsing JSON string: ', err);
}
});
return fileContentGet;
}
async addInto(id, title) {
const filmsList_add = await this.getAll();
const obj = JSON.parse(filmsList_add);

obj.push({
id: id,
title: title
});

await this.save(obj);

}

async updateItem(id, title) {
const filmsList_updata = await this.getAll();
const obj = JSON.parse(filmsList_updata);
const itemToUpdateIndex = await this.findIndexItemById(id);

const itemToUpdate = obj[itemToUpdateIndex];
itemToUpdate.title = title;
obj[itemToUpdateIndex] = itemToUpdate;

await this.save(obj);
}

async deleteItem(id) {
const filmsList_delete = await this.getAll();
const obj = JSON.parse(filmsList_delete);
const index = await this.findIndexItemById(id);

obj.splice(index, 1);
await this.save(obj);
}

async findIndexItemById(id) {
const filmsList = await this.getAll();
const obj = JSON.parse(filmsList);
const item = obj.find(item => item.id === id);
return obj.indexOf(item);
}

async save(obj) {
const jsonStringify = JSON.stringify(obj, null, 2);
await fs.writeFile('./db.json', jsonStringify, err => {
if (err) {
return ('Error writing file', err);
} else {
return ('Successfully wrote file!');
}
});
}
}

const model = new Model();
module.exports = model;
64 changes: 64 additions & 0 deletions HW9(http)/js/routes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
const model = require('./model.js');
const controller = require('./controller.js');

const appRouter = (app, fs) => {
// default route
app.get('/', (req, res) => {
res.send('welcome to the development api-server');
});
// // other routes
filmRoutes(app, fs);
};

const filmRoutes = (app, fs) => {
// READ
//localhost:5501/films
app.get('/films', async (req, res) => {

const result = await controller.getAllInFile();
if (result === 0) {
res.status(204).json({ message: `Error 204. No Content` });
}
else {
res.status(200).send(result);
}
});

// CREATE
//localhost:5501/films?id=5&title=The+Great+Gatsby
app.post('/films', async (req, res) => {
const result = await controller.addInFile(req.query);
if (result === 0) {
res.status(204).json({ message: `Error 204. No Content` });
}
else {
res.status(200).send(result);
}
});

// UPDATE
//localhost:5501/films?id=4&title=Pulp+Fiction
app.put('/films', async (req, res) => {
console.log('PUT');
const result = await controller.updateItemInFile(req.query);
if (result === 0) {
res.status(204).json({ message: `Error 204. No Content` });
}
else {
res.status(200).send(result);
}
});

// DELETE
//localhost:5501/films?id=5
app.delete('/films', async (req, res) => {
const result = await controller.deleteItemInFile(req.query);
if (result === 0) {
res.status(204).json({ message: `Error 204. No Content` });
}
else {
res.status(200).send(result);
}
});
}
module.exports = appRouter;
4 changes: 4 additions & 0 deletions HW9(http)/js/view.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class View {


}
Loading