ManagePosts.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. /*
  3. * Copyright (C) 2022 Echedey López Romero <elr@disroot.org>
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. class ManagePosts {
  19. private static $Instance = null;
  20. private FirebaseCon $Connection;
  21. private string $SpecificPath;
  22. private function __construct(FirebaseCon $Connection, string $SpecificPath) {
  23. $this->Connection = $Connection;
  24. $this->SpecificPath = $SpecificPath;
  25. }
  26. public static function GetInstance(FirebaseCon $Connection, string $SpecificPath) {
  27. if (self::$Instance === null) {
  28. self::$Instance = new ManagePosts($Connection, $SpecificPath);
  29. }
  30. return self::$Instance;
  31. }
  32. public function GetPosts() {
  33. $Result = $this->Connection->GetList($this->SpecificPath);
  34. if (isset($Result['error'])) {
  35. return 'General error while requesting';
  36. } else if (count($Result) === 0) {
  37. return 'No posts found';
  38. } else {
  39. $PostsList = [];
  40. foreach ($Result as $PostData) {
  41. $Post = new Post($PostData['id'], $PostData['user_id'],
  42. $PostData['title'], $PostData['date'],
  43. $PostData['image'], $PostData['content'],
  44. $PostData['comments']);
  45. array_push($PostsList, $Post);
  46. }
  47. return $PostsList;
  48. }
  49. }
  50. public function GetPost(string $Id) {
  51. $Result = $this->Connection->GetListElement($this->SpecificPath, $Id);
  52. if (isset($Result['error'])) {
  53. return 'General error while requesting';
  54. } else if (count($Result) === 0) {
  55. return 'Post does not exist';
  56. } else {
  57. $Post = new Post($Result['id'], $Result['user_id'],
  58. $Result['title'], $Result['date'],
  59. $Result['image'], $Result['content'],
  60. $Result['comments']);
  61. return $Post;
  62. }
  63. }
  64. public function AddPost(Post $Post): string {
  65. $PostArray = [
  66. 'id' => $Post->GetId(),
  67. 'user_id' => $Post->GetUserId(),
  68. 'title' => $Post->GetTitle(),
  69. 'date' => $Post->GetDate(),
  70. 'image' => $Post->GetImage(),
  71. 'content' => $Post->GetContent(),
  72. 'comments' => $Post->GetComments()
  73. ];
  74. if ($this->Connection->AddListElement($this->SpecificPath, $PostArray)) {
  75. return 'Post added successfully';
  76. } else {
  77. return 'Post already exists';
  78. }
  79. }
  80. public function RemovePost(string $Id): string {
  81. if ($this->Connection->RemoveListElement($this->SpecificPath, $Id)) {
  82. return 'Post removed successfully';
  83. } else {
  84. return 'Post does not exist';
  85. }
  86. }
  87. }