diff --git a/Sources/api_database/sources/app/Model.php b/Sources/api_database/sources/app/Model.php index bc9fe9b..8b41730 100644 --- a/Sources/api_database/sources/app/Model.php +++ b/Sources/api_database/sources/app/Model.php @@ -1,6 +1,7 @@ addUser($data['idDafl'], $data['idSpotify'], $data['passw']); } -} \ No newline at end of file + + public function like($user,$liked) : bool { + global $app; + $db = $app->getContainer()['settings']['db']; + + $data = []; + $data['user'] = filter_var($user, FILTER_SANITIZE_STRING); + $data['liked'] = filter_var($liked, FILTER_SANITIZE_STRING); + $gw = new LikesGateway(new Connection($db['dsn'], $db['user'], $db['pass'])); + return $gw->addUser($data['idDafl'], $data['idSpotify'], $data['passw']); + } +} diff --git a/Sources/api_database/sources/app/gateways/LikesGateway.php b/Sources/api_database/sources/app/gateways/LikesGateway.php new file mode 100644 index 0000000..ca681f7 --- /dev/null +++ b/Sources/api_database/sources/app/gateways/LikesGateway.php @@ -0,0 +1,26 @@ +con = $con; + } + + public function addLike($user,$liked):bool + { + $query = 'INSERT INTO likes VALUES (:user,:liked)'; + $this->con->executeQuery($query,array(':user'=>array($user,PDO::PARAM_STR),':liked'=>array($liked,PDO::PARAM_STR))); + $query = 'SELECT * FROM likes WHERE (user=:user AND liked=:liked) OR (liked=:liked AND user=:user )'; + $this->con->executeQuery($query,array(':user'=>array($user,PDO::PARAM_STR),':liked'=>array($liked,PDO::PARAM_STR))); + $res=$this->con->getResults(); + if(count($res) == 2) { + $query = 'INSERT INTO matches VALUES (:user,:liked)'; + $this->con->executeQuery($query,array(':user'=>array($user,PDO::PARAM_STR),':liked'=>array($liked,PDO::PARAM_STR))); + return true; + } + return false; + } +} diff --git a/Sources/api_database/sources/app/gateways/UserGateway.php b/Sources/api_database/sources/app/gateways/UserGateway.php index 2ecd10d..a2d8854 100644 --- a/Sources/api_database/sources/app/gateways/UserGateway.php +++ b/Sources/api_database/sources/app/gateways/UserGateway.php @@ -21,4 +21,4 @@ class UserGateway $query = 'INSERT INTO users VALUES (:idDafl,:idSpotify,:passw)'; $this->con->executeQuery($query, array(':idDafl' => array($idDafl, PDO::PARAM_STR), ':idSpotify' => array($idSpotify, PDO::PARAM_STR), ':passw' => array($passw, PDO::PARAM_STR))); } -} \ No newline at end of file +} diff --git a/Sources/api_database/sources/app/routes.php b/Sources/api_database/sources/app/routes.php index 5504d9f..e94f017 100644 --- a/Sources/api_database/sources/app/routes.php +++ b/Sources/api_database/sources/app/routes.php @@ -54,15 +54,24 @@ $app->delete('/users/{id}', function (Request $request, Response $response, arra return $response; }); - + */ // Like someone $app->post('/user/{id}/like', function (Request $request, Response $response, array $args) { - $res = "User " . $args['id'] . " liked " . $args['liked']; - $response->getBody()->write($res); - - return $response; + try { + $mdl = new Model(); + $data = $request->getParsedBody(); + if (!isset($data['liked'])) { + throw new Exception("missing arguments"); + } + $res=($mdl->like($args["id"],$data["liked"])) ? "Match" : "Ok"; + } catch (Exception $e) { + $res = array("Error: " . $e->getMessage()); + } finally { + $response->getBody()->write(json_encode($res)); + return $response; + } }); - +/* // Add a new song as a preference for a situation $app->post('/users/{id}/preferences', function (Request $request, Response $response, array $args) { $res = "User " . $args['id'] . " add music " . $args['music'] . " to his preferences for category " . $args['categ']; @@ -70,4 +79,4 @@ $app->post('/users/{id}/preferences', function (Request $request, Response $resp return $response; }); -*/ + */