123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- <?php
- /*
- * Copyright (C) 2022 Echedey López Romero <elr@disroot.org>
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- class ManagePosts {
- private static $Instance = null;
- private FirebaseCon $Connection;
- private string $SpecificPath;
- private function __construct(FirebaseCon $Connection, string $SpecificPath) {
- $this->Connection = $Connection;
- $this->SpecificPath = $SpecificPath;
- }
- public static function GetInstance(FirebaseCon $Connection, string $SpecificPath) {
- if (self::$Instance === null) {
- self::$Instance = new ManagePosts($Connection, $SpecificPath);
- }
- return self::$Instance;
- }
- public function GetPosts() {
- $Result = $this->Connection->GetList($this->SpecificPath);
- if (isset($Result['error'])) {
- return 'General error while requesting';
- } else if (count($Result) === 0) {
- return 'No posts found';
- } else {
- $PostsList = [];
- foreach ($Result as $PostData) {
- $Post = new Post($PostData['id'], $PostData['user_id'],
- $PostData['title'], $PostData['date'],
- $PostData['image'], $PostData['content'],
- $PostData['comments']);
- array_push($PostsList, $Post);
- }
- return $PostsList;
- }
- }
- public function GetPost(string $Id) {
- $Result = $this->Connection->GetListElement($this->SpecificPath, $Id);
- if (isset($Result['error'])) {
- return 'General error while requesting';
- } else if (count($Result) === 0) {
- return 'Post does not exist';
- } else {
- $Post = new Post($Result['id'], $Result['user_id'],
- $Result['title'], $Result['date'],
- $Result['image'], $Result['content'],
- $Result['comments']);
- return $Post;
- }
- }
- public function AddPost(Post $Post): string {
- $PostArray = [
- 'id' => $Post->GetId(),
- 'user_id' => $Post->GetUserId(),
- 'title' => $Post->GetTitle(),
- 'date' => $Post->GetDate(),
- 'image' => $Post->GetImage(),
- 'content' => $Post->GetContent(),
- 'comments' => $Post->GetComments()
- ];
- if ($this->Connection->AddListElement($this->SpecificPath, $PostArray)) {
- return 'Post added successfully';
- } else {
- return 'Post already exists';
- }
- }
- public function RemovePost(string $Id): string {
- if ($this->Connection->RemoveListElement($this->SpecificPath, $Id)) {
- return 'Post removed successfully';
- } else {
- return 'Post does not exist';
- }
- }
- }
|