Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.idea/
backend/vendor/
backend/composer.lock
File renamed without changes.
19 changes: 19 additions & 0 deletions backend/app/controlador.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

require_once __DIR__ . '/../vendor/autoload.php';

$app = new Silex\Application();

if (DEBUG_ENVIRONMENT) {
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

$app['debug'] = true;
}

require_once __DIR__ . '/database.php';
require_once __DIR__ . '/../src/router.php';


$app->run();
12 changes: 12 additions & 0 deletions backend/app/database.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

$app->register(new Silex\Provider\DoctrineServiceProvider(), array(
'db.options' => array(
'driver' => 'pdo_mysql',
'host' => '127.0.0.1',
'dbname' => 'test',
'user' => 'root',
'password' => 'root',
'charset' => 'utf8mb4',
),
));
11 changes: 11 additions & 0 deletions backend/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"autoload": {
"psr-4": {
"Classes\\": "src/Classes/"
}
},
"require": {
"silex/silex": "~2.0",
"doctrine/dbal": "~2.2"
}
}
79 changes: 79 additions & 0 deletions backend/src/Classes/Authentication.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php
/**
* Created by PhpStorm.
* User: admin
* Date: 01/09/17
* Time: 22:39
*/

namespace Classes;

use Classes\UserRepository;

class Authentication
{
private $app;
private $request;

private static $authentication = null;
public static function get()
{
if (self::$authentication == null) {
self::$authentication = new Authentication();
}
return self::$authentication;
}

public function init($request, $app)
{
$this->request = $request;
$this->app = $app;
}

public function authenticate()
{
$auth = $this->request->headers->get("Authorization");
$apikey = substr($auth, strpos($auth, ' '));
$apikey = trim($apikey);

$user = UserRepository::get()->getByAPIKey($apikey);
if(!$user) {
$this->app->abort(401);
}

$last_connection = $user['last_connection'];
$last_connection = date_create_from_format("Y-m-d H:i:s",$last_connection);
$date = new \DateTime('now');


// if($date->getTimestamp() - $last_connection->getTimestamp() > 1*60*60){
// $this->app->abort(401);
// }


UserRepository::get()->updateLastConnetion( $user["id"]);

$this->request->attributes->set("id_user",$user["id"]);
}


public function login()
{
$data = json_decode($this->request->getContent(),true);

$email = $data['email'];
$password = md5($data['password']);

$user = UserRepository::get()->getByEmailPassword($email, $password);
if(!$user) {
$this->app->abort(401);
}

$apikey= uniqid("",true).uniqid("",true);
UserRepository::get()->updateApiKey($apikey, $user["id"]);
UserRepository::get()->updateLastConnetion( $user["id"]);

return json_encode(array("apikey" => $apikey));

}
}
128 changes: 128 additions & 0 deletions backend/src/Classes/ProductRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
<?php
/**
* Created by PhpStorm.
* User: admin
* Date: 01/09/17
* Time: 22:39
*/

namespace Classes;

use Symfony\Component\HttpFoundation\Response;

