Commit 92287d25 authored by Manish Kumar's avatar Manish Kumar
Browse files

Merge branch 'master' of gitlab.unige.ch:Manish.Kumar/webpoke

parents 8595b56a 2eb678e4
module.exports = {
host: "10.25.10.21",
user: "g2",
password: "qP1r7rnfIybWdHKk",
database: "g2"
host: "localhost",
user: "root",
//password: "null",
database: "webpoke"
};
\ No newline at end of file
......@@ -3,89 +3,85 @@ const Pokemon = require("../models/pokemon.model.js");
exports.create = (req, res) => {
// Validate request
if (!req.body) {
res.status(400).send({
message: "Content can not be empty!"
});
res.status(400).send({
message: "Content can not be empty!"
});
}
// Create a team pokemon
const pokemon = new Pokemon({
id_pokemon : req.body.id_pokemon,
id_joueur : req.session.uid,
courant : 1
id_pokemon: req.body.id_pokemon,
id_joueur: req.session.uid,
courant: 1
});
// add pokemon to team in the database
Pokemon.create(pokemon, (err, data) => {
if (err)
res.status(500).send({
message:
err.message || "Some error occurred while creating the Pokemon."
});
else res.send(data);
if (err)
res.status(500).send({
message: err.message || "Some error occurred while creating the Pokemon."
});
else res.send(data);
});
};
exports.findAll = (req, res) => {
Pokemon.getAll((err, data) => {
if (err)
res.status(500).send({
message:
err.message || "Some error occurred while retrieving Pokemon."
});
else res.send(data);
if (err)
res.status(500).send({
message: err.message || "Some error occurred while retrieving Pokemon."
});
else res.send(data);
});
};
exports.getTeamUtil = (req, res) => {
Pokemon.getTeamUtil(req.body.uid, (err, data) => {
if (err) {
if (err.kind === "not_found") {
res.send({res: true, body: data});
} else {
res.status(500).send({
message: "Error retrieving Team with id " + req.body.uid
});
}
} else{
Pokemon.getTeamUtil(req.body.uid, (err, data) => {
if (err) {
if (err.kind === "not_found") {
res.send({ res: true, body: data });
} else {
res.status(500).send({
message: "Error retrieving Team with id " + req.body.uid
});
}
} else {
res.send({res: true, body: data});
}
});
res.send({ res: true, body: data });
}
});
};
exports.updateMove = (req, res) => {
Pokemon.updateMove(req.body.nMove, req.body.id_team, req.body.id_move, (err, data) => {
if (err) {
if (err.kind === "not_found") {
res.send({res: false, body: data});
} else {
res.status(500).send({
message: "Error updating moves",
received: req.body
});
}
} else{
Pokemon.updateMove(req.body.nMove, req.body.id_team, req.body.id_move, (err, data) => {
if (err) {
if (err.kind === "not_found") {
res.send({ res: false, body: data });
} else {
res.status(500).send({
message: "Error updating moves",
received: req.body
});
}
} else {
res.send({res: true, body: data});
}
});
res.send({ res: true, body: data });
}
});
};
exports.remove = (req, res) => {
Pokemon.remove(req.session.uid ,req.body.id_pokemon, (err, data) => {
if (err) {
if (err.kind === "not_found") {
res.status(404).send({
message: `Not found Team with user id ${req.session.uid} and Pokemon id ${req.body.id_pokemon}`
});
} else {
res.status(500).send({
message: "Could not delete Pokemon from team with user id " + req.session.uid + " and Pokemon id " + req.body.id_pokemon
});
}
} else res.send({ message: `Pokemon was deleted successfully from Team!` });
});
};
Pokemon.remove(req.session.uid, req.body.id_pokemon, (err, data) => {
if (err) {
if (err.kind === "not_found") {
res.status(404).send({
message: `Not found Team with user id ${req.session.uid} and Pokemon id ${req.body.id_pokemon}`
});
} else {
res.status(500).send({
message: "Could not delete Pokemon from team with user id " + req.session.uid + " and Pokemon id " + req.body.id_pokemon
});
}
} else res.send({ message: `Pokemon was deleted successfully from Team!` });
});
};
\ No newline at end of file
const sql = require("./db.js");
const JSONmanip = require("../methods/JSONmanip");
const { result } = require("underscore");
// constructor
const Pokemon = function(pokemon) {
......@@ -47,7 +48,7 @@ Pokemon.getTeamUtil = (joueur_id, result) => {
sql.beginTransaction(function(err) {
if (err) { throw err; }
sql.query('SELECT team.id_team, p.*, m.* FROM pokemon as p, team, moveUtil as mu, move as m WHERE id_joueur= ? AND p.id_pokemon=team.id_pokemon AND mu.move_id=m.id_move AND mu.pokemon_id=p.id_pokemon ORDER BY team.id_team;', [joueur_id], (err, res1) => {
if (err) {
if (err) {
console.log("error: ", err);
sql.rollback(function() {
throw err;
......@@ -64,18 +65,9 @@ Pokemon.getTeamUtil = (joueur_id, result) => {
console.log(res1);
var dataUtil = JSONmanip.filterJSON(res1, true);
sql.query('SELECT t.id_team, p.*, m.* FROM pokemon as p, movePoss as mp INNER JOIN move as m ON mp.id_move=m.id_move RIGHT JOIN team as t ON mp.id_team=t.id_team WHERE id_joueur= ? AND p.id_pokemon=t.id_pokemon ORDER BY t.id_team;', [joueur_id], (err, res2) => {
if (err) {
console.log("error: ", err);
sql.rollback(function() {
throw err;
});
result(err, null);
return;
}
sql.commit(function(err) {
if (err) {
if (err) {
console.log("error: ", err);
sql.rollback(function() {
throw err;
......@@ -83,23 +75,32 @@ Pokemon.getTeamUtil = (joueur_id, result) => {
result(err, null);
return;
}
if (!res2.length) {
//console.log("found team: ", res);
result({ kind: "not_found" }, null);
return;
}
var dataPoss = JSONmanip.filterJSON(res2, false);
var fusedData = JSONmanip.fuseJSON(dataUtil, dataPoss);
console.log('Transaction Complete.');
//console.log(fusedData);
result(null, fusedData);
//sql.end();
sql.commit(function(err) {
if (err) {
console.log("error: ", err);
sql.rollback(function() {
throw err;
});
result(err, null);
return;
}
if (!res2.length) {
//console.log("found team: ", res);
result({ kind: "not_found" }, null);
return;
}
var dataPoss = JSONmanip.filterJSON(res2, false);
var fusedData = JSONmanip.fuseJSON(dataUtil, dataPoss);
console.log('Transaction Complete.');
//console.log(fusedData);
result(null, fusedData);
//sql.end();
});
});
});
});
});
};
Pokemon.getTeamPoss = (joueur_id, result) => {
......@@ -121,7 +122,7 @@ Pokemon.getTeamPoss = (joueur_id, result) => {
});
};
Pokemon.updateMove = (nMove, id_team, id_move, result) =>{
Pokemon.updateMove = (nMove, id_team, id_move, result) => {
sql.query('UPDATE movePoss SET id_move=? WHERE id_team=? AND id_move=?;', [nMove, id_team, id_move], (err, res) => {
if (err) {
console.log("error: ", err);
......@@ -145,7 +146,7 @@ Pokemon.create = (newPokemon, result) => {
sql.beginTransaction(function(err) {
if (err) { throw err; }
sql.query('INSERT INTO team SET ?', newPokemon, function(err, res) {
if (err) {
if (err) {
console.log("error: ", err);
sql.rollback(function() {
throw err;
......@@ -153,18 +154,9 @@ Pokemon.create = (newPokemon, result) => {
result(err, null);
return;
}
sql.query('INSERT INTO movePoss (id_move, id_team) SELECT mu.move_id, ? FROM moveUtil as mu WHERE mu.pokemon_id=? LIMIT 4', [res.insertId, newPokemon.id_pokemon], function(err, result) {
if (err) {
console.log("error: ", err);
sql.rollback(function() {
throw err;
});
result(err, null);
return;
}
sql.commit(function(err) {
if (err) {
if (err) {
console.log("error: ", err);
sql.rollback(function() {
throw err;
......@@ -172,13 +164,22 @@ Pokemon.create = (newPokemon, result) => {
result(err, null);
return;
}
console.log('Transaction Complete.');
//sql.end();
sql.commit(function(err) {
if (err) {
console.log("error: ", err);
sql.rollback(function() {
throw err;
});
result(err, null);
return;
}
console.log('Transaction Complete.');
//sql.end();
});
});
result(null, { id: res.insertId, ...newPokemon });
});
});
});
};
......
module.exports = app => {
const pokemon = require("../controllers/pokemon.controller.js");
// Retrieve all Pokemon
app.get("/allpokemon", pokemon.findAll);
// Add Pokemon to a team
app.post("/addpokemon", pokemon.create);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment