Skip to content

Commit a024233

Browse files
committed
Added methods for deleting and editing countries
1 parent 4427305 commit a024233

File tree

1 file changed

+72
-4
lines changed

1 file changed

+72
-4
lines changed

app/models/Country.php

Lines changed: 72 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,83 @@
22
class Country {
33
private $db;
44

5-
public function __construct() {
5+
public function __construct()
6+
{
67
$this->db = new DatabaseController;
78
}
89

9-
public function getCountries() {
10+
public function getContinents()
11+
{
12+
// https://stackoverflow.com/a/2350145
13+
$this->db->query("SELECT SUBSTRING(COLUMN_TYPE,5) as :name
14+
FROM information_schema.COLUMNS
15+
WHERE TABLE_SCHEMA = :database
16+
AND TABLE_NAME = :table
17+
AND COLUMN_NAME = :column");
18+
19+
$this->db
20+
->bind(":name", "continents")
21+
->bind(":database", DB_NAME)
22+
->bind(":table", "countries")
23+
->bind(":column", "continent");
24+
25+
$results = [];
26+
27+
preg_match_all(
28+
"/'([^']+?)',{0,1}/",
29+
$this->db->execute()->fetchAll()[0]["continents"],
30+
$results
31+
);
32+
33+
return $results[1];
34+
}
35+
36+
public function getCountries()
37+
{
1038
$this->db->query("SELECT * FROM `countries`");
1139

12-
$result = $this->db->execute()->fetchAll();
40+
return $this->db->execute()->fetchAll();
41+
}
42+
43+
public function getCountry(int $id)
44+
{
45+
$this->db->query("SELECT * FROM `countries` WHERE `id` = :id");
46+
47+
$this->db->bind(":id", $id);
48+
49+
return $this->db->execute()->fetch();
50+
}
51+
52+
public function deleteCountry(int $id)
53+
{
54+
$this->db->query("DELETE FROM `countries` WHERE `id` = :id");
55+
56+
$this->db->bind(":id", $id);
57+
58+
$this->db->execute();
59+
60+
return $this;
61+
}
62+
63+
public function editCountry(int $id, string $name, string $capitalCity, string $continent, int $population)
64+
{
65+
$this->db->query("UPDATE `countries`
66+
SET
67+
`name` = :name,
68+
`capitalCity` = :capitalCity,
69+
`continent` = :continent,
70+
`population` = :population
71+
WHERE `id` = :id");
72+
73+
$this->db
74+
->bind(":name", $name)
75+
->bind(":capitalCity", $capitalCity)
76+
->bind(":continent", $continent)
77+
->bind(":population", $population)
78+
->bind(":id", $id);
79+
80+
$this->db->execute();
1381

14-
return $result;
82+
return $this;
1583
}
1684
}

0 commit comments

Comments
 (0)