Monster_Collection_Handler.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. require_once($path["handler"] . "Handler.php");
  3. require_once($path["entity"] . "Monster_Collection.php");
  4. /**
  5. * Handles the update of a monster in the collection.
  6. */
  7. class Monster_Collection_Handler extends Handler{
  8. /**
  9. * Constructor.
  10. *
  11. * Handles the database operation.
  12. *
  13. * @param SQLite3 $db Connection to the database.
  14. * @param int $id Monster identifier in the collection. Will be ignored
  15. * when inserting.
  16. * @param string $mode 'insert', 'update' or 'delete'.
  17. * @param string[] $params Array of parameters with the properties to
  18. * modify. Will ony use 'monster', 'stars',
  19. * 'level' and 'awaken'. Will be ignored when
  20. * deleting. All of them are required when
  21. * inserting.
  22. */
  23. public function __construct($db, $id, $mode, $params){
  24. parent::__construct($db, new Monster_Collection($db, $id));
  25. if (strtoupper($mode{0}) == "I"){
  26. $this->insert($params);
  27. }
  28. elseif(strtoupper($mode{0}) == "D"){
  29. $this->delete();
  30. }
  31. elseif(strtoupper($mode{0}) == "U"){
  32. $this->update($params);
  33. }
  34. else{
  35. error_log("Monster_Collection_Handler::__construct : Unknown mode " + $mode);
  36. $this->response = 400;
  37. }
  38. }
  39. private function insert($params){
  40. $s =
  41. "INSERT INTO monster_collection (monster, stars, level, awaken) VALUES( " .
  42. $params["monster"] . ", " .
  43. $params["stars"] . ", " .
  44. $params["level"] . ", " .
  45. $params["awaken"] . " " .
  46. "); ";
  47. if ($this->db->exec($s)){
  48. $this->response = 200;
  49. }
  50. else{
  51. error_log("Monster_Collection_Handler::insert : Query error for --" + $s + "-- " + mysqli_error($this->db));
  52. $this->response = 400;
  53. }
  54. }
  55. private function delete(){
  56. $s =
  57. "UPDATE monster_collection SET " .
  58. " stars = 0, " .
  59. " level = 0, " .
  60. " awaken = 0 " .
  61. "WHERE id = " . $this->entity->id . "; ";
  62. if ($this->db->exec($s)){
  63. $this->response = 200;
  64. }
  65. else{
  66. error_log("Monster_Collection_Handler::delete : Query error for --" + $s + "-- " + mysqli_error($this->db));
  67. $this->response = 400;
  68. }
  69. }
  70. private function update($params){
  71. if (isset($params["monster"])){
  72. $this->entity->monster = new Monster($params["monster"]);
  73. }
  74. if (isset($params["stars"])){
  75. $this->entity->stars = $params["stars"];
  76. }
  77. if (isset($params["level"])){
  78. $this->entity->level = $params["level"];
  79. }
  80. if (isset($params["awaken"])){
  81. $this->entity->awaken = $params["awaken"];
  82. }
  83. $s =
  84. "UPDATE monster_collection SET " .
  85. " monster = " . $this->monster->id . ", " .
  86. " stars = " . $this->stars . ", " .
  87. " level = " . $this->level . ", " .
  88. " awaken = " . $this->awaken . " " .
  89. "WHERE id = " . $this->entity->id . "; ";
  90. if ($this->db->exec($s)){
  91. $this->response = 200;
  92. }
  93. else{
  94. error_log("Monster_Collection_Handler::update : Query error for --" + $s + "-- " + mysqli_error($this->db));
  95. $this->response = 400;
  96. }
  97. }
  98. }
  99. ?>