class ProductRepository
{
private $app;
private $request;


private static $authentication = null;

/**
* @return ProductRepository
*/
public static function get()
{
if (self::$authentication == null) {
self::$authentication = new ProductRepository();
}
return self::$authentication;
}

public function init($request, $app)
{
$this->request = $request;
$this->app = $app;
}


public function listAll()
{
$sql = "SELECT * FROM `tb_product` WHERE user_id = ? ";
$post = $this->app['db']->fetchAll($sql, array(
$this->request->attributes->get("id_user")
));

return $post;
}


public function getOne($product_id)
{
$sql = "SELECT * FROM `tb_product` WHERE user_id = ? AND id = ? ";
$post = $this->app['db']->fetchAssoc($sql, array(
$this->request->attributes->get("id_user"),
$product_id
));

return $post;
}

public function addOne($product)
{
$date = new \DateTime('now');
$sql = "INSERT INTO `tb_product` (`user_id`, `title`, `cantidade`, `description`, `created_at`) VALUES (?, ?, ?, ?, ?);";
$post = $this->app['db']->executeUpdate($sql, array(
(int)$this->request->attributes->get("id_user"),
$product['title'],
(int)$product['cantidade'],
$product['description'],
$date->format('Y-m-d H:i:s')
));

$sql = "SELECT * FROM `tb_product` WHERE user_id = ? ORDER BY created_at DESC";
$post = $this->app['db']->fetchAssoc($sql, array(
$this->request->attributes->get("id_user")
));

return $post;
}

public function updateOne($product_id, $product)
{
$string_keys = "";
$parameters_value = array();
foreach ($product as $key => $value) {
if($string_keys!="") $string_keys.=" , ";
$string_keys .= " `" . $key . "` = ? ";
$parameters_value[] = $value;
}


if (count($parameters_value) > 0) {
$parameters_value[] = (int)$product_id;
$parameters_value[] = $this->request->attributes->get("id_user");
$sql = "UPDATE tb_product SET ".$string_keys." WHERE id = ? AND user_id = ?";
$post = $this->app['db']->executeUpdate($sql, $parameters_value);
}
$sql = "SELECT * FROM `tb_product` WHERE id = ? AND user_id = ?";
$post = $this->app['db']->fetchAssoc($sql, array(
(int)$product_id,
$this->request->attributes->get("id_user"),
));

return $post;
}

public function removeOne($product_id)
{

$sql = "SELECT * FROM `tb_product` WHERE id = ? AND user_id = ?";
$post = $this->app['db']->fetchAssoc($sql, array(
(int)$product_id,
$this->request->attributes->get("id_user"),
));

if(!$post){
return new Response('', 400);
}
$sql = "DELETE FROM `tb_product` WHERE id = ? AND user_id = ?";
$post = $this->app['db']->executeUpdate($sql, array(
(int)$product_id,
$this->request->attributes->get("id_user"),
));

return new Response('', 204);
}

}
73 changes: 73 additions & 0 deletions backend/src/Classes/UserRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php
/**
* Created by PhpStorm.
* User: admin
* Date: 01/09/17
* Time: 22:39
*/

namespace Classes;

class UserRepository
{
private $app;
private $request;


private static $authentication = null;

/**
* @return UserRepository
*/
public static function get()
{
if (self::$authentication == null) {
self::$authentication = new UserRepository();
}
return self::$authentication;
}

public function init($request, $app)
{
$this->request = $request;
$this->app = $app;
}

public function getByAPIKey($apikey)
{
$sql = "SELECT * FROM `tb_user` WHERE apikey = ?";
$post = $this->app['db']->fetchAssoc($sql, array($apikey));

return $post;
}

public function getByEmailPassword($email, $password)
{
$sql = "SELECT * FROM `tb_user` WHERE email = ? AND password = ?";
$post = $this->app['db']->fetchAssoc($sql, array($email, $password));

return $post;
}

public function updateLastConnetion($id_usuario)
{
$date = new \DateTime('now');
$sql = "UPDATE tb_user SET last_connection = ? WHERE id = ?";
$post = $this->app['db']->executeUpdate($sql, array(
$date->format('Y-m-d H:i:s'),
(int) $id_usuario
));

return $post;
}
public function updateApiKey($apikey, $id_usuario)
{
$sql = "UPDATE tb_user SET apikey = ? WHERE id = ?";
$post = $this->app['db']->executeUpdate($sql, array(
$apikey,
(int) $id_usuario
));

return $post;
}
}
47 changes: 47 additions & 0 deletions backend/src/router.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

use Classes\Authentication;
use Classes\UserRepository;
use Classes\ProductRepository;
use Symfony\Component\HttpFoundation\Request;

$app->before(function(Request $request, $app) {

UserRepository::get()->init($request, $app);
Authentication ::get() ->init($request, $app);
ProductRepository::get()->init($request, $app);
if($request->get('_route') != 'login'){
Authentication::get()->authenticate();
}

});


$app->post('/login', function(Request $request) use ($app) {
header('Content-Type: application/json');
return Authentication::get()->login();
})->bind('login');

$app->get('/product', function(Request $request) use ($app) {
header('Content-Type: application/json');
return json_encode(ProductRepository::get()->listAll());
});

$app->get('/product/{product_id}', function($product_id) use ($app) {
header('Content-Type: application/json');
return json_encode(ProductRepository::get()->getOne($product_id));
});
$app->post('/product', function(Request $request) use ($app) {
$product = json_decode($request->getContent(),true);
header('Content-Type: application/json');
return json_encode(ProductRepository::get()->addOne($product));
});
$app->put('/product/{product_id}', function(Request $request, $product_id) use ($app) {
$product = json_decode($request->getContent(),true);
header('Content-Type: application/json');
return json_encode(ProductRepository::get()->updateOne($product_id,$product));
});

$app->delete('/product/{product_id}', function(Request $request, $product_id) use ($app) {
return ProductRepository::get()->removeOne($product_id);
});
Loading