Skip to content

Commit 9740fda

Browse files
Update app.js
1 parent 0334dcb commit 9740fda

File tree

1 file changed

+61
-107
lines changed

1 file changed

+61
-107
lines changed

4-network/app.js

Lines changed: 61 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -1,115 +1,81 @@
11
const express = require('express');
22
const bodyParser = require('body-parser');
3-
const axios = require('axios').default;
4-
const mongoose = require('mongoose');
5-
6-
const Favorite = require('./models/favorite');
7-
const Character = require('./models/character');
8-
const { application } = require('express');
3+
const mysql = require('mysql');
4+
const axios = require('axios');
95

106
const app = express();
7+
const port = 3000;
118

129
app.use(bodyParser.json());
1310

14-
app.get('/favorites', async (req, res) => {
15-
try {
16-
const favorites = await Favorite.find();
17-
res.status(200).json({
18-
favorites: favorites,
19-
});
20-
} catch (error) {
21-
res.status(500).json({ message: 'Something went wrong.' });
22-
}
11+
// MySQL connection setup
12+
const connection = mysql.createConnection({
13+
host: process.env.DB_HOST || '172.17.0.1', //ovde staviti svoju IP adresu
14+
user: 'novi',
15+
password: 'novi',
16+
database: 'swfavorites'
2317
});
2418

25-
app.post('/favorites', async (req, res) => {
26-
const favType = req.body.type;
27-
const favName = req.body.name;
28-
const favUrl = req.body.url;
19+
connection.connect(err => {
20+
if (err) throw err;
21+
console.log('Connected to the database.');
22+
23+
const createTableSql = `
24+
CREATE TABLE IF NOT EXISTS favorites (
25+
id INT AUTO_INCREMENT PRIMARY KEY,
26+
name VARCHAR(100) NOT NULL,
27+
type VARCHAR(100) NOT NULL,
28+
url VARCHAR(100) NOT NULL
29+
)
30+
`;
31+
32+
connection.query(createTableSql, (err, result) => {
33+
if (err) throw err;
34+
console.log("Table 'favorites' is ready.");
35+
});
36+
});
2937

30-
try {
31-
if (favType !== 'episode' && favType !== 'character') {
32-
throw new Error('"type" should be "episode" or "character"!');
33-
}
34-
const existingFav = await Favorite.findOne({ name: favName });
35-
if (existingFav) {
36-
throw new Error('Favorite exists already!');
37-
}
38-
} catch (error) {
39-
return res.status(500).json({ message: error.message });
40-
}
4138

42-
const favorite = new Favorite({
43-
name: favName,
44-
type: favType,
45-
url: favUrl,
39+
// Endpoint to get all favorites
40+
app.get('/favorites', (req, res) => {
41+
connection.query('SELECT * FROM favorites', (error, results) => {
42+
if (error) throw error;
43+
res.json(results);
4644
});
47-
48-
try {
49-
await favorite.save();
50-
res
51-
.status(201)
52-
.json({ message: 'Favorite saved!', favorite: favorite.toObject() });
53-
} catch (error) {
54-
res.status(500).json({ message: 'Something went wrong.' });
55-
}
5645
});
5746

58-
59-
app.post('/characters', async (req, res) => {
60-
const charName = req.body.name;
61-
const charStatus = req.body.status;
62-
const charSpecies = req.body.species;
63-
const charGender = req.body.gender;
64-
const charOrigin = req.body.origin;
65-
const charLocation = req.body.location;
66-
const charImage = req.body.image;
67-
const charCreated = req.body.created;
68-
const charUrl = req.body.url;
69-
70-
try {
71-
// Provera postojanja lika
72-
const existingChar = await Character.findOne({ name: charName });
73-
if (existingChar) {
74-
throw new Error('Character exists already!');
75-
}
76-
} catch (error) {
77-
return res.status(500).json({ message: error.message });
78-
}
79-
80-
// Kreiranje novog lika
81-
const character = new Character({
82-
name: charName,
83-
status: charStatus,
84-
species: charSpecies,
85-
gender: charGender,
86-
origin: charOrigin,
87-
location: charLocation,
88-
image: charImage,
89-
created: charCreated,
90-
url: charUrl,
47+
// Endpoint to add a favorite
48+
app.post('/favorites', (req, res) => {
49+
const { name, type, url } = req.body;
50+
const query = 'INSERT INTO favorites (name, type, url) VALUES (?, ?, ?)';
51+
connection.query(query, [name, type, url], (error, results) => {
52+
if (error) throw error;
53+
res.status(201).send(`Favorite added with ID: ${results.insertId}`);
9154
});
55+
});
9256

93-
try {
94-
// Čuvanje lika u bazi podataka
95-
await character.save();
96-
res
97-
.status(201)
98-
.json({ message: 'Character saved!', character: character.toObject() });
99-
} catch (error) {
100-
res.status(500).json({ message: 'Something went wrong.' });
101-
}
57+
// Endpoint to delete a favorite
58+
app.delete('/favorites/:id', (req, res) => {
59+
const { id } = req.params;
60+
const query = 'DELETE FROM favorites WHERE id = ?';
61+
connection.query(query, [id], (error, results) => {
62+
if (error) throw error;
63+
res.send(`Favorite with ID ${id} deleted.`);
64+
});
10265
});
10366

104-
app.get('/characters', async (req, res) => {
105-
try {
106-
const response = await axios.get('https://rickandmortyapi.com/api/character');
107-
res.status(200).json({ characters: response.data });
108-
} catch (error) {
109-
res.status(500).json({ message: 'Something went wrong.' });
110-
}
67+
// Endpoint to update a favorite
68+
app.put('/favorites/:id', (req, res) => {
69+
const { id } = req.params;
70+
const { name, type, url } = req.body;
71+
const query = 'UPDATE favorites SET name = ?, type = ?, url = ? WHERE id = ?';
72+
connection.query(query, [name, type, url, id], (error, results) => {
73+
if (error) throw error;
74+
res.send(`Favorite with ID ${id} updated.`);
75+
});
11176
});
11277

78+
// Endpoint to fetch episodes from an external API
11379
app.get('/episodes', async (req, res) => {
11480
try {
11581
const response = await axios.get('https://rickandmortyapi.com/api/episode');
@@ -119,18 +85,6 @@ app.get('/episodes', async (req, res) => {
11985
}
12086
});
12187

122-
mongoose.connect(
123-
'mongodb://mongodb:27017/swfavorites',
124-
{ useNewUrlParser: true },
125-
(err) => {
126-
if (err) {
127-
console.log(err);
128-
} else {
129-
app.listen(3000);
130-
}
131-
}
132-
);
133-
134-
135-
136-
88+
app.listen(port, () => {
89+
console.log(`Server running on port ${port}`);
90+
});

0 commit comments

Comments
 (0)