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

Création toutes les routes fonctionnelles

parent df17374f
No related branches found
No related tags found
No related merge requests found
Showing
with 354 additions and 22 deletions
No preview for this file type
...@@ -7,7 +7,18 @@ import monkey from './src/models/Monkey.js'; ...@@ -7,7 +7,18 @@ import monkey from './src/models/Monkey.js';
import species from './src/models/Species.js'; import species from './src/models/Species.js';
import express from 'express'; import express from 'express';
import helloRoute from './src/routes/helloRoute.js'; 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() { async function createDb() {
if(fs.existsSync('./db/database.db') == false){ if(fs.existsSync('./db/database.db') == false){
const db = await open({ const db = await open({
...@@ -29,7 +40,7 @@ async function openDb() { ...@@ -29,7 +40,7 @@ async function openDb() {
} }
async function main() { async function main() {
//createDb(); //await createDb();
var db = await openDb(); var db = await openDb();
var DbFactory = new DAODbFactory(db); var DbFactory = new DAODbFactory(db);
DbFactory.createMonkeyDAO(); DbFactory.createMonkeyDAO();
...@@ -53,10 +64,12 @@ async function main() { ...@@ -53,10 +64,12 @@ async function main() {
// console.log("ALL MONKEYS 3"); // console.log("ALL MONKEYS 3");
// console.log(allMonkeys); // console.log(allMonkeys);
// DbFactory.MonkeyDbDAO.deleteAll(); // DbFactory.MonkeyDbDAO.deleteAll();
await DbFactory.MonkeyDbDAO.insert(new monkey('test',1));
const allMonkeys=await DbFactory.MonkeyDbDAO.findAll(); 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(allMonkeys);
console.log("ALL SPECIES");
console.log(allSpecies);
} }
const api=express(); const api=express();
...@@ -64,6 +77,19 @@ const port=3001; ...@@ -64,6 +77,19 @@ const port=3001;
api.use(express.json()); api.use(express.json());
api.use('/hello',helloRoute); 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, () => { api.listen(port, () => {
console.log(`Server is running on port ${port}`); console.log(`Server is running on port ${port}`);
}); });
......
...@@ -13,22 +13,21 @@ CREATE TABLE species ( ...@@ -13,22 +13,21 @@ CREATE TABLE species (
CREATE TABLE monkey ( CREATE TABLE monkey (
id INTEGER PRIMARY KEY, id INTEGER PRIMARY KEY,
name VARCHAR(50) NOT NULL, name VARCHAR(50) NOT NULL,
species_id INT, speciesId INT,
sex VARCHAR(10), sex VARCHAR(10),
age INT, age INT,
birth_loc VARCHAR(50), birthLoc VARCHAR(50),
live_loc VARCHAR(50), liveLoc VARCHAR(50),
FOREIGN KEY (species_id) REFERENCES species(id) FOREIGN KEY (speciesId) REFERENCES species(id)
); );
-- Insertion des données dans la table 'species' -- Insertion des données dans la table 'species'
INSERT INTO species (id, name, habitat, height, weight, diet) VALUES INSERT INTO species (id, name, habitat, height, weight, diet) VALUES
(1, 'Chimpanzee', 'Tropical Rainforest', 1.7, 70, 'Omnivore'), (1, 'Chimpanzee', 'Tropical Rainforest', 1.7, 70, 'Omnivore'),
(2, 'Baboon', 'Savanna', 1.1, 40, 'Omnivore'); (2, 'Baboon', 'Savanna', 1.1, 40, 'Omnivore');
-- Insertion des données dans la table 'monkey' -- 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'), (1, 'Champ', 1, 'Male', 15, 'Congo Rainforest', 'Congo Rainforest'),
(2, 'Babs', 2, 'Female', 20, 'Kenya', 'Kenya'); (2, 'Babs', 2, 'Female', 20, 'Kenya', 'Kenya');
......
import IMonkeyDAO from "../interfaces/IMonkeyDAO.js"; import IMonkeyDAO from "../interfaces/IMonkeyDAO.js";
import { open } from 'sqlite';
import sqlite3 from 'sqlite3';
class MonkeyDbDAO extends IMonkeyDAO{ class MonkeyDbDAO extends IMonkeyDAO{
constructor(db){ constructor(db){
super(); super();
...@@ -7,27 +8,51 @@ class MonkeyDbDAO extends IMonkeyDAO{ ...@@ -7,27 +8,51 @@ class MonkeyDbDAO extends IMonkeyDAO{
} }
async insert(monkey){ 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){ 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(id){
async delete(monkey){ this.db = await open({
await this.db.run('DELETE FROM monkey WHERE id = ?', [monkey.getId()]); filename: './db/database.db',
driver: sqlite3.Database
});
await this.db.run('DELETE FROM monkey WHERE id = ?', [id]);
} }
async deleteAll(){ async deleteAll(){
this.db = await open({
filename: './db/database.db',
driver: sqlite3.Database
});
await this.db.run('DELETE FROM monkey'); await this.db.run('DELETE FROM monkey');
} }
async findAll(){ async findAll(){
this.db = await open({
filename: './db/database.db',
driver: sqlite3.Database
});
const monkeys = await this.db.all('SELECT * FROM monkey'); const monkeys = await this.db.all('SELECT * FROM monkey');
return monkeys; return monkeys;
} }
async findById(id){ 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]); const monkey = await this.db.get('SELECT * FROM monkey WHERE id = ?', [id]);
return monkey; return monkey;
} }
......
import ISpeciesDAO from "../interfaces/ISpeciesDAO.js"; import ISpeciesDAO from "../interfaces/ISpeciesDAO.js";
import { open } from 'sqlite';
import sqlite3 from 'sqlite3';
class SpeciesDbDAO extends ISpeciesDAO{ class SpeciesDbDAO extends ISpeciesDAO{
constructor(db){ constructor(db){
super(); super();
this.db=db this.db=db
} }
async insert(species){ 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){ 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(){ async findAll(){
this.db = await open({
filename: './db/database.db',
driver: sqlite3.Database
});
const species = await this.db.all('SELECT * FROM species'); const species = await this.db.all('SELECT * FROM species');
return 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;
} }
} }
......
...@@ -18,6 +18,10 @@ export default class IMonkeyDbDAO { ...@@ -18,6 +18,10 @@ export default class IMonkeyDbDAO {
throw new Error('You have to implement the method delete!'); throw new Error('You have to implement the method delete!');
} }
deleteAll() {
throw new Error('You have to implement the method deleteAll!');
}
findAll() { findAll() {
throw new Error('You have to implement the method findAll!'); throw new Error('You have to implement the method findAll!');
} }
......
...@@ -17,6 +17,10 @@ export default class ISpeciesDAO{ ...@@ -17,6 +17,10 @@ export default class ISpeciesDAO{
throw new Error('Method "delete(id)" must be implemented'); throw new Error('Method "delete(id)" must be implemented');
} }
async deleteAll(){
throw new Error('Method "deleteAll" must be implemented');
}
async findall(){ async findall(){
throw new Error('Method "findall" must be implemented'); throw new Error('Method "findall" must be implemented');
} }
......
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;
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
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;
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
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
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
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
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
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
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
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;
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
[
{
"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"
}
]
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment