<?php

require_once '../class/Note.php';
require_once '../class/Security.php';



$app->get('/notes', function ($request, $response,$args) {
    $db = Db::getInstance();
    $conn = $db->getConn();
    $userToken = $request->getHeader("Authorization")[0];
    $se = new Security();
    $userId = $se->decode($userToken);

    $note = new Note($conn, $userId);
    $res = $note->getNotes($request->getParams());
    
     //return response
     $response->withHeader('Content-Type', 'application/json');
     $response->write(json_encode($res));
     return $response;

    //disconnect connection
    $conn->close();
});


$app->post('/notes', function ($request, $response,$args) {
    $db = Db::getInstance();
    $conn = $db->getConn();
    $userToken = $request->getHeader("Authorization")[0];
    $se = new Security();
    $userId = $se->decode($userToken);

    $note = new Note($conn, $userId);
    $res = $note->saveNote($request->getParsedBody());
    
     //return response
     $response->withHeader('Content-Type', 'application/json');
     $response->write(json_encode($res));
     return $response;

    //disconnect connection
    $conn->close();
});

$app->get('/note', function ($request, $response,$args) {
    $db = Db::getInstance();
    $conn = $db->getConn();
    $userToken = $request->getHeader("Authorization")[0];
    $se = new Security();
    $userId = $se->decode($userToken);

    $note = new Note($conn, $userId);
    $res = $note->getNote($request->getParams());
    
     //return response
     $response->withHeader('Content-Type', 'application/json');
     $response->write(json_encode($res));
     return $response;

    //disconnect connection
    $conn->close();
});

?>
