| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- <?php
- require_once($path["handler"] . "Handler.php");
- require_once($path["entity"] . "Collection.php");
- /**
- * Handles the update of a monster in the collection.
- */
- class Collection_Handler extends Handler{
- /**
- * Constructor.
- *
- * Handles the database operation.
- *
- * @param SQLite3 $db Connection to the database.
- * @param int $id Monster identifier in the collection. Will be ignored
- * when inserting.
- * @param string $mode 'insert', 'update' or 'delete'.
- * @param string[] $params Array of parameters with the properties to
- * modify. Will ony use 'monster', 'stars',
- * 'level' and 'awaken'. Will be ignored when
- * deleting. All of them are required when
- * inserting.
- */
- public function __construct($db, $id, $mode, $params){
- parent::__construct($db, new Collection($db, $id));
- if (strtoupper($mode{0}) == "I"){
- $this->insert($params);
- }
- elseif(strtoupper($mode{0}) == "D"){
- $this->delete();
- }
- elseif(strtoupper($mode{0}) == "U"){
- $this->update($params);
- }
- else{
- error_log("Collection_Handler::__construct : Unknown mode " + $mode);
- $this->response = 400;
- }
- }
- private function insert($params){
- $s =
- "INSERT INTO collection (monster, stars, level, awaken) VALUES( " .
- $params["monster"] . ", " .
- $params["stars"] . ", " .
- $params["level"] . ", " .
- $params["awaken"] . " " .
- "); ";
- if ($this->db->exec($s)){
- $this->response = 200;
- }
- else{
- error_log("Collection_Handler::insert : Query error for --" + $s + "-- " + mysqli_error($this->db));
- $this->response = 400;
- }
- }
- private function delete(){
- $s =
- "UPDATE collection SET " .
- " stars = 0, " .
- " level = 0, " .
- " awaken = 0 " .
- "WHERE id = " . $this->entity->id . "; ";
- if ($this->db->exec($s)){
- $this->response = 200;
- }
- else{
- error_log("Collection_Handler::delete : Query error for --" + $s + "-- " + mysqli_error($this->db));
- $this->response = 400;
- }
- }
- private function update($params){
- if (isset($params["monster"])){
- $this->entity->monster = new Monster($params["monster"]);
- }
- if (isset($params["stars"])){
- $this->entity->stars = $params["stars"];
- }
- if (isset($params["level"])){
- $this->entity->level = $params["level"];
- }
- if (isset($params["awaken"])){
- $this->entity->awaken = $params["awaken"];
- }
- $s =
- "UPDATE collection SET " .
- " monster = " . $this->monster->id . ", " .
- " stars = " . $this->stars . ", " .
- " level = " . $this->level . ", " .
- " awaken = " . $this->awaken . " " .
- "WHERE id = " . $this->entity->id . "; ";
- if ($this->db->exec($s)){
- $this->response = 200;
- }
- else{
- error_log("Collection_Handler::update : Query error for --" + $s + "-- " + mysqli_error($this->db));
- $this->response = 400;
- }
- }
- }
- ?>
|