From c62529c8212658e29a1c7947df4b5da0d0a3d8f4 Mon Sep 17 00:00:00 2001 From: tomivt Date: Wed, 23 Oct 2024 16:38:10 +0200 Subject: [PATCH] Update Character Entity, Gateway & Model + Fix issues --- src/Entity/CharacterEntity.php | 51 +++++++++++++++ src/Entity/characterEntity.php | 46 -------------- src/Gateway/CharacterGateway.php | 76 ++++++++++++++++++++++ src/Gateway/characterGateway.php | 105 ------------------------------- src/Model/CharacterModel.php | 74 ++++++++++++++++++++++ src/Model/characterModel.php | 54 ---------------- 6 files changed, 201 insertions(+), 205 deletions(-) create mode 100644 src/Entity/CharacterEntity.php delete mode 100644 src/Entity/characterEntity.php create mode 100644 src/Gateway/CharacterGateway.php delete mode 100644 src/Gateway/characterGateway.php create mode 100644 src/Model/CharacterModel.php delete mode 100644 src/Model/characterModel.php diff --git a/src/Entity/CharacterEntity.php b/src/Entity/CharacterEntity.php new file mode 100644 index 0000000..f91906c --- /dev/null +++ b/src/Entity/CharacterEntity.php @@ -0,0 +1,51 @@ + id_character = $id_character; + $this -> name = $name; + $this -> img_char = $img_char; + } + + public function getIdCharacter(): int + { + return $this -> id_character; + } + + public function getName(): string + { + return $this -> name; + } + + public function getImgChar(): string + { + return $this -> img_char; + } + + public function setIdCharacter(int $id_character): void + { + $this -> id_character = $id_character; + } + + public function setName(string $name): void + { + $this -> name = $name; + } + + public function setImgChar(string $img_char): void + { + $this -> img_char = $img_char; + } + +} + diff --git a/src/Entity/characterEntity.php b/src/Entity/characterEntity.php deleted file mode 100644 index bac366e..0000000 --- a/src/Entity/characterEntity.php +++ /dev/null @@ -1,46 +0,0 @@ - id_character = $id_character; - $this -> name = $name; - $this -> img_path = $img_path; - } - - public function getIdCharacter() : int - { - return $this -> id_character; - } - public function getName() : string - { - return $this -> name; - } - public function getImgPath() : string - { - return $this -> img_path; - } - - public function setIdCharacter(int $id_character) : void - { - $this -> id_character = $id_character; - } - public function setName(string $name) : void - { - $this -> name = $name; - } - public function setImgPath(string $img_path) : void - { - $this -> img_path = $img_path; - } - -} - diff --git a/src/Gateway/CharacterGateway.php b/src/Gateway/CharacterGateway.php new file mode 100644 index 0000000..42769b6 --- /dev/null +++ b/src/Gateway/CharacterGateway.php @@ -0,0 +1,76 @@ + co = $co; + } + + public function create(int $id_character, string $name , string $img_char) : bool + { + $query = " + INSERT INTO Characters + VALUES(:id_char, :name, :img_char) + "; + + return $this -> co -> executeQuery($query, [ + 'id_char' => array($id_character, PDO::PARAM_INT), + 'name' => array($name, PDO::PARAM_STR), + 'img_char' => array($img_char, PDO::PARAM_STR) + ]); + + } + + public function findById(int $id) : array + { + $query = "SELECT * FROM Characters WHERE id_char = :id_c"; + $this -> co -> executeQuery($query, ["id_c" => array($id, PDO::PARAM_INT)]); + return $this -> co -> getResults(); + } + + public function findByName(string $name) : array + { + $query = "SELECT * FROM Characters WHERE name = :n"; + $this -> co -> executeQuery($query, ["n" => array($name, PDO::PARAM_STR)]); + return $this -> co -> getResults(); + } + + public function findAll() : array + { + $query = "SELECT * FROM Characters"; + $this -> co -> executeQuery($query); + return $this -> co -> getResults(); + } + + public function delete(int $id) : bool + { + $query = "DELETE FROM Characters WHERE id_char = :id_c"; + return $this -> co -> executeQuery($query, ["id_c" => array($id, PDO::PARAM_INT)]); + } + + public function update(int $id_char, string $name, string $img_char) : bool + { + $query = " + UPDATE Characters + SET name = :n, img_char = :i + WHERE id_char = :id_c + "; + + return $this -> co -> executeQuery($query, [ + "id_c" => array($id_char, PDO::PARAM_INT), + "name" => array($name, PDO::PARAM_STR), + "i" => array($img_char, PDO::PARAM_STR) + ]); + } + +} \ No newline at end of file diff --git a/src/Gateway/characterGateway.php b/src/Gateway/characterGateway.php deleted file mode 100644 index 36bf969..0000000 --- a/src/Gateway/characterGateway.php +++ /dev/null @@ -1,105 +0,0 @@ - co = $co; - } - - public function create(characterEntity $c) : bool - { - $query = " - INSERT INTO Character - VALUES(:id_character, :name, :img_path) - "; - - return $this -> co -> executeQuery($query, [ - "id_character" => array($c -> getIdCharacter(), PDO::PARAM_INT), - "name" => array($c -> getName(), PDO::PARAM_STR), - "img_path" => array($c -> getImgPath(), PDO::PARAM_STR) - ]); - - } - - public function findById(int $id) : ?characterEntity - { - $query = "SELECT * FROM Character WHERE id_character = :id_c"; - - $this -> co -> executeQuery($query, ["id_c" => array($id, PDO::PARAM_INT)]); - $res = $this ->co -> getResults(); - - if($res) - return new characterEntity( - $res["id_character"], - $res["name"], - $res["img_path"] - ); - return null; - } - - public function findByName(string $name) : ?characterEntity - { - $query = "SELECT * FROM Character WHERE name = :n"; - - $this -> co -> executeQuery($query, ["n" => array($name, PDO::PARAM_STR)]); - $res = $this ->co -> getResults(); - - if($res) - return new characterEntity( - $res["id_character"], - $res["name"], - $res["img_path"] - ); - return null; - } - - public function findAll() : array - { - $query = "SELECT * FROM Character"; - - $this -> co -> executeQuery($query); - $res = $this ->co -> getResults(); - - $characters = []; - - foreach ($res as $character) - { - $characters[] = new characterEntity( - $character["id_character"], - $character["name"], - $character["img_path"] - ); - } - return $characters; - } - - public function delete(int $id) : bool - { - $query = "DELETE FROM Character WHERE id_character = :id_c"; - - return $this -> co -> executeQuery($query, ["id_c" => array($id, PDO::PARAM_INT)]); - } - - public function update(characterEntity $c) : bool - { - $query = " - UPDATE Character - SET name = :n, img_path = :i - WHERE id_character = :id_c - "; - - return $this -> co -> executeQuery($query, [ - "id_c" => array($c -> getIdCharacter(), PDO::PARAM_INT), - "name" => array($c -> getName(), PDO::PARAM_STR), - "i" => array($c -> getImgPath(), PDO::PARAM_STR) - ]); - } - -} \ No newline at end of file diff --git a/src/Model/CharacterModel.php b/src/Model/CharacterModel.php new file mode 100644 index 0000000..6071bb3 --- /dev/null +++ b/src/Model/CharacterModel.php @@ -0,0 +1,74 @@ + gateway = $gw; + } + + public function createCharacter(int $id_character, string $name , string $img_char) : bool + { + return $this -> gateway -> create($id_character, $name, $img_char); + } + + public function getCharacterById(int $id_character) : ?CharacterEntity + { + $c = $this -> gateway -> findById($id_character); + if ($c) + return new CharacterEntity( + $c['id_character'], + $c['name'], + $c['img_path'] + ); + return null; + } + + public function getCharacterByName(string $name) : ?CharacterEntity + { + $c = $this -> gateway -> findByName($name); + if ($c) + return new CharacterEntity( + $c['id_character'], + $c['name'], + $c['img_path'] + ); + return null; + } + + public function getAllCharacters() : array + { + $c = $this -> gateway -> findAll(); + + $characters = []; + + foreach ($c as $character) + { + $characters[] = new CharacterEntity( + $character['id_character'], + $character['name'], + $character['img_path'] + ); + } + return $characters; + } + + public function deleteCharacter(int $id_character) : bool + { + return $this -> gateway -> delete($id_character); + } + + public function updateCharacter(int $id_character, string $name, string $img_char) : bool + { + return $this -> gateway -> update($id_character, $name, $img_char); + } + +} \ No newline at end of file diff --git a/src/Model/characterModel.php b/src/Model/characterModel.php deleted file mode 100644 index a9769b0..0000000 --- a/src/Model/characterModel.php +++ /dev/null @@ -1,54 +0,0 @@ - gateway = $gateway; - } - - public function createCharacter(int $id_character, string $name , string $img_path) : bool - { - $q = new CharacterEntity($id_character, $name, $img_path); - - return $this -> gateway -> create($q); - } - public function getCharacterById(int $id_character) : ?CharacterEntity - { - return $this -> gateway -> findById($id_character); - } - - public function getCharacterByName(string $name) : ?CharacterEntity - { - return $this -> gateway -> findByName($name); - } - - public function getAllCharacters() : array - { - return $this -> gateway -> findAll(); - } - - public function deleteCharacter(int $id_character) : bool - { - return $this -> gateway -> delete($id_character); - } - - public function updateCharacter(int $id_character, string $name, string $img_path) : bool - { - $q = $this -> gateway -> findById($id_character); - - if($q){ - $q -> setName($name); - $q -> setImgPath($img_path); - return $this -> gateway -> update($q); - } - return false; - } - -} \ No newline at end of file