Skip to content
Snippets Groups Projects
Commit df17374f authored by HENRIQUES Valentin's avatar HENRIQUES Valentin
Browse files

Création bdd & DAO

parent ab6932c1
Branches
No related tags found
No related merge requests found
File added
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();
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
-- Up -- Up
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
CREATE TABLE species ( CREATE TABLE species (
id INT PRIMARY KEY AUTO_INCREMENT, id INTEGER PRIMARY KEY,
name VARCHAR(50) NOT NULL, name VARCHAR(50) NOT NULL,
habitat VARCHAR(50), habitat VARCHAR(50),
height FLOAT, height FLOAT,
...@@ -11,7 +11,7 @@ CREATE TABLE species ( ...@@ -11,7 +11,7 @@ CREATE TABLE species (
); );
CREATE TABLE monkey ( CREATE TABLE monkey (
id INT PRIMARY KEY AUTO_INCREMENT, id INTEGER PRIMARY KEY,
name VARCHAR(50) NOT NULL, name VARCHAR(50) NOT NULL,
species_id INT, species_id INT,
sex VARCHAR(10), sex VARCHAR(10),
...@@ -21,8 +21,19 @@ CREATE TABLE monkey ( ...@@ -21,8 +21,19 @@ CREATE TABLE monkey (
FOREIGN KEY (species_id) REFERENCES species(id) 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 -- Down
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
DROP TABLE monkey;
DROP TABLE species; DROP TABLE species;
DROP TABLE monkey;
class MonkeyDbDAO{ import IMonkeyDAO from "../interfaces/IMonkeyDAO.js";
class MonkeyDbDAO extends IMonkeyDAO{
constructor(db){ constructor(db){
super();
this.db=db; 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;
} }
} }
export default MonkeyDbDAO;
\ No newline at end of file
class SpeciesDbDAO{ import ISpeciesDAO from "../interfaces/ISpeciesDAO.js";
class SpeciesDbDAO extends ISpeciesDAO{
constructor(db){ constructor(db){
super();
this.db=db 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){
} }
} }
export default SpeciesDbDAO;
\ No newline at end of file
import MonkeyDbDAO from "../DAO/MonkeyDbDAO.js"; import MonkeyDbDAO from "../DAO/MonkeyDbDAO.js";
import SpeciesDbDAO from "../DAO/SpeciesDbDAO.js"; import SpeciesDbDAO from "../DAO/SpeciesDbDAO.js";
class DAODbFactory { export default class DAODbFactory {
constructor() { constructor(db) {
this.db = null; this.db = db;
} }
createMonkeyDAO() { async createMonkeyDAO() {
// Code pour créer et retourner une instance de MonkeyDAO this.MonkeyDbDAO= new MonkeyDbDAO(this.db);
return MonkeyDbDAO
} }
createSpeciesDAO() { async createSpeciesDAO() {
// Code pour créer et retourner une instance de TimingDAO this.SpeciesDbDAO = new SpeciesDbDAO(this.db);
return SpeciesDbDAO
} }
getConnection() { async getConnection() {
// Code pour établir une connexion à la base de données et la retourner const db = await open({
filename: './db/database.db',
driver: sqlite3.Database,
});
return db;
} }
} }
...@@ -3,11 +3,9 @@ import SpeciesDbDAO from "../DAO/SpeciesDbDAO.js"; ...@@ -3,11 +3,9 @@ import SpeciesDbDAO from "../DAO/SpeciesDbDAO.js";
class DAOFactory { class DAOFactory {
createMonkeyDAO(){ createMonkeyDAO(){
return new MonkeyDAO(this.getConnection())
} }
createSpeciesDAO(){ createSpeciesDAO(){
return new SpeciesDAO(this.getConnection())
} }
} }
\ No newline at end of file
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!');
}
}
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
class Monkey { export default class Monkey {
constructor(id, name, speciesId, sex, age, birthLoc, liveLoc) { constructor(id, name, speciesId, sex, age, birthLoc, liveLoc) {
this.id = id; this.id = id;
this.name = name; this.name = name;
...@@ -37,3 +37,4 @@ class Monkey { ...@@ -37,3 +37,4 @@ class Monkey {
return this.liveLoc; return this.liveLoc;
} }
} }
class Species { export default class Species {
constructor(id, name, habitat, height, weight, diet) { constructor(name, habitat, height, weight, diet) {
this.id = id; this.id = null;
this.name = name; this.name = name;
this.habitat = habitat; this.habitat = habitat;
this.height = height; this.height = height;
this.weight = weight; this.weight = weight;
this.diet = diet; this.diet = diet;
this.monkeys = [];
} }
getId() { getId() {
...@@ -33,10 +32,6 @@ class Species { ...@@ -33,10 +32,6 @@ class Species {
return this.diet; return this.diet;
} }
addMonkey(monkey) {
this.monkeys.push(monkey);
}
getMonkeys() { getMonkeys() {
return this.monkeys; return this.monkeys;
} }
......
import express from 'express';
const router = express.Router();
router.get('/', (req, res) => {
res.send('Hello, World!');
});
export default router;
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment