From df17374f8389ec574d15d4bacf5b053f0827c2d5 Mon Sep 17 00:00:00 2001 From: Valentin <valentin.henriques@etu.univ-amu.fr> Date: Sat, 9 Dec 2023 16:07:06 +0100 Subject: [PATCH] =?UTF-8?q?Cr=C3=A9ation=20bdd=20&=20DAO?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- db/database.db | Bin 0 -> 16384 bytes index.js | 75 ++++++++++++++++++ .../migration.sql => migrations/001-db.sql | 17 +++- src/DAO/MonkeyDbDAO.js | 33 +++++--- src/DAO/SpeciesDbDAO.js | 22 +++-- src/factory/DAODbFactory.js | 26 +++--- src/factory/DAOFactory.js | 2 - src/interfaces/IMonkeyDAO.js | 28 +++++++ src/interfaces/ISpeciesDAO.js | 27 +++++++ src/models/monkey.js | 5 +- src/models/species.js | 11 +-- src/routes/helloRoute.js | 8 ++ 12 files changed, 207 insertions(+), 47 deletions(-) create mode 100644 db/database.db rename migrations.sql/migration.sql => migrations/001-db.sql (56%) create mode 100644 src/interfaces/IMonkeyDAO.js create mode 100644 src/interfaces/ISpeciesDAO.js create mode 100644 src/routes/helloRoute.js diff --git a/db/database.db b/db/database.db new file mode 100644 index 0000000000000000000000000000000000000000..bbb338d5fe2411c67c447e74eef5883dce9a017c GIT binary patch literal 16384 zcmWFz^vNtqRY=P(%1ta$FlG>7U}R))P*7lCU=U(pU|?ZD0A@5kBSH!%ONc=)?IkY* z0|PT}B?EsXUq5dpPPL;FqaiRF0;3@?8UmvsFd71*Aut*OqaiRF0;3@?JVT&Kn3-K% zQj)QSu_Q4mCp9-eFFUmoLb5mqxjKfpDug&X`M4@T#1u4mxfB!>6f#p3JpDpk-Ccte z0)jk!9fKkjyj>%8Ad-2Bxv2_ajzP{IjzJox2AT?f{vir}p*}uP`Qn1q<jmCKc!(h| z!PE-49zz37sAyt(D##)zFDbLABqKg2KN+sl6s|HSvn&;dl$(E$tEamk$b||T2+K7U zf?VBPgIxWbU4s>%!Wx+=n!H?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from 'sqlite3' +import { open } from 'sqlite' +import fs from 'fs'; +import DAODbFactory from './src/factory/DAODbFactory.js'; +import MonkeyDbDAO from './src/DAO/MonkeyDbDAO.js'; +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'; + +async function createDb() { + if(fs.existsSync('./db/database.db') == false){ + const db = await open({ + filename: './db/database.db', + driver: sqlite3.Database, + }); + await db.migrate(); + }else{ + throw new Error('Database already exists'); + } +} + +async function openDb() { + const db = await open({ + filename: './db/database.db', + driver: sqlite3.Database, + }); + return db; +} + +async function main() { + //createDb(); + var db = await openDb(); + var DbFactory = new DAODbFactory(db); + DbFactory.createMonkeyDAO(); + DbFactory.createSpeciesDAO(); + // var allMonkeys=await DbFactory.MonkeyDbDAO.findAll(); + // console.log("ALL MONKEYS"); + // console.log(allMonkeys); + // var allSpecies=await DbFactory.SpeciesDbDAO.findAll(); + // console.log("ALL SPECIES"); + // console.log(allSpecies); + // var amonkey= new monkey('test',1); + // await DbFactory.MonkeyDbDAO.insert(amonkey); + // allMonkeys=await DbFactory.MonkeyDbDAO.findAll(); + // console.log("ALL MONKEYS 2"); + // console.log(allMonkeys); + // var idMonkey=await DbFactory.MonkeyDbDAO.findById(6); + // console.log("ID MONKEY"); + // console.log(idMonkey); + // DbFactory.MonkeyDbDAO.delete(amonkey); + // allMonkeys=await DbFactory.MonkeyDbDAO.findAll(); + // 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"); + console.log(allMonkeys); +} + +const api=express(); +const port=3001; +api.use(express.json()); + +api.use('/hello',helloRoute); +api.listen(port, () => { + console.log(`Server is running on port ${port}`); +}); + + + +main(); + + diff --git a/migrations.sql/migration.sql b/migrations/001-db.sql similarity index 56% rename from migrations.sql/migration.sql rename to migrations/001-db.sql index ff21cc2..7c403e9 100644 --- a/migrations.sql/migration.sql +++ b/migrations/001-db.sql @@ -2,7 +2,7 @@ -- Up -------------------------------------------------------------------------------- CREATE TABLE species ( - id INT PRIMARY KEY AUTO_INCREMENT, + id INTEGER PRIMARY KEY, name VARCHAR(50) NOT NULL, habitat VARCHAR(50), height FLOAT, @@ -11,7 +11,7 @@ CREATE TABLE species ( ); CREATE TABLE monkey ( - id INT PRIMARY KEY AUTO_INCREMENT, + id INTEGER PRIMARY KEY, name VARCHAR(50) NOT NULL, species_id INT, sex VARCHAR(10), @@ -21,8 +21,19 @@ CREATE TABLE monkey ( FOREIGN KEY (species_id) 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 +(1, 'Champ', 1, 'Male', 15, 'Congo Rainforest', 'Congo Rainforest'), +(2, 'Babs', 2, 'Female', 20, 'Kenya', 'Kenya'); + -------------------------------------------------------------------------------- -- Down -------------------------------------------------------------------------------- +DROP TABLE species; DROP TABLE monkey; -DROP TABLE species; \ No newline at end of file diff --git a/src/DAO/MonkeyDbDAO.js b/src/DAO/MonkeyDbDAO.js index 8b114d5..29a7624 100644 --- a/src/DAO/MonkeyDbDAO.js +++ b/src/DAO/MonkeyDbDAO.js @@ -1,25 +1,36 @@ -class MonkeyDbDAO{ +import IMonkeyDAO from "../interfaces/IMonkeyDAO.js"; + +class MonkeyDbDAO extends IMonkeyDAO{ constructor(db){ + super(); this.db=db; } - insert(monkey){ - + async insert(monkey){ + await this.db.run('INSERT INTO monkey (name, species_id) VALUES (?,?)', [monkey.getName(), monkey.getSpeciesId()]); } - update(monkey){ - + async update(monkey){ + await this.db.run('UPDATE monkey SET name = ?, species_id = ? WHERE id = ?', [monkey.getName(), monkey.getSpeciesId(), monkey.getId()]); } - delete(monkey){ - + async delete(monkey){ + await this.db.run('DELETE FROM monkey WHERE id = ?', [monkey.getId()]); } - findAll(){ - + async deleteAll(){ + await this.db.run('DELETE FROM monkey'); } - findById(id){ + async findAll(){ + const monkeys = await this.db.all('SELECT * FROM monkey'); + return monkeys; + } + async findById(id){ + const monkey = await this.db.get('SELECT * FROM monkey WHERE id = ?', [id]); + return monkey; } -} \ No newline at end of file +} + +export default MonkeyDbDAO; \ No newline at end of file diff --git a/src/DAO/SpeciesDbDAO.js b/src/DAO/SpeciesDbDAO.js index e88e0ec..89ed611 100644 --- a/src/DAO/SpeciesDbDAO.js +++ b/src/DAO/SpeciesDbDAO.js @@ -1,24 +1,30 @@ -class SpeciesDbDAO{ +import ISpeciesDAO from "../interfaces/ISpeciesDAO.js"; + +class SpeciesDbDAO extends ISpeciesDAO{ constructor(db){ + super(); this.db=db } - insert(species){ + async insert(species){ } - update(species){ + async update(species){ } - delete(species){ + async delete(species){ } - findAll(){ - + async findAll(){ + const species = await this.db.all('SELECT * FROM species'); + return species; } - findById(id){ + async findById(id){ } -} \ No newline at end of file +} + +export default SpeciesDbDAO; \ No newline at end of file diff --git a/src/factory/DAODbFactory.js b/src/factory/DAODbFactory.js index 46d1992..57058fd 100644 --- a/src/factory/DAODbFactory.js +++ b/src/factory/DAODbFactory.js @@ -1,24 +1,24 @@ import MonkeyDbDAO from "../DAO/MonkeyDbDAO.js"; import SpeciesDbDAO from "../DAO/SpeciesDbDAO.js"; -class DAODbFactory { - constructor() { - this.db = null; +export default class DAODbFactory { + constructor(db) { + this.db = db; } - createMonkeyDAO() { - // Code pour créer et retourner une instance de MonkeyDAO - - return MonkeyDbDAO + async createMonkeyDAO() { + this.MonkeyDbDAO= new MonkeyDbDAO(this.db); } - createSpeciesDAO() { - // Code pour créer et retourner une instance de TimingDAO - - return SpeciesDbDAO + async createSpeciesDAO() { + this.SpeciesDbDAO = new SpeciesDbDAO(this.db); } - getConnection() { - // Code pour établir une connexion à la base de données et la retourner + async getConnection() { + const db = await open({ + filename: './db/database.db', + driver: sqlite3.Database, + }); + return db; } } diff --git a/src/factory/DAOFactory.js b/src/factory/DAOFactory.js index 7e0f53b..110c057 100644 --- a/src/factory/DAOFactory.js +++ b/src/factory/DAOFactory.js @@ -3,11 +3,9 @@ import SpeciesDbDAO from "../DAO/SpeciesDbDAO.js"; class DAOFactory { createMonkeyDAO(){ - return new MonkeyDAO(this.getConnection()) } createSpeciesDAO(){ - return new SpeciesDAO(this.getConnection()) } } \ No newline at end of file diff --git a/src/interfaces/IMonkeyDAO.js b/src/interfaces/IMonkeyDAO.js new file mode 100644 index 0000000..4ab069b --- /dev/null +++ b/src/interfaces/IMonkeyDAO.js @@ -0,0 +1,28 @@ +export default class IMonkeyDbDAO { + constructor(db) { + if (this.constructor === IMonkeyDbDAO) { + throw new TypeError('Abstract class "IMonkeyDbDAO" cannot be instantiated directly.'); + } + this.db = db; + } + + insert(monkey) { + throw new Error('You have to implement the method insert!'); + } + + update(monkey) { + throw new Error('You have to implement the method update!'); + } + + delete(monkey) { + throw new Error('You have to implement the method delete!'); + } + + findAll() { + throw new Error('You have to implement the method findAll!'); + } + + findById(id) { + throw new Error('You have to implement the method findById!'); + } +} diff --git a/src/interfaces/ISpeciesDAO.js b/src/interfaces/ISpeciesDAO.js new file mode 100644 index 0000000..121ab4f --- /dev/null +++ b/src/interfaces/ISpeciesDAO.js @@ -0,0 +1,27 @@ +export default class ISpeciesDAO{ + constructor(){ + if(this.constructor === ISpeciesDAO){ + throw new TypeError('Abstract class "ISpeciesDAO" cannot be instantiated directly'); + } + } + + async insert(species){ + throw new Error('Method "create(species)" must be implemented'); + } + + async update(species){ + throw new Error('Method "update(species)" must be implemented'); + } + + async delete(id){ + throw new Error('Method "delete(id)" must be implemented'); + } + + async findall(){ + throw new Error('Method "findall" must be implemented'); + } + + findById(id){ + throw new Error('Method "findById(id)" must be implemented'); + } +} \ No newline at end of file diff --git a/src/models/monkey.js b/src/models/monkey.js index adbd528..b1e23b7 100644 --- a/src/models/monkey.js +++ b/src/models/monkey.js @@ -1,4 +1,4 @@ -class Monkey { +export default class Monkey { constructor(id, name, speciesId, sex, age, birthLoc, liveLoc) { this.id = id; this.name = name; @@ -36,4 +36,5 @@ class Monkey { getLiveLoc() { return this.liveLoc; } -} \ No newline at end of file +} + diff --git a/src/models/species.js b/src/models/species.js index a54bda4..8cf6679 100644 --- a/src/models/species.js +++ b/src/models/species.js @@ -1,12 +1,11 @@ -class Species { - constructor(id, name, habitat, height, weight, diet) { - this.id = id; +export default class Species { + constructor(name, habitat, height, weight, diet) { + this.id = null; this.name = name; this.habitat = habitat; this.height = height; this.weight = weight; this.diet = diet; - this.monkeys = []; } getId() { @@ -33,10 +32,6 @@ class Species { return this.diet; } - addMonkey(monkey) { - this.monkeys.push(monkey); - } - getMonkeys() { return this.monkeys; } diff --git a/src/routes/helloRoute.js b/src/routes/helloRoute.js new file mode 100644 index 0000000..f63d11d --- /dev/null +++ b/src/routes/helloRoute.js @@ -0,0 +1,8 @@ +import express from 'express'; +const router = express.Router(); + +router.get('/', (req, res) => { + res.send('Hello, World!'); +}); + +export default router; -- GitLab