mirror of
				https://github.com/pierre42100/ComunicAPI
				synced 2025-11-04 04:04:20 +00:00 
			
		
		
		
	Can update user page like status
This commit is contained in:
		
							
								
								
									
										59
									
								
								RestControllers/likesController.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										59
									
								
								RestControllers/likesController.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,59 @@
 | 
				
			|||||||
 | 
					<?php
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * API likes controller
 | 
				
			||||||
 | 
					 * 
 | 
				
			||||||
 | 
					 * @author Pierre HUBERT
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class likesController {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/**
 | 
				
			||||||
 | 
						 * Update a specific component like
 | 
				
			||||||
 | 
						 * 
 | 
				
			||||||
 | 
						 * @url POST /likes/update
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
 | 
						public function update_like(){
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							user_login_required(); //Need login
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							//Get component kind
 | 
				
			||||||
 | 
							if(!isset($_POST["type"]))
 | 
				
			||||||
 | 
								Rest_fatal_error(401, "Please specify like type !");
 | 
				
			||||||
 | 
							
 | 
				
			||||||
 | 
							$type = $_POST["type"];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							//Check new status for the like
 | 
				
			||||||
 | 
							if(!isset($_POST['like']))
 | 
				
			||||||
 | 
								Rest_fatal_error(400, "Please specify the new like status with the request !");
 | 
				
			||||||
 | 
							$like = $_POST['like'] == "true";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							//Find the right component type for checks
 | 
				
			||||||
 | 
							switch($type){
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								//In case of user
 | 
				
			||||||
 | 
								case "user":
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
									//Extract informations
 | 
				
			||||||
 | 
									$id = getPostUserId("id");
 | 
				
			||||||
 | 
									$componentType = Likes::LIKE_USER;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
									//Check if user can access page
 | 
				
			||||||
 | 
									if(!CS::get()->components->user->userAllowed(userID, $id))
 | 
				
			||||||
 | 
										Rest_fatal_error(401, "You can not access this user information !");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
									break;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								//Default case : error
 | 
				
			||||||
 | 
								default:
 | 
				
			||||||
 | 
									Rest_fatal_error(404, "Didn't find specified component type !");
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							//Update like status
 | 
				
			||||||
 | 
							CS::get()->components->likes->update(userID, $like, $id, $componentType);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							//Success
 | 
				
			||||||
 | 
							return array("success" => "Like status updated.");
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -69,6 +69,48 @@ class Likes {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/**
 | 
				
			||||||
 | 
						 * Update like status
 | 
				
			||||||
 | 
						 * 
 | 
				
			||||||
 | 
						 * @param int $userID The ID of the user
 | 
				
			||||||
 | 
						 * @param bool $like New like status
 | 
				
			||||||
 | 
						 * @param int $id The ID of the component element to update
 | 
				
			||||||
 | 
						 * @param string $kind The kind of component
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
 | 
						public function update(int $userID, bool $like, int $id, string $kind){
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							//Check if the request is not to like anymore
 | 
				
			||||||
 | 
							if(!$like){
 | 
				
			||||||
 | 
								
 | 
				
			||||||
 | 
								//Delete on the database
 | 
				
			||||||
 | 
								$conditions = "ID_personne = ? AND ID_type = ? AND type = ?";
 | 
				
			||||||
 | 
								$values = array($userID, $id, $this::KINDS_DB[$kind]);
 | 
				
			||||||
 | 
								CS::get()->db->deleteEntry($this::LIKES_TABLE, $conditions, $values);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							//We have to like component
 | 
				
			||||||
 | 
							else {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								//Check if user is already liking component
 | 
				
			||||||
 | 
								if($this->is_liking($userID, $id, $kind))
 | 
				
			||||||
 | 
									return; //Nothing to be done
 | 
				
			||||||
 | 
								
 | 
				
			||||||
 | 
								//Insert in the database
 | 
				
			||||||
 | 
								$values = array(
 | 
				
			||||||
 | 
									"ID_type" => $id,
 | 
				
			||||||
 | 
									"ID_personne" => $userID,
 | 
				
			||||||
 | 
									"Date_envoi" => mysql_date(),
 | 
				
			||||||
 | 
									"type" => $this::KINDS_DB[$kind]
 | 
				
			||||||
 | 
								);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								//Insert in the database
 | 
				
			||||||
 | 
								CS::get()->db->addLine($this::LIKES_TABLE, $values);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
//Register class
 | 
					//Register class
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user