Skip to content

Commit d535d8c

Browse files
committed
v1.0
1 parent 4bad099 commit d535d8c

14 files changed

+900
-0
lines changed

.htaccess

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
3+
RewriteEngine On
4+
RewriteCond %{SCRIPT_FILENAME} !-f
5+
RewriteCond %{SCRIPT_FILENAME} !-d
6+
SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1
7+
RewriteRule ^(.*)$ index.php

Classes/DB/MySQL.php

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<?php
2+
3+
namespace DB;
4+
5+
use InvalidArgumentException;
6+
use PDO;
7+
use PDOException;
8+
use Util\ConstantesGenericasUtil;
9+
10+
class MySQL
11+
{
12+
private object $db;
13+
14+
/**
15+
* MySQL constructor.
16+
*/
17+
public function __construct()
18+
{
19+
$this->db = $this->setDB();
20+
}
21+
22+
/**
23+
* @return PDO
24+
*/
25+
public function setDB()
26+
{
27+
try {
28+
return new PDO(
29+
'mysql:host=' . HOST . '; dbname=' . BANCO . ';', USUARIO, SENHA
30+
);
31+
} catch (PDOException $exception) {
32+
throw new PDOException($exception->getMessage());
33+
}
34+
}
35+
36+
/**
37+
* @param $tabela
38+
* @param $id
39+
* @return string
40+
*/
41+
public function delete($tabela, $id)
42+
{
43+
$consultaDelete = 'DELETE FROM ' . $tabela . ' WHERE id = :id';
44+
if ($tabela && $id) {
45+
$this->db->beginTransaction();
46+
$stmt = $this->db->prepare($consultaDelete);
47+
$stmt->bindParam(':id', $id);
48+
$stmt->execute();
49+
if ($stmt->rowCount() > 0) {
50+
$this->db->commit();
51+
return ConstantesGenericasUtil::MSG_DELETADO_SUCESSO;
52+
}
53+
$this->db->rollBack();
54+
throw new InvalidArgumentException(ConstantesGenericasUtil::MSG_ERRO_SEM_RETORNO);
55+
}
56+
throw new InvalidArgumentException(ConstantesGenericasUtil::MSG_ERRO_GENERICO);
57+
}
58+
59+
/**
60+
* @param $tabela
61+
* @return array
62+
*/
63+
public function getAll($tabela)
64+
{
65+
if ($tabela) {
66+
$consulta = 'SELECT * FROM ' . $tabela;
67+
$stmt = $this->db->query($consulta);
68+
$registros = $stmt->fetchAll($this->db::FETCH_ASSOC);
69+
if (is_array($registros) && count($registros) > 0) {
70+
return $registros;
71+
}
72+
}
73+
throw new InvalidArgumentException(ConstantesGenericasUtil::MSG_ERRO_SEM_RETORNO);
74+
}
75+
76+
/**
77+
* @param $tabela
78+
* @param $id
79+
* @return mixed
80+
*/
81+
public function getOneByKey($tabela, $id)
82+
{
83+
if ($tabela && $id) {
84+
$consulta = 'SELECT * FROM ' . $tabela . ' WHERE id = :id';
85+
$stmt = $this->db->prepare($consulta);
86+
$stmt->bindParam(':id', $id);
87+
$stmt->execute();
88+
$totalRegistros = $stmt->rowCount();
89+
if ($totalRegistros === 1) {
90+
return $stmt->fetch($this->db::FETCH_ASSOC);
91+
}
92+
throw new InvalidArgumentException(ConstantesGenericasUtil::MSG_ERRO_SEM_RETORNO);
93+
}
94+
95+
throw new InvalidArgumentException(ConstantesGenericasUtil::MSG_ERRO_ID_OBRIGATORIO);
96+
}
97+
98+
/**
99+
* @return object|PDO
100+
*/
101+
public function getDb()
102+
{
103+
return $this->db;
104+
}
105+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace Repository;
4+
5+
use DB\MySQL;
6+
use InvalidArgumentException;
7+
use Util\ConstantesGenericasUtil;
8+
9+
class TokensAutorizadosRepository{
10+
11+
private object $MySQL;
12+
public const TABELA = 'tokens_autorizados';
13+
14+
/**
15+
* UsuariosRepository constructor.
16+
*/
17+
public function __construct(){
18+
19+
$this->MySQL = new MySQL();
20+
}
21+
22+
/**
23+
* @param $token
24+
*/
25+
public function validarToken($token){
26+
27+
$token = str_replace([' ', 'Bearer'], '', $token);
28+
29+
if ($token) {
30+
$consultaToken = 'SELECT id FROM ' . self::TABELA . ' WHERE token = :token AND status = :status';
31+
$stmt = $this->getMySQL()->getDb()->prepare($consultaToken);
32+
$stmt->bindValue(':token', $token);
33+
$stmt->bindValue(':status', ConstantesGenericasUtil::SIM);
34+
$stmt->execute();
35+
if ($stmt->rowCount() !== 1) {
36+
header("HTTP/1.1 401 Unauthorized");
37+
throw new InvalidArgumentException(ConstantesGenericasUtil::MSG_ERRO_TOKEN_NAO_AUTORIZADO);
38+
}
39+
} else {
40+
throw new InvalidArgumentException(ConstantesGenericasUtil::MSG_ERRO_TOKEN_VAZIO);
41+
}
42+
}
43+
44+
/**
45+
* @return MySQL|object
46+
*/
47+
public function getMySQL(){
48+
49+
return $this->MySQL;
50+
}
51+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
namespace Repository;
4+
5+
use DB\MySQL;
6+
7+
class UsuariosRepository{
8+
9+
private object $MySQL;
10+
public const TABELA = 'usuarios';
11+
12+
/**
13+
* UsuariosRepository constructor.
14+
*/
15+
public function __construct(){
16+
17+
$this->MySQL = new MySQL();
18+
}
19+
20+
/**
21+
* @return MySQL|object
22+
*/
23+
public function getMySQL(){
24+
25+
return $this->MySQL;
26+
}
27+
28+
/**
29+
* @param $login
30+
* @return int
31+
*/
32+
public function getRegistroByLogin($login){
33+
$consulta = 'SELECT * FROM ' . self::TABELA . ' WHERE login = :login';
34+
$stmt = $this->MySQL->getDb()->prepare($consulta);
35+
$stmt->bindParam(':login', $login);
36+
$stmt->execute();
37+
return $stmt->rowCount();
38+
}
39+
40+
/**
41+
* @param $login
42+
* @param $senha
43+
* @return int
44+
*/
45+
public function insertUser($login, $senha){
46+
$consultaInsert = 'INSERT INTO ' . self::TABELA . ' (login, senha) VALUES (:login, :senha)';
47+
$this->MySQL->getDb()->beginTransaction();
48+
$stmt = $this->MySQL->getDb()->prepare($consultaInsert);
49+
$stmt->bindParam(':login', $login);
50+
$stmt->bindParam(':senha', $senha);
51+
$stmt->execute();
52+
return $stmt->rowCount();
53+
}
54+
55+
/**
56+
* @param $id
57+
* @param $login
58+
* @param $senha
59+
* @return int
60+
*/
61+
public function updateUser($id, $dados){
62+
$consultaUpdate = 'UPDATE ' . self::TABELA . ' SET login = :login, senha = :senha WHERE id = :id';
63+
$this->MySQL->getDb()->beginTransaction();
64+
$stmt = $this->MySQL->getDb()->prepare($consultaUpdate);
65+
$stmt->bindParam(':id', $id);
66+
$stmt->bindParam(':login', $dados['login']);
67+
$stmt->bindParam(':senha', $dados['senha']);
68+
$stmt->execute();
69+
return $stmt->rowCount();
70+
}
71+
72+
73+
74+
}
75+

0 commit comments

Comments
 (0)