-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask.php
86 lines (74 loc) · 1.77 KB
/
task.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php
include 'classes/Task.class.php';
$data = json_decode( file_get_contents('php://input'), true );
$task = new Task();
$option = ( !isset ($data['params']['option']) ) ? 'invalid' : $data['params']['option'];
//print_r( $data );
// echo $_REQUEST['option'];
switch ( $option ) {
case 'read':
echo json_encode( $task->all() );
break;
case 'create':
$data = array(
$data['params']['name'],
$data['params']['description'],
$data['params']['priority'],
$data['params']['completed']
);
$id = $task->insert( $data );
if ( $id > 0 ) {
echo json_encode(array(
'id' => $id,
'message' => 'Cadastrado com sucesso!',
'error' => false
));
} else {
echo json_encode(array(
'id' => 0,
'message' => 'Erro ao cadastrar a tarefa!',
'error' => true
));
}
break;
case 'update':
$task_data = array(
$data['params']['name'],
$data['params']['description'],
$data['params']['priority'],
$data['params']['completed'],
$data['params']['id']
);
$linhas_afetados = $task->update( $task_data );
if ( $linhas_afetados > 0 ) {
echo json_encode(array(
'lines' => $linhas_afetados, // Total of lines affected
'message' => 'Atualizado com sucesso!',
'error' => false
));
} else {
echo json_encode(array(
'lines' => 0,
'message' => 'Erro ao atualizar a tarefa!',
'error' => true
));
}
break;
case 'delete':
$id = is_integer( $data['params']['id'] ) ? $data['params']['id'] : 0;
if( ( $id > 0 ) && ( $task->delete($id) > 0 ) ) {
echo json_encode( array(
'message' => 'Deletado com sucesso!',
'error' => false
));
} else {
echo json_encode( array(
'message' => 'Erro ao deletar!',
'error' => true
));
}
break;
default:
echo "Ação inválida";
break;
}