diff --git a/db/database.db b/db/database.db index bbb338d5fe2411c67c447e74eef5883dce9a017c..4f4f2cb1624566ed9e04c2f0fd6189b080a49089 100644 Binary files a/db/database.db and b/db/database.db differ diff --git a/index.js b/index.js index dc43e9d73dee0de6d77dc58d588a44064ea47d9b..973ac77fa78e3eb9a0cdd3f9cab7a9499892efba 100644 --- a/index.js +++ b/index.js @@ -7,7 +7,18 @@ import monkey from './src/models/Monkey.js'; import species from './src/models/Species.js'; import express from 'express'; import helloRoute from './src/routes/helloRoute.js'; - +import monkeyDeletteAll from './src/routes/deleteRoutes/monkeyDeleteAll.js'; +import monkeyDeleteById from './src/routes/deleteRoutes/monkeyDeleteById.js'; +import speciesDeleteAll from './src/routes/deleteRoutes/speciesDeleteAll.js'; +import speciesDeleteById from './src/routes/deleteRoutes/speciesDeleteById.js'; +import monkeyFindAll from './src/routes/findRoutes/monkeyFindAll.js'; +import monkeyFindById from './src/routes/findRoutes/monkeyFindById.js'; +import speciesFindAll from './src/routes/findRoutes/speciesFindAll.js'; +import speciesFindById from './src/routes/findRoutes/speciesFindById.js'; +import monkeyInsert from './src/routes/modifierRoute/insertMonkey.js'; +import speciesInsert from './src/routes/modifierRoute/insertSpecies.js'; +import monkeyUpdate from './src/routes/modifierRoute/updateMonkey.js'; +import speciesUpdate from './src/routes/modifierRoute/updateSpecies.js'; async function createDb() { if(fs.existsSync('./db/database.db') == false){ const db = await open({ @@ -29,7 +40,7 @@ async function openDb() { } async function main() { - //createDb(); + //await createDb(); var db = await openDb(); var DbFactory = new DAODbFactory(db); DbFactory.createMonkeyDAO(); @@ -53,10 +64,12 @@ async function main() { // console.log("ALL MONKEYS 3"); // console.log(allMonkeys); // DbFactory.MonkeyDbDAO.deleteAll(); - await DbFactory.MonkeyDbDAO.insert(new monkey('test',1)); const allMonkeys=await DbFactory.MonkeyDbDAO.findAll(); - console.log("ALL MONKEYS 4"); + const allSpecies=await DbFactory.SpeciesDbDAO.findAll(); + console.log("ALL MONKEYS"); console.log(allMonkeys); + console.log("ALL SPECIES"); + console.log(allSpecies); } const api=express(); @@ -64,6 +77,19 @@ const port=3001; api.use(express.json()); api.use('/hello',helloRoute); +api.use('/monkey/deleteAll',monkeyDeletteAll); +api.use('/monkey/deleteById',monkeyDeleteById); +api.use('/species/deleteAll',speciesDeleteAll); +api.use('/species/deleteById',speciesDeleteById); +api.use('/monkey/findAll',monkeyFindAll); +api.use('/species/findAll',speciesFindAll); +api.use('/monkey/findById',monkeyFindById); +api.use('/species/findById',speciesFindById); +api.use('/monkey/insert',monkeyInsert); +api.use('/species/insert',speciesInsert); +api.use('/monkey/update',monkeyUpdate); +api.use('/species/update',speciesUpdate); + api.listen(port, () => { console.log(`Server is running on port ${port}`); }); diff --git a/migrations/001-db.sql b/migrations/001-db.sql index 7c403e900023be513b9e9372b1dbcf2f5c624323..564dff709ea5ac7bf76848381245554592f31c03 100644 --- a/migrations/001-db.sql +++ b/migrations/001-db.sql @@ -13,22 +13,21 @@ CREATE TABLE species ( CREATE TABLE monkey ( id INTEGER PRIMARY KEY, name VARCHAR(50) NOT NULL, - species_id INT, + speciesId INT, sex VARCHAR(10), age INT, - birth_loc VARCHAR(50), - live_loc VARCHAR(50), - FOREIGN KEY (species_id) REFERENCES species(id) + birthLoc VARCHAR(50), + liveLoc VARCHAR(50), + FOREIGN KEY (speciesId) REFERENCES species(id) ); - -- Insertion des données dans la table 'species' INSERT INTO species (id, name, habitat, height, weight, diet) VALUES (1, 'Chimpanzee', 'Tropical Rainforest', 1.7, 70, 'Omnivore'), (2, 'Baboon', 'Savanna', 1.1, 40, 'Omnivore'); -- Insertion des données dans la table 'monkey' -INSERT INTO monkey (id, name, species_id, sex, age, birth_loc, live_loc) VALUES +INSERT INTO monkey (id, name, speciesId, sex, age, birthLoc, liveLoc) VALUES (1, 'Champ', 1, 'Male', 15, 'Congo Rainforest', 'Congo Rainforest'), (2, 'Babs', 2, 'Female', 20, 'Kenya', 'Kenya'); diff --git a/src/DAO/MonkeyDbDAO.js b/src/DAO/MonkeyDbDAO.js index 29a7624259e403a7613f2ff77027e99bfe55611a..9b5d1e7982fd55fc1eeb08ba62f5dcc32faac3cc 100644 --- a/src/DAO/MonkeyDbDAO.js +++ b/src/DAO/MonkeyDbDAO.js @@ -1,5 +1,6 @@ import IMonkeyDAO from "../interfaces/IMonkeyDAO.js"; - +import { open } from 'sqlite'; +import sqlite3 from 'sqlite3'; class MonkeyDbDAO extends IMonkeyDAO{ constructor(db){ super(); @@ -7,27 +8,51 @@ class MonkeyDbDAO extends IMonkeyDAO{ } async insert(monkey){ - await this.db.run('INSERT INTO monkey (name, species_id) VALUES (?,?)', [monkey.getName(), monkey.getSpeciesId()]); + this.db = await open({ + filename: './db/database.db', + driver: sqlite3.Database + }); + await this.db.run('INSERT INTO monkey (name, speciesId, sex, age, birthLoc, liveLoc) VALUES (?,?,?,?,?,?)', [monkey.name, monkey.speciesId, monkey.sex, monkey.age, monkey.birthLoc, monkey.liveLoc]); } async update(monkey){ - await this.db.run('UPDATE monkey SET name = ?, species_id = ? WHERE id = ?', [monkey.getName(), monkey.getSpeciesId(), monkey.getId()]); + console.log(monkey) + this.db = await open({ + filename: './db/database.db', + driver: sqlite3.Database + }); + await this.db.run('UPDATE monkey SET name = ?, speciesId = ?, sex=?, age = ?, birthLoc = ?, liveLoc = ? WHERE id = ?',[monkey.name, monkey.speciesId, monkey.sex, monkey.age, monkey.birthLoc, monkey.liveLoc, monkey.id]); } - - async delete(monkey){ - await this.db.run('DELETE FROM monkey WHERE id = ?', [monkey.getId()]); + async delete(id){ + this.db = await open({ + filename: './db/database.db', + driver: sqlite3.Database + }); + await this.db.run('DELETE FROM monkey WHERE id = ?', [id]); } async deleteAll(){ + this.db = await open({ + filename: './db/database.db', + driver: sqlite3.Database + }); await this.db.run('DELETE FROM monkey'); } async findAll(){ + this.db = await open({ + filename: './db/database.db', + driver: sqlite3.Database + }); const monkeys = await this.db.all('SELECT * FROM monkey'); return monkeys; } async findById(id){ + this.db = await open({ + filename: './db/database.db', + driver: sqlite3.Database + }); const monkey = await this.db.get('SELECT * FROM monkey WHERE id = ?', [id]); return monkey; } diff --git a/src/DAO/SpeciesDbDAO.js b/src/DAO/SpeciesDbDAO.js index 89ed6114b5eb7287da06cd8f242275899e21ffde..83db1c0d6ed4421e59336835f99d894fb1678834 100644 --- a/src/DAO/SpeciesDbDAO.js +++ b/src/DAO/SpeciesDbDAO.js @@ -1,29 +1,59 @@ import ISpeciesDAO from "../interfaces/ISpeciesDAO.js"; - +import { open } from 'sqlite'; +import sqlite3 from 'sqlite3'; class SpeciesDbDAO extends ISpeciesDAO{ constructor(db){ super(); this.db=db } async insert(species){ - + this.db = await open({ + filename: './db/database.db', + driver: sqlite3.Database + }); + await this.db.run('INSERT INTO species (name, habitat, height, weight, diet) VALUES (?, ?, ?, ?, ?)', [species.name, species.habitat, species.height, species.weight, species.diet]); } async update(species){ - + this.db = await open({ + filename: './db/database.db', + driver: sqlite3.Database + }); + await this.db.run('UPDATE species SET name = ?, habitat = ?, height = ?, weight = ?, diet = ? WHERE id = ?', [species.name, species.habitat, species.height, species.weight, species.diet, species.id]); } - async delete(species){ - + async delete(id){ + this.db = await open({ + filename: './db/database.db', + driver: sqlite3.Database + }); + await this.db.run('DELETE FROM species WHERE id = ?', [id]); } async findAll(){ + this.db = await open({ + filename: './db/database.db', + driver: sqlite3.Database + }); const species = await this.db.all('SELECT * FROM species'); return species; } - async findById(id){ + async deleteAll(){ + this.db = await open({ + filename: './db/database.db', + driver: sqlite3.Database + }); + await this.db.run('DELETE FROM species'); + } + async findById(id){ + this.db = await open({ + filename: './db/database.db', + driver: sqlite3.Database + }); + const species = await this.db.get('SELECT * FROM species WHERE id = ?', [id]); + return species; } } diff --git a/src/interfaces/IMonkeyDAO.js b/src/interfaces/IMonkeyDAO.js index 4ab069b9ef7bbc18cce6698565339d52d04db32a..86747386a7ff50c9796aa9d277f57ac18f8aacc4 100644 --- a/src/interfaces/IMonkeyDAO.js +++ b/src/interfaces/IMonkeyDAO.js @@ -18,6 +18,10 @@ export default class IMonkeyDbDAO { throw new Error('You have to implement the method delete!'); } + deleteAll() { + throw new Error('You have to implement the method deleteAll!'); + } + findAll() { throw new Error('You have to implement the method findAll!'); } diff --git a/src/interfaces/ISpeciesDAO.js b/src/interfaces/ISpeciesDAO.js index 121ab4f8a67234c5152cd2789ec1d20dfd6b79e4..e183c9387c0f5c28b413b853db83e0415d098b58 100644 --- a/src/interfaces/ISpeciesDAO.js +++ b/src/interfaces/ISpeciesDAO.js @@ -17,6 +17,10 @@ export default class ISpeciesDAO{ throw new Error('Method "delete(id)" must be implemented'); } + async deleteAll(){ + throw new Error('Method "deleteAll" must be implemented'); + } + async findall(){ throw new Error('Method "findall" must be implemented'); } diff --git a/src/routes/deleteRoutes/monkeyDeleteAll.js b/src/routes/deleteRoutes/monkeyDeleteAll.js new file mode 100644 index 0000000000000000000000000000000000000000..571738f588c9bb15f8cf70cdc28432df02f8a957 --- /dev/null +++ b/src/routes/deleteRoutes/monkeyDeleteAll.js @@ -0,0 +1,15 @@ +import express from 'express'; +import MonkeyDbDAO from '../../DAO/MonkeyDbDAO.js'; +const router = express.Router(); + +router.delete('/', async (req, res) => { + try{ + var monkeyDbDAO = new MonkeyDbDAO(); + await monkeyDbDAO.deleteAll(); + res.send('All species deleted'); + }catch(error){ + res.send(error.message); + } +}); + +export default router; diff --git a/src/routes/deleteRoutes/monkeyDeleteById.js b/src/routes/deleteRoutes/monkeyDeleteById.js new file mode 100644 index 0000000000000000000000000000000000000000..16ec41c379d45f020474f933cabfce20cdda2174 --- /dev/null +++ b/src/routes/deleteRoutes/monkeyDeleteById.js @@ -0,0 +1,18 @@ +import express from 'express'; +import MonkeyDbDAO from '../../DAO/MonkeyDbDAO.js'; +const router = express.Router(); + +router.delete('/:id', async (req, res) => { + try{ + const id = req.params.id; + console.log(id); + var monkeyDbDAO = new MonkeyDbDAO(); + await monkeyDbDAO.delete(id); + res.send('Monkey deleted'); + }catch(error){ + res.send(error.message); + } +}); + + +export default router; \ No newline at end of file diff --git a/src/routes/deleteRoutes/speciesDeleteAll.js b/src/routes/deleteRoutes/speciesDeleteAll.js new file mode 100644 index 0000000000000000000000000000000000000000..4a3309bf23fd335cdd3848c97e492129cae77052 --- /dev/null +++ b/src/routes/deleteRoutes/speciesDeleteAll.js @@ -0,0 +1,15 @@ +import express from 'express'; +import SpeciesDbDAO from '../../DAO/SpeciesDbDAO.js'; +const router = express.Router(); + +router.delete('/', (req, res) => { + try{ + var speciesDbDAO = new SpeciesDbDAO(); + speciesDbDAO.deleteAll(); + res.send('All species deleted'); + }catch(error){ + res.send(error.message); + } +}); + +export default router; diff --git a/src/routes/deleteRoutes/speciesDeleteById.js b/src/routes/deleteRoutes/speciesDeleteById.js new file mode 100644 index 0000000000000000000000000000000000000000..5d294b107ed89a1c80aee30fc0d939e974d2085a --- /dev/null +++ b/src/routes/deleteRoutes/speciesDeleteById.js @@ -0,0 +1,17 @@ +import express from 'express'; +import SpeciesDbDAO from '../../DAO/SpeciesDbDAO.js'; +const router = express.Router(); + +router.delete('/:id', async (req, res) => { + try{ + const id = req.params.id; + console.log(id); + var speciesDbDAO = new SpeciesDbDAO(); + await speciesDbDAO.delete(id); + res.send('species deleted'); + }catch(error){ + res.send(error.message); + } + }); + +export default router; \ No newline at end of file diff --git a/src/routes/findRoutes/monkeyFindAll.js b/src/routes/findRoutes/monkeyFindAll.js new file mode 100644 index 0000000000000000000000000000000000000000..5a27556ec69e209cdbbe8bc74d31cb0ebbb82c8f --- /dev/null +++ b/src/routes/findRoutes/monkeyFindAll.js @@ -0,0 +1,16 @@ +import express from 'express'; +import MonkeyDbDAO from '../../DAO/MonkeyDbDAO.js'; +const router = express.Router(); + +router.get('/', async (req, res) => { + try{ + var monkeyDbDAO = new MonkeyDbDAO(); + const result = await monkeyDbDAO.findAll(); + console.log("test"); + res.send(result); + }catch(error){ + res.send(error.message+"quoicoubeh"); + } +}); + +export default router; \ No newline at end of file diff --git a/src/routes/findRoutes/monkeyFindById.js b/src/routes/findRoutes/monkeyFindById.js new file mode 100644 index 0000000000000000000000000000000000000000..6cb34796429d2bc7c1bd8d1f3f969bd1b1b99854 --- /dev/null +++ b/src/routes/findRoutes/monkeyFindById.js @@ -0,0 +1,17 @@ +import express from 'express'; +import MonkeyDbDAO from '../../DAO/MonkeyDbDAO.js'; +const router = express.Router(); + +router.get('/:id', async (req, res) => { + try{ + const id = req.params.id; + console.log(id); + var monkeyDbDAO = new MonkeyDbDAO(); + const result=await monkeyDbDAO.findById(id); + res.send(result); + }catch(error){ + res.send(error.message); + } +}); + +export default router; \ No newline at end of file diff --git a/src/routes/findRoutes/speciesFindAll.js b/src/routes/findRoutes/speciesFindAll.js new file mode 100644 index 0000000000000000000000000000000000000000..16c83d221f0468c88f68e040c50f9e1b7e68e62a --- /dev/null +++ b/src/routes/findRoutes/speciesFindAll.js @@ -0,0 +1,15 @@ +import express from 'express'; +import SpeciesDbDAO from '../../DAO/SpeciesDbDAO.js'; +const router = express.Router(); + +router.get('/', async (req, res) => { + try{ + var speciesDbDAO = new SpeciesDbDAO(); + const result=await speciesDbDAO.findAll(); + res.send(result); + }catch(error){ + res.send(error.message); + } +}); + +export default router; \ No newline at end of file diff --git a/src/routes/findRoutes/speciesFindById.js b/src/routes/findRoutes/speciesFindById.js new file mode 100644 index 0000000000000000000000000000000000000000..b4f02f49e9305bb5ab6473111f6f11011b14427d --- /dev/null +++ b/src/routes/findRoutes/speciesFindById.js @@ -0,0 +1,17 @@ +import express from 'express'; +import SpeciesDbDAO from '../../DAO/SpeciesDbDAO.js'; +const router = express.Router(); + +router.get('/:id', async (req, res) => { + try{ + const id = req.params.id; + console.log(id); + var speciesDbDAO = new SpeciesDbDAO(); + const result=await speciesDbDAO.findById(id); + res.send(result); + }catch(error){ + res.send(error.message); + } +}); + +export default router; \ No newline at end of file diff --git a/src/routes/modifierRoute/insertMonkey.js b/src/routes/modifierRoute/insertMonkey.js new file mode 100644 index 0000000000000000000000000000000000000000..e4c40ced4103fcdb9101d38e2f43d17ed867da18 --- /dev/null +++ b/src/routes/modifierRoute/insertMonkey.js @@ -0,0 +1,17 @@ +import express from 'express'; +import MonkeyDbDAO from '../../DAO/MonkeyDbDAO.js'; +const router = express.Router(); + +router.post('/', async (req, res) => { + try{ + const monkey = req.body; + console.log(monkey); + var monkeyDbDAO = new MonkeyDbDAO(); + await monkeyDbDAO.insert(monkey); + res.send('Monkey inserted'); + }catch(error){ + res.send(error.message); + } +}); + +export default router; \ No newline at end of file diff --git a/src/routes/modifierRoute/insertSpecies.js b/src/routes/modifierRoute/insertSpecies.js new file mode 100644 index 0000000000000000000000000000000000000000..5afede74e1b5f4dc800838060abf7d5d59d864a8 --- /dev/null +++ b/src/routes/modifierRoute/insertSpecies.js @@ -0,0 +1,17 @@ +import express from 'express'; +import SpeciesDbDAO from '../../DAO/SpeciesDbDAO.js'; +const router = express.Router(); + +router.post('/', async (req, res) => { + try{ + console.log("test8") + const species = req.body; + var speciesDbDAO = new SpeciesDbDAO(); + await speciesDbDAO.insert(species); + res.send('Species inserted'); + }catch(error){ + res.send(error.message); + } +}); + +export default router; \ No newline at end of file diff --git a/src/routes/modifierRoute/updateMonkey.js b/src/routes/modifierRoute/updateMonkey.js new file mode 100644 index 0000000000000000000000000000000000000000..4acc08ef67b4b6e6453e3b5d157ca001bb90a08c --- /dev/null +++ b/src/routes/modifierRoute/updateMonkey.js @@ -0,0 +1,17 @@ +import express from 'express'; +import MonkeyDbDAO from '../../DAO/MonkeyDbDAO.js'; +const router = express.Router(); + +router.put('/', async (req, res) => { + try{ + const monkey = req.body; + console.log(monkey); + var monkeyDbDAO = new MonkeyDbDAO(); + await monkeyDbDAO.update(monkey); + res.send('Monkey updated'); + }catch(error){ + res.send(error.message); + } +}); + +export default router; diff --git a/src/routes/modifierRoute/updateSpecies.js b/src/routes/modifierRoute/updateSpecies.js new file mode 100644 index 0000000000000000000000000000000000000000..c23886214ddb205c490cb21d398f119c930a36a7 --- /dev/null +++ b/src/routes/modifierRoute/updateSpecies.js @@ -0,0 +1,16 @@ +import express from 'express'; +import SpeciesDbDAO from '../../DAO/SpeciesDbDAO.js'; +const router = express.Router(); + +router.put('/', async (req, res) => { + try{ + const species = req.body; + var speciesDbDAO = new SpeciesDbDAO(); + await speciesDbDAO.update(species); + res.send('Species updated'); + }catch(error){ + res.send(error.message); + } +}); + +export default router; \ No newline at end of file diff --git a/test_monkey_insert.json b/test_monkey_insert.json new file mode 100644 index 0000000000000000000000000000000000000000..f27b5019f54490d6ae2dd41d1b1294f774ed64c6 --- /dev/null +++ b/test_monkey_insert.json @@ -0,0 +1,47 @@ +[ + { + "id": 1, + "name": "George", + "species_id": 1, + "sex": "Male", + "age": 15, + "birth_loc": "Forêt tropicale", + "live_loc": "Zoo de Paris" + }, + { + "id": 2, + "name": "Lucy", + "species_id": 2, + "sex": "Female", + "age": 12, + "birth_loc": "Forêt tropicale", + "live_loc": "Zoo de Lyon" + }, + { + "id": 3, + "name": "Fred", + "species_id": 3, + "sex": "Male", + "age": 20, + "birth_loc": "Forêt tropicale", + "live_loc": "Zoo de Marseille" + }, + { + "id": 4, + "name": "Bella", + "species_id": 4, + "sex": "Female", + "age": 10, + "birth_loc": "Savane", + "live_loc": "Zoo de Lille" + }, + { + "id": 5, + "name": "Max", + "species_id": 5, + "sex": "Male", + "age": 8, + "birth_loc": "Forêt tropicale", + "live_loc": "Zoo de Strasbourg" + } +] diff --git a/test_species_insert.json b/test_species_insert.json new file mode 100644 index 0000000000000000000000000000000000000000..cad0cb4a6223fa5c8b7c71bc0eeedf5a33001284 --- /dev/null +++ b/test_species_insert.json @@ -0,0 +1,42 @@ +[ + { + "id": 1, + "name": "Gorille", + "habitat": "Forêt tropicale", + "height": 1.7, + "weight": 200.0, + "diet": "Omnivore" + }, + { + "id": 2, + "name": "Chimpanzé", + "habitat": "Forêt tropicale", + "height": 1.2, + "weight": 60.0, + "diet": "Omnivore" + }, + { + "id": 3, + "name": "Orang-outan", + "habitat": "Forêt tropicale", + "height": 1.4, + "weight": 90.0, + "diet": "Frugivore" + }, + { + "id": 4, + "name": "Babouin", + "habitat": "Savane", + "height": 0.9, + "weight": 30.0, + "diet": "Omnivore" + }, + { + "id": 5, + "name": "Capucin", + "habitat": "Forêt tropicale", + "height": 0.5, + "weight": 3.9, + "diet": "Omnivore" + } +] \ No newline at end of file