Skip to content
Snippets Groups Projects
Commit 61af7954 authored by Mamadu-lamarana Bah's avatar Mamadu-lamarana Bah :speech_balloon:
Browse files

updt coté serveur et coté client : gestion suppression spectacle dans user

parent bbc727f0
Branches
No related tags found
No related merge requests found
Showing
with 224 additions and 104 deletions
......@@ -3,7 +3,7 @@
<head>
<meta charset="UTF-8"/>
<title>user profile</title>
<link rel="stylesheet" type="text/css" href="/stylesheets/style.css"/>
<link rel="stylesheet" type="text/css" href="../style/admin.css"/>
</head>
......
......@@ -3,7 +3,7 @@
<head>
<meta charset="UTF-8"/>
<title>login/register</title>
<link rel="stylesheet" type="text/css" href="/stylesheets/style.css"/>
<link rel="stylesheet" type="text/css" href="../style/style.css"/>
</head>
......
......@@ -3,7 +3,7 @@
<head>
<meta charset="UTF-8"/>
<title>login/register</title>
<link rel="stylesheet" type="text/css" href="/stylesheets/style.css"/>
<link rel="stylesheet" type="text/css" href="../style/style.css"/>
</head>
......
......@@ -3,7 +3,7 @@
<head>
<meta charset="UTF-8"/>
<title>user profile</title>
<link rel="stylesheet" type="text/css" href="/stylesheets/style.css"/>
<link rel="stylesheet" type="text/css" href="../style/style.css"/>
</head>
......@@ -22,13 +22,14 @@
<div id="content">
<div id="tickets"> <strong>Liste des tickets :</strong>
<div id="showTickets"> </div>
</div>
<div id="tickets"> <strong>Liste des tickets :</strong>
<div id="showTickets"> </div>
</div>
<div id="shows"> <strong>spectacles :</strong>
<div id="list">
</div>
<div id="shows"> <strong>spectacles :</strong>
<div id="list">
</div>
</div>
</div>
<!-- <div id="controls">
......
......@@ -34,9 +34,6 @@ const addToList = (show,list) => {
const node = document.createElement('div');
node.className = 'show';
node.textContent = `${show.description} (${show.places})`;
//
//node.addEventListener('mouseover', () => getTask(show._id));
//
const deleteButton = document.createElement('button');
deleteButton.className = 'delete';
deleteButton.addEventListener('click', () => deleteShow(show._id));
......@@ -56,6 +53,7 @@ const deleteShow =
if (response.ok) {
displayMessage('suppression effectuée');
displayList();
await fetch(`/${showId}`, requestOptions);
}
else {
displayMessage('problème suppression');
......
......@@ -2,13 +2,14 @@ let userlogin;
let userpassword;
let username;
//const displayList = require('./utils');
//const displayShowList = require('./utils');
const setup = () => {
username = document.getElementById('username');
getUser();
// document.getElementById('update').addEventListener('click', update);
displayList();
displayShowList();
displayTicketsList();
document.getElementById('logout').addEventListener('click', logout);
}
window.addEventListener('DOMContentLoaded', setup);
......@@ -28,7 +29,7 @@ const getUser = async () => {
}
}
const displayList = async () => {
const displayShowList = async () => {
const requestOptions = {
method : 'GET'
};
......@@ -68,7 +69,7 @@ const update = async (data) => {
headers : { "Content-Type": "application/json" },
body : body
};
const response = await fetch('/user/me', requestOptions);
const response = await fetch('/tickets', requestOptions);
if (response.ok) {
const updatedUser = await response.json();
console.log(`show infos updated : ${JSON.stringify(updatedUser)}`);
......@@ -79,42 +80,81 @@ const update = async (data) => {
}
}
const displayTicketsList = async () => {
const requestOptions = {
method : 'GET'
};
const response = await fetch('/tickets', requestOptions)
const allTickets = await response.json();
allTickets.forEach( ticket => addTicketsToList(ticket) );
}
const addTicketsToList = (ticket) => {
const list = document.getElementById("showTickets");
createDivTickets(ticket, list);
}
const addTicketShow = async (event) => {
const divShow = event.target.parentElement;
const nbTickets = addToTicketsList(divShow);
const ticketsData = {description : divShow.textContent.split(':')[0], tickets : nbTickets, show : divShow.id};
await update(ticketsData);
const showId = divShow.id;
const description = divShow.textContent.split(':')[0];
const ticket = {show : showId, description : description, tickets : 1};
const list = document.getElementById("showTickets");
const nbTickets = addToTicketsList(ticket, list);
ticket.tickets = nbTickets;
await update(ticket);
};
const addToTicketsList = (divShow) => {
let tmp;
const divTickets = document.getElementById("showTickets");
const showId = divShow.id + "-t";
if(document.getElementById(showId) === null) {
const node = document.createElement('div');
node.id = showId;
node.textContent = `${divShow.textContent.split(':')[0]}`;
const span = document.createElement('span');
span.className = "tickets";
span.textContent = 1;
node.appendChild(span);
const annulationButton = document.createElement('button');
annulationButton.className = 'annulation';
annulationButton.textContent = "Annuler";
annulationButton.addEventListener("click", cancelTicketShow);
node.appendChild(annulationButton);
divTickets.appendChild(node);
tmp = span.textContent;
const addToTicketsList = (ticket, list) => {
let nbTickets;
if(document.getElementById(ticket.show+"-t") === null) {
createDivTickets(ticket, list);
nbTickets = 1;
}else {
const sp = document.getElementById(showId).querySelector(".tickets");
const sp = document.getElementById(ticket.show+"-t").querySelector(".tickets");
sp.textContent = 1 + parseInt(sp.textContent);
tmp = parseInt(sp.textContent);
nbTickets = parseInt(sp.textContent);
}
return tmp;
return nbTickets;
};
const cancelTicketShow = () => {
const createDivTickets = (ticket, list) => {
const showId = ticket.show + "-t";
const node = document.createElement('div');
node.id = showId;
node.textContent = `${ticket.description}`;
const span = document.createElement('span');
span.className = "tickets";
span.textContent = ticket.tickets;
node.appendChild(span);
const annulationButton = document.createElement('button');
annulationButton.className = 'annulation';
annulationButton.textContent = "Annuler";
annulationButton.addEventListener("click", cancelTicketShow);
node.appendChild(annulationButton);
list.appendChild(node);
}
const cancelTicketShow = async (event) => {
const list = document.getElementById("showTickets");
const ticketDiv = event.target.parentElement;
list.removeChild(ticketDiv);
const showId = ticketDiv.id.split('-')[0];
const body = JSON.stringify(showId);
const requestOptions = {
method :'DELETE',
};
const response = await fetch(`/${showId}`, requestOptions);
if (response.ok) {
const updatedUser = await response.json();
console.log(`deleted ticket id : ${JSON.stringify(updatedUser)}`);
}
else {
const error = await response.json();
handleError(error);
}
}
const logout = async () => {
......@@ -134,3 +174,48 @@ const handleError = error => {
console.log(`erreur : ${error.message}`);
}
// const update = async (data) => {
// const body = JSON.stringify(data);
// const requestOptions = {
// method :'PUT',
// headers : { "Content-Type": "application/json" },
// body : body
// };
// const response = await fetch('/tickets', requestOptions);
// if (response.ok) {
// const updatedUser = await response.json();
// console.log(`show infos updated : ${JSON.stringify(updatedUser)}`);
// }
// else {
// const error = await response.json();
// handleError(error);
// }
// }
// const displayTicketsList = async () => {
// const requestOptions = {
// method : 'GET'
// };
// const response = await fetch('/tickets', requestOptions)
// const allTickets = await response.json();
// allTickets.forEach( ticket => addTicketsToList(ticket) );
// }
// const addTicketsToList = (ticket) => {
// const list = document.getElementById("showTickets");
// createDivTickets(ticket, list);
// }
// const addTicketShow = async (event) => {
// const divShow = event.target.parentElement;
// const nbTickets = addToTicketsList(divShow);
// const ticketsData = {description : divShow.textContent.split(':')[0], tickets : nbTickets, show : divShow.id};
// await update(ticketsData);
// };
......@@ -24,11 +24,24 @@ button {
font-weight : bold;
}
#list div.shows {
padding : 2px;
margin : 1px 2px;
}
div.shows:nth-of-type(even) {
background-color: #EEE;
}
.tickets {
width : 50px;
color: red;
}
#userdata {
background-color : #DDD;
}
#userdata div{
text-align : right;
text-align : center;
margin : 2px;
}
#controls {
......
const User = require('../models/user.model').model;
const path = require('path');
const { use } = require('../app');
module.exports.home = (_,res) => res.sendFile(path.join(__dirname, '../public/html/user.html'));
module.exports.list = async (_,res) => {
module.exports.listShow = async (_,res) => {
const allShows = await Shows.find();
res.status(200).json(allShows);
}
module.exports.listTickets = async (req,res) => {
const user = await User.findById(req.userId);
console.log(user);
res.status(200).json(user.reservedTickets);
}
module.exports.me =
async (req, res) => {
const user = await User.findById(req.userId);
......@@ -17,16 +24,41 @@ const path = require('path');
res.status(200).json({ name : user.name, admin : user.admin});
}
module.exports.deleteTickets =
async (req, res) => {
try {
await User.updateMany(
{ "reservedTickets.show": req.params.showId },
{ $pull: { reservedTickets: { show: req.params.showId } } },
)
res.status(200).json(req.params.showId);
}catch(error) {
throw error;
}
}
module.exports.update =
async (req,res) => {
const updatedData = { ...req.body };
console.log(updatedData);
const user = await User.findByIdAndUpdate(req.userId,
updatedData,
{ new : true });
res.status(200).json({ name : user.name , message : 'mise à jour réussie'});
}
try {
const updatedData = { ...req.body };
const updated = await User.updateOne(
{ _id: req.userId, "reservedTickets.show": updatedData.show },
{$set : {
"reservedTickets.$.description": updatedData.description,
"reservedTickets.$.tickets": updatedData.tickets,
},
});
if (updated.modifiedCount === 0) {
const user = await User.findByIdAndUpdate(req.userId,
{ $push: { reservedTickets: updatedData } },
{ new : true });
}
res.status(200).json({ description : updatedData.description , message : 'mise à jour réussie'});
} catch(error) {
console.log(error.errmsg)
res.status(400).json(error.errmsg);
}
}
module.exports.about =
(req, res) => res.render('about');
......
......@@ -17,10 +17,12 @@ const userSchema = new mongoose.Schema({
type : Boolean,
default: false
},
show: { type: mongoose.ObjectId},
description : { type : String, unique :true, required : true},
tickets: { type: Number, required: true },
});
reservedTickets: [{
show: { type: mongoose.ObjectId},
description : { type : String, required : true},
tickets: { type: Number, required: true }
}]
});
module.exports = userSchema;
......@@ -35,31 +37,3 @@ module.exports.model = User;
// const mongoose = require('mongoose');
// const usersModel = new mongoose.Schema({
// name : { type : String, unique :true, required : true},
// tickets : {
// type : Number,
// min : 0,
// default : 0,
// }
// });
// const dbConnection = require('../controllers/db.controller'); // importation de l'objet qui gère la connexion
// const Users = dbConnection.model('Task', usersModel, 'tasks');
// module.exports = usersModel;
// module.exports.model = Users;
......@@ -3,9 +3,9 @@
<head>
<meta charset="UTF-8"/>
<title>user profile</title>
<link rel="stylesheet" type="text/css" href="/stylesheets/style.css"/>
<link rel="stylesheet" type="text/css" href="../style/admin.css"/>
<script defer src="../scripts/admin-bundle.js?0dfd6f030b50bdc40759"></script></head>
<script defer src="../scripts/admin-bundle.js?c951f040e1da5516deac"></script></head>
<body>
<h1>Application Showtime</h1>
......
......@@ -3,9 +3,9 @@
<head>
<meta charset="UTF-8"/>
<title>login/register</title>
<link rel="stylesheet" type="text/css" href="/stylesheets/style.css"/>
<link rel="stylesheet" type="text/css" href="../style/style.css"/>
<script defer src="../scripts/login-bundle.js?0dfd6f030b50bdc40759"></script></head>
<script defer src="../scripts/login-bundle.js?c951f040e1da5516deac"></script></head>
<body>
<h1>Application de partage</h1>
......
......@@ -3,9 +3,9 @@
<head>
<meta charset="UTF-8"/>
<title>login/register</title>
<link rel="stylesheet" type="text/css" href="/stylesheets/style.css"/>
<link rel="stylesheet" type="text/css" href="../style/style.css"/>
<script defer src="../scripts/register-bundle.js?0dfd6f030b50bdc40759"></script></head>
<script defer src="../scripts/register-bundle.js?c951f040e1da5516deac"></script></head>
<body>
<h1>Identification</h1>
......
......@@ -3,9 +3,9 @@
<head>
<meta charset="UTF-8"/>
<title>user profile</title>
<link rel="stylesheet" type="text/css" href="/stylesheets/style.css"/>
<link rel="stylesheet" type="text/css" href="../style/style.css"/>
<script defer src="../scripts/user-bundle.js?0dfd6f030b50bdc40759"></script></head>
<script defer src="../scripts/user-bundle.js?c951f040e1da5516deac"></script></head>
<body>
<h1>Application Showtime</h1>
......@@ -22,13 +22,14 @@
<div id="content">
<div id="tickets"> <strong>Liste des tickets :</strong>
<div id="showTickets"> </div>
</div>
<div id="tickets"> <strong>Liste des tickets :</strong>
<div id="showTickets"> </div>
</div>
<div id="shows"> <strong>spectacles :</strong>
<div id="list">
</div>
<div id="shows"> <strong>spectacles :</strong>
<div id="list">
</div>
</div>
</div>
<!-- <div id="controls">
......
This diff is collapsed.
This diff is collapsed.
......@@ -24,11 +24,24 @@ button {
font-weight : bold;
}
#list div.shows {
padding : 2px;
margin : 1px 2px;
}
div.shows:nth-of-type(even) {
background-color: #EEE;
}
.tickets {
width : 50px;
color: red;
}
#userdata {
background-color : #DDD;
}
#userdata div{
text-align : right;
text-align : center;
margin : 2px;
}
#controls {
......
......@@ -9,7 +9,10 @@ const indexController = require('../controllers/index.controller');
router.get('/', authMiddleware.validToken, indexController.home );
router.get('/me', authMiddleware.validToken, indexController.me );
router.get('/items', authMiddleware.validToken, indexController.list);
router.put('/tickets', authMiddleware.validToken, indexController.update );
router.delete('/:showId', authMiddleware.validToken, indexController.deleteTickets);
router.get('/tickets', authMiddleware.validToken, indexController.listTickets);
router.get('/items', authMiddleware.validToken, indexController.listShow);
router.get('/about', indexController.about );
router.get('/adminonly', authMiddleware.validToken, authMiddleware.isAdmin, indexController.adminonly );
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment