Can get all the likes of a user

This commit is contained in:
Pierre 2018-05-13 17:50:54 +02:00
parent b6d0d88622
commit c95e2d7498
3 changed files with 101 additions and 1 deletions

View File

@ -5,7 +5,7 @@
* @author Pierre HUBERT * @author Pierre HUBERT
*/ */
class likesController { class LikesController {
/** /**
* Update a specific component like * Update a specific component like
@ -80,4 +80,23 @@ class likesController {
return array("success" => "Like status updated."); return array("success" => "Like status updated.");
} }
/**
* Turn a UserLike object into valid API entry
*
* @param UserLike $object The object to convert
* @return array Valid API database entry
*/
public static function UserLikeToAPI(UserLike $object) : array {
$data = array();
$data["id"] = $object->get_id();
$data["userID"] = $object->get_userID();
$data["time_sent"] = $object->get_time_sent();
$data["elem_type"] = $object->get_elem_type();
$data["elem_id"] = $object->get_elem_id();
return $data;
}
} }

View File

@ -111,6 +111,29 @@ class Likes {
} }
/**
* Get all the likes of a user, as UserLike objects
*
* @param int $userID The ID of the target user
* @return array The list of likes
*/
public function get_all_user(int $userID) : array {
//Query the database
$table = self::LIKES_TABLE;
$condition = "WHERE ID_personne = ?";
$condValues = array($userID);
$entries = cs()->db->select($table, $condition, $condValues);
$likes = array();
//Process the list of likes
foreach($entries as $entry)
$likes[] = $this->dbToUserLike($entry);
return $likes;
}
/** /**
* Delete all the likes associated to an element * Delete all the likes associated to an element
* *
@ -146,6 +169,25 @@ class Likes {
} }
/**
* Turn database entry into UserLike object
*
* @param array $entry The database entry
* @return UserLike Generated user like object
*/
private static function dbToUserLike(array $entry) : UserLike {
$like = new UserLike();
$like->set_id($entry["ID"]);
$like->set_userID($entry["ID_personne"]);
$like->set_time_sent(strtotime($entry["Date_envoi"]));
$like->set_elem_id($entry["ID_type"]);
$like->set_elem_type($entry["type"]);
return $like;
}
} }
//Register class //Register class

View File

@ -0,0 +1,39 @@
<?php
/**
* UserLike object
*
* @author Pierre HUBERT
*/
class UserLike extends BaseUniqueObjectFromUser {
//Private fields
private $elem_id;
private $elem_type;
//Set and get element ID
public function set_elem_id(int $elem_id){
$this->elem_id = $elem_id;
}
public function has_elem_id() : bool {
return $this->elem_id > 0;
}
public function get_elem_id() : int {
return $this->elem_id;
}
//Set and get elem type
public function set_elem_type(string $elem_type){
$this->elem_type = $elem_type == "" ? null : $elem_type;
}
public function has_elem_type() : bool {
return $this->elem_type != null;
}
public function get_elem_type() : string {
return $this->elem_type != null ? $this->elem_type : "null";
}
}