diff --git a/db/database.db b/db/database.db new file mode 100644 index 0000000000000000000000000000000000000000..bbb338d5fe2411c67c447e74eef5883dce9a017c Binary files /dev/null and b/db/database.db differ diff --git a/index.js b/index.js index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..dc43e9d73dee0de6d77dc58d588a44064ea47d9b 100644 --- a/index.js +++ b/index.js @@ -0,0 +1,75 @@ +import sqlite3 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 ff21cc2d2bbf525fbec435042308eefe38940b30..7c403e900023be513b9e9372b1dbcf2f5c624323 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 8b114d5146e2deea44157eec12d3d1cac9c45abe..29a7624259e403a7613f2ff77027e99bfe55611a 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 e88e0ec210dc0d18e23ddb20685ad1a010d1990b..89ed6114b5eb7287da06cd8f242275899e21ffde 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 46d199254b1161dd3c746be57cc3f48d5c07f72f..57058fd24f723ab7bde184be1e6bcb9753fbe3f9 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 7e0f53bfccfd357fa3da9081ff96ce52b061601a..110c057a6f0b5837368f67f67f7626b935dffedb 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 0000000000000000000000000000000000000000..4ab069b9ef7bbc18cce6698565339d52d04db32a --- /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 0000000000000000000000000000000000000000..121ab4f8a67234c5152cd2789ec1d20dfd6b79e4 --- /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 adbd528515ddb68905c9b798df6dbd5c1894405c..b1e23b7a5098fba049397924848e0cf20bc83f4b 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 a54bda4731a7f35e4b6fb97c1b14dd1948bd025d..8cf6679029b14e1cae281ae6d2d62b7bed48324c 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 0000000000000000000000000000000000000000..f63d11d5d408c600df6c288ed4fc7767338fb3bf --- /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;