Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found
Select Git revision
Loading items

Target

Select target project
  • florian.lecointe.etu/ifi_express_tp
  • pirlouit.provis.etu/ifi_express_tp
  • allyson.bazir.etu/ifi_express_tp
  • fabien.delbecque.etu/ifi_express_tp
  • nicolas.dausque.etu/ifi_express_tp
  • florian.pourry.etu/ifi_express_tp
  • cyril.rocher.etu/ifi_express_tp
  • quentin.briand.etu/ifi_express_tp
8 results
Select Git revision
Loading items
Show changes
Commits on Source (2)
class Registry {
/**
* Build a new registry
*/
constructor(){
this.container = [];
}
/**
* Find an item of the registry
*
* @param {Number | Function} seeker is the criterium to find an item of the registry
*
* @returns an item from the registry or null if missing
*/
find( seeker ){
let seekerUnited;
if( seeker instanceof Function ){
seekerUnited = seeker;
} else {
seekerUnited = (id) => id === seeker;
}
for ( let index = 0; index < this.container.length; index++ )
if ( seekerUnited( index, this.container[ index ] ) ){
return {
id: index,
item: this.container[ index ]
};
}
return null;
}
/**
* Get the item id or null if the item missing
*
* @param {Type} item is the item seeked
*
* @returns the id of the item
*/
findId( item ){
for ( let index = 0; index < this.container.length; index++ )
if ( item === this.container[ index ] ){
return index;
}
return null;
}
/**
* Add a new item to the registry
*
* @param {Type} item is the new task to be added
*
* @returns the id of the item
*/
insert( item ){
return this.findId( item ) || (
this.container.push( item ) - 1
);
}
delete( id ){
let [item] = this.container.shift( id, 1 );
return item;
}
all(){
let list = [];
for ( let index = 0; index < this.container.length; index++ ){
list.push({
id: index,
item: this.container[ index ]
});
}
return list;
}
}
const TaskRegistry = new Registry();
exports.TaskRegistry = TaskRegistry;
const TaskListRegistry = new Registry();
exports.TaskListRegistry = TaskListRegistry;
\ No newline at end of file
module.exports.Task = class {
/**
* Create a task with a simple name
*
* @param {String} name is the task name
* @param {String} description is a small text to give more detail about the task
* @param {Number} start is the date from since the task start
* @param {Number} duetime is the date where the task sould be complete
*/
constructor( name, description = "", start = null, duetime = null ){
this.name = name;
this.description = description;
this.start = start;
this.duetime = duetime;
}
/**
* @returns the task has been started
*/
isStarted(){
if( this.start ){
return this.start < Date.now();
}
return false;
}
/**
* @returns if the task is finished
*/
isFinished(){
if( this.duetime ){
return this.duetime < Date.now();
}
return false;
}
/**
* @returns {String} a small description of this task
*/
toString(){
let format = (dateInMillis) => {
let date = new Date( dateInMillis );
return `${ date.getDay() }/${ date.getMonth() }/${ date.getFullYear() }`;
};
let date = "";
if( this.start && this.duetime ){
date = `${ format( this.start ) } -> ${ format( this.duetime ) }`
} else if( this.start ) {
date = `${ format( this.start ) } -> no due date`;
} else if( this.duetime ){
date = `due to ${ format( this.duetime ) }`;
}
return `${ this.name }: ${date}\n${this.description}`;
}
}
\ No newline at end of file
/**
* Sort a task by their start time and their due time
*
* @param {Task} taskA is the first one
* @param {Task} taskB is the second one
*
* @returns {Number} taskA is before {-1} or after {1} taskB
*/
function defaultCriterium( taskA, taskB ){
if( taskA.start && taskB.start ){
return taskA.start - taskB.start;
} else if( taskA.start ){
return -1;
} else if( taskB.start ){
return 1;
}
if( taskA.duetime && taskB.duetime ){
return taskA.duetime - taskB.duetime;
} else if( taskA.duetime ){
return -1;
} else if( taskB.duetime ){
return 1;
}
return 0;
}
module.exports.TaskList = class {
/**
* Create a task list
*
* @param {String} name is the task list name
* @param {Number[]} list is the default task for the task list
*/
constructor( name, list = [] ){
this.name = name;
this.list = list;
}
/**
* @returns true if the task has zero task
*/
isEmpty(){
return this.list.length === 0;
}
/**
* Add a task to the list
*
* @param {Number} taskId
*/
add( taskId ){
this.list.push( taskId );
}
/**
* Delete some task from the task list
*
* @param {Function} mustBeDeleted is the function who determine which task we should keep
*/
removeIf( mustBeDeleted ) {
let keepIt = [];
this.list.forEach((element) => {
if( ! mustBeDeleted( element ) ){
keepIt.push( element );
}
});
this.list = keepIt;
}
/**
* Remove a task from the TaskList
*
* @param {Number} taskId is the task toitem.toString()be deleted
*/
remove( taskId ){
this.removeIf( (anotherTask) => taskId == anotherTask );
}
/**
* Sort the task list
*
* @param {Function} criterium is the criterium to determine the order of the list
* @param {Boolean} duplicate determine if we should create a new task list or not
*
* @returns {TaskList} only when duplicate is true, create a new task list from the sorted list
*/
sort( criterium = defaultCriterium, duplicate = false ){
let sortedTask = this.task.sort( criterium );
if( duplicate ){
return new TaskList( sortedTask );
} else {
this.list = sortedTask;
}
return null;
}
/**
* @returns {String} print the task list
*/
toString(){
let print = `Liste des taches "${ this.name }"`;
return print;
}
/**
* Contains this task id
*
* @param {Number} id is the task id
*/
has( id ){
for (const _id of this.list) {
if( id === _id ){
return true;
}
}
return false;
}
};
\ No newline at end of file
const express = require( "express" );
const router = express.Router();
const { TaskListRegistry, TaskRegistry } = require( "./Registry" );
const { TaskList } = require( "./TaskList" );
let filter = (req, res, next) => {
let idStr = req.params.id;
if( idStr === null ){
res.status( 404 ).send( "Bad request" );
return;
}
let id = parseInt( idStr );
let it = TaskListRegistry.find( id );
if( it ){
req.tasklist = {
id: id,
value: it.item
};
next();
} else {
res.status( 404 ).send( `There is no task list binded to ${id} id` );
}
};
let formFilter = (req, res, next) => {
req.form = {
name: req.body.name,
tasks: req.body.task || []
};
if( req.form.tasks ){
if( req.form.tasks instanceof Array ){
let t = [];
for (const id of req.form.tasks) {
t.push( parseInt( id ) );
}
req.form.tasks = t;
} else {
req.form.tasks = [
parseInt( req.form.tasks )
];
}
}
next();
};
router.get( "/", (req, res) => {
res.status( 200 ).render( "list/naviguation", {
list: TaskListRegistry.all()
});
});
router.get( "/new", (req, res) => {
res.status( 200 ).render( "list/form", {
target: "/list/new",
tasks: TaskRegistry.all(),
selected: () => false
});
});
router.post( "/new", formFilter, (req, res) => {
let tasklist = new TaskList( req.form.name, req.form.tasks );
TaskListRegistry.insert( tasklist );
res.status( 200 ).render( "list/naviguation", {
list: TaskListRegistry.all()
});
});
router.get( "/:id", filter, (req, res) => {
let tasks = [];
for (const id of req.tasklist.value.list ) {
tasks.push( TaskRegistry.find( id ) );
}
res.status( 200 ).render( "list/view", {
tasks: tasks,
tasklist: req.tasklist
});
});
router.get( "/:id/edit", filter, (req, res) => {
res.status( 200 ).render( "list/form", {
target: `/list/${ req.tasklist.id }/edit`,
tasks: TaskRegistry.all(),
selected: (id) => {
return req.tasklist.value.has( id );
}
});
});
router.post( "/:id/edit", filter, formFilter, (req, res) => {
req.tasklist.value.list = req.form.tasks;
let tasks = [];
for (const id of req.tasklist.value.list ) {
tasks.push( TaskRegistry.find( id ) );
}
res.status( 200 ).render( "list/view", {
tasks: tasks,
tasklist: req.tasklist
});
});
router.post( "/delete/:id", filter, (req, res) => {
TaskListRegistry.delete( req.tasklist.id );
res.status( 200 ).render( "list/naviguation", {
list: TaskListRegistry.all()
});
});
module.exports = router;
\ No newline at end of file
const express = require( "express" );
const router = express.Router();
const { TaskRegistry } = require( "./Registry" );
const { Task } = require( "./Task" );
router.get( "/", (req, res) => {
res.status( 200 ).render( "task/NavPage", {
list: TaskRegistry.all()
});
});
let filter = (req, res, next) => {
let idStr = req.params.id;
if( idStr === null ){
res.status( 404 ).send( "Bad request" );
return;
}
let id = parseInt( idStr );
let it = TaskRegistry.find( id );
if( it ){
req.task = {
id: id,
value: it.item
};
next();
} else {
res.status( 404 ).send( `There is no task binded to ${id} id` );
}
};
let formFilter = (req, res, next) => {
req.form = {
name: req.body.name,
description: req.body.description,
start: req.body.start,
duetime: req.body.duetime
};
if( req.form.start ){
req.form.start = new Date( req.form.start ).getTime();
}
if( req.form.duetime ){
req.form.duetime = new Date( req.form.duetime ).getTime();
}
next();
};
router.get( "/display/:id", filter, (req, res) => {
console.log( req.task.value.toString() );
res.status( 200 ).send( req.task.value.toString() );
});
router.get( "/edit/:id", filter, (req, res) => {
res.status( 200 ).render( "task/form", {
target: `/edit/${ req.task.id }`,
task: req.task.value
});
});
router.post( "/edit/:id", filter, formFilter, (req, res) => {
if( req.form.name ){
req.task.value.name = req.form.name;
}
if( req.form.description ){
req.task.value.description = req.form.description;
}
if( req.form.start ){
req.task.value.start = req.form.start;
}
if( req.form.duetime ){
req.task.value.duetime = req.form.duetime;
}
res.status( 200 ).send( req.task.value.toString() );
});
router.post( "/delete/:id", filter, formFilter, (req, res) => {
TaskRegistry.delete( req.task.id );
res.status( 200 ).render( "task/NavPage", {
list: TaskRegistry.all()
});
});
router.get( "/new", (req, res) => {
res.status( 200 ).render( "task/form", {
target: `/task/new`
});
});
router.post( "/new", formFilter, (req, res) => {
console.log( JSON.stringify( req.form ) );
let task = new Task(
req.form.name,
req.form.description,
req.form.start,
req.form.duetime
);
console.log( "Added: " + task.toString() );
TaskRegistry.insert( task );
res.status( 200 ).render( "task/NavPage", {
list: TaskRegistry.all()
});
});
module.exports = router;
\ No newline at end of file
const express = require( "express" );
const app = express();
const bodyParser = require('body-parser')
app.use( bodyParser.json() ); // to support JSON-encoded bodies
app.use(bodyParser.urlencoded({ // to support URL-encoded bodies
extended: true
}));
app.set('view engine', 'pug');
app.set('views', `${ __dirname }/web` );
const TaskListRouter = require( "./TaskListRouter" );
app.use( "/list", TaskListRouter );
const TaskRouter = require( "./TaskRouter" );
app.use( "/task", TaskRouter );
app.listen( 8080 );
\ No newline at end of file
html
body
form(action=target, method="post")
p Nom de la liste des tâches
input(type="text", name="name")
table
th
td
td Nom de la tâche
each task in tasks
tr
td
if selected( task.id )
input(type="checkbox", name="task", value=task.id, checked="checked")
else
input(type="checkbox", name="task", value=task.id )
td #{ task.item.name }
button(type="submit") Validez
\ No newline at end of file
html
body
table
th
td Liste de tâches
td Actions
each tasklist in list
tr
td #{ tasklist.item.name }
td
a(href=`/list/${ tasklist.id }`) détail
a(href=`/list/delete/${ tasklist.id }` ) supprimer
a(href=`/list/new`) ajouter une nouvelle liste de tâche
html
body
h1 #{ tasklist.value.name }
table
th
td Nom de la tâche
td Actions
each task in tasks
tr
td #{ task.item.name }
td
a(href=`/task/${ task.id }`) détail
a(href=`/list/${ tasklist.id }/edit`) Editer la liste
html
body
table
th
td Identifiant de la tache
td Nom
td Description
td Action
each task in list
tr
td #{ task.id }
td #{ task.item.name }
td #{ task.item.description }
td
a(href=`/task/display/${ task.id }`) Afficher
a(href=`/task/edit/${ task.id }`) Editer
a(href=`/task/delete/${ task.id }`) Supprimer
a( href="/task/new" ) Crée une nouvelle tâche
\ No newline at end of file
html
body
form(action=target, method="post")
p Nom de la tâche
input(type="text", name="name", value= task ? task.name : "" )
p Description
input(type="text", name="description", value= task ? task.description : "")
p Début de la tache
input(type="date", name="start", value= task && task.start ? task.start : "")
p Fin de la tache
input(type="date", name="duetime", value= task && task.duetime ? task.duetime : "")
button(type="submit") valider
\ No newline at end of file