forked from hurbcom/challenge-bravo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCurrencyDAO.php
168 lines (97 loc) · 3.48 KB
/
CurrencyDAO.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
require_once('Currency.php');
require_once('ConnectionFactory.php');
class CurrencyDAO {
public function selectByCode(string $code) : ?Currency {
$curr = null;
try {
$conn = new ConnectionFactory();
$conn = $conn->getConnection();
$stmt = $conn->prepare("select * from Currency where code = :code");
$stmt->bindValue(':code', $code, PDO::PARAM_STR);
$stmt->execute();
$stmt->setFetchMode(PDO::FETCH_ASSOC);
$result = $stmt->fetch();
if(!empty($result)) {
$curr = new Currency();
$curr->setId($result["id"]);
$curr->setCode($result["code"]);
$curr->setName($result["name"]);
$curr->setRate($result["rate"]);
$curr->setSrc($result["src"]);
$curr->setLastUpdate($result["lastUpdate"]);
}
$stmt->closeCursor();
$con = null;
return $curr;
}catch(Exception $ex){
return null;
}
}
// importante para verificar a sincronização com a API externa
public function selectLastUpdateFromExternal() {
try{
$conn = new ConnectionFactory();
$conn = $conn->getConnection();
$stmt = $conn->prepare("select distinct lastUpdate from Currency where src = 'external'");
$stmt->execute();
$stmt->setFetchMode(PDO::FETCH_ASSOC);
$result = $stmt->fetch();
$stmt->closeCursor();
$con = null;
return $result;
}catch(PDOException $ex){
return null;
}
}
public function insert(Currency $curr): ?string {
try {
$conn = new ConnectionFactory();
$conn = $conn->getConnection();
$stmt = $conn->prepare("insert into Currency (code, name, rate, src, lastUpdate)
values (upper(:code), :name, ifnull(:rate, 0), :src, ifnull(:lastUpdate, 0))");
$stmt->bindValue(':code', $curr->getCode(), PDO::PARAM_STR);
$stmt->bindValue(':name', $curr->getName(), PDO::PARAM_STR);
$stmt->bindValue(':rate', $curr->getRate(), PDO::PARAM_STR);
$stmt->bindValue(':src', $curr->getSrc(), PDO::PARAM_STR);
$stmt->bindValue(':lastUpdate', $curr->getLastUpdate(), PDO::PARAM_INT);
$stmt->execute();
$stmt->closeCursor();
$con = null;
return null;
}catch(PDOException $ex){
return $ex->getMessage();
}
}
public function update(Currency $curr): ?string {
try {
$conn = new ConnectionFactory();
$conn = $conn->getConnection();
$stmt = $conn->prepare("update Currency set rate = :rate, lastUpdate = :lastUpdate where code = :code");
$stmt->bindValue(':code', $curr->getCode(), PDO::PARAM_STR);
$stmt->bindValue(':rate', $curr->getRate(), PDO::PARAM_STR);
$stmt->bindValue(':lastUpdate', $curr->getLastUpdate(), PDO::PARAM_INT);
$stmt->execute();
$stmt->closeCursor();
$con = null;
return null;
}catch(PDOException $ex){
return $ex->getMessage();
}
}
public function delete(Currency $curr): ?string {
try {
$conn = new ConnectionFactory();
$conn = $conn->getConnection();
$stmt = $conn->prepare("delete from Currency where code = :code");
$stmt->bindValue(':code', $curr->getCode(), PDO::PARAM_STR);
$stmt->execute();
$stmt->closeCursor();
$con = null;
return null;
}catch(PDOException $ex){
return $ex->getMessage();
}
}
}
?>