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
1 change: 1 addition & 0 deletions .htaccess
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
RewriteCond %{REQUEST_FILENAME} !-f

# RewriteRule

</IfModule>
80 changes: 79 additions & 1 deletion Carro.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,82 @@
/**
* Classe do carro
*/
?>
class Carro {
private $id;
private $marca;
private $modelo;
private $ano;

//===============================================
public function __construct($id = null, $marca = null, $modelo = null, $ano = null){
$this->$id = $id;
$this->$marca = $marca;
$this->$modelo = $modelo;
$this->$ano = $ano;
}

//===============================================
public function getAllCarros(){
//Api
$api = new Api();
$response = $api->apiCon('GET', 'carros');
//Retornar
return $response;
}

//===============================================
public function getCarro($id){
//Api
$api = new Api();
$response = $api->apiCon('GET', 'carros', $id);
//Retornar
return $response;
}

public function getMarcas(){
//Api
$api = new Api();
$response = $api->apiCon('GET', 'marcas');
//Retornar
return $response;
}
}

/**
* Classe API
*/
class Api {

//===============================================
public function __construct(){}

//===============================================
public function apiCon($method, $loc, $id = ''){
//===============================================
$curl = curl_init();
//===============================================
curl_setopt_array($curl, array(
CURLOPT_URL => 'http://localhost:8080/'.$loc.'/'.$id,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => $method,
CURLOPT_PROXY => $_SERVER['SERVER_ADDR'] . ':' . $_SERVER['SERVER_PORT'],
CURLOPT_HTTPHEADER => array(
'Cookie: XDEBUG_SESSION=PHPSTORM'
),
));
//===============================================
$response = curl_exec($curl);
$response = json_decode($response, true);
//===============================================
if(curl_errno($curl)) { print curl_error($curl); }
curl_close($curl);
//===============================================
return $response;
}
}

76 changes: 74 additions & 2 deletions api.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,76 @@
<?php
//session_start();
header("Cache-Control: no-cache, no-store, must-revalidate");
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=utf-8");

?>
//===============================================
$parramUrl = explode('/', $_SERVER['REQUEST_URI']);
$parram = $parramUrl[2];
$loc1 = $parramUrl[1];

//echo $loc1;

$method = $_SERVER['REQUEST_METHOD'];
$contents = file_get_contents('db.json');
$json = json_decode($contents, true);
$body = file_get_contents('php://input');

//===============================================
if($method === 'POST'){
$jsonBody = json_decode($body, true);
$lastId = end($json[$loc1]);
$jsonBody['id'] = $lastId['id']+1;
$json[$loc1][] = $jsonBody;
file_put_contents('db.json', json_encode($json));
echo true;
}

//===============================================
if($method === 'GET'){
if($parram){
$jsonGetSingle = json_decode($contents, true);
$check = false;
foreach($jsonGetSingle[$loc1] as $key => $obj){
if($obj['id'] == $parram){
$check = true;
echo json_encode($jsonGetSingle[$loc1][$key]);
break;
}
}
if($check == false){ echo 'Nada encontrado'; }
} else {
$contents = json_decode($contents, true);
echo json_encode($contents[$loc1]);
}
}

//===============================================
if($method === 'PUT'){
$item = false;
foreach($json[$loc1] as $key => $value){
if($value['id'] == $parram){
$item = $key;
break;
}
}
$jsonBody = json_decode($body, true);
$jsonBody['id'] = $parram;
$json[$loc1][$item] = $jsonBody;
file_put_contents('db.json', json_encode($json));
echo true;
}

//===============================================
if($method === 'DELETE'){
$item = false;
foreach($json[$loc1] as $key => $value){
if($value['id'] == $parram){
$item = $key;
break;
}
}
unset($json[$loc1][$item]);
//$json[$loc1] = array_values($json[$loc1]);
file_put_contents('db.json', json_encode($json));
echo true;
}
107 changes: 107 additions & 0 deletions css/main.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
body {
margin: 0;
padding: 0;
font-family: sans-serif;
}

.w-100 {
width: 100%;
}

.mb-10 {
margin-bottom: 10px;
}

.mb-20 {
margin-bottom: 20px;
}

input:active,
input:visited,
input:focus {
outline: none;
}

.text-right {
text-align: right;
}

.c-pointer {
cursor: pointer;
}

.d-flex {
display: flex;
}

.f-direction-column {
flex-direction: column;
}

.j-content-center {
justify-content: center;
}

.j-content-space-between {
justify-content: space-between;
}

.j-content-flex-end {
justify-content: flex-end;
}

.al-items-center {
align-items: center;
}

.container-geral {
width: 100vw;
min-height: 100vh;
/* Permalink - use to edit and share this gradient: https://colorzilla.com/gradient-editor/#c5deea+0,8abbd7+31,066dab+100;Web+2.0+Blue+3D+%231 */
background: #c5deea; /* Old browsers */
background: -moz-linear-gradient(top, #c5deea 0%, #8abbd7 31%, #066dab 100%); /* FF3.6-15 */
background: -webkit-linear-gradient(top, #c5deea 0%,#8abbd7 31%,#066dab 100%); /* Chrome10-25,Safari5.1-6 */
background: linear-gradient(to bottom, #c5deea 0%,#8abbd7 31%,#066dab 100%); /* W3C, IE10+, FF16+, Chrome26+, Opera12+, Safari7+ */
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#c5deea', endColorstr='#066dab',GradientType=0 ); /* IE6-9 */
}

.container {
padding: 25px;
background: #fff;
max-width: 400px;
width: 100%;
border-radius: 10px
}

.container-form input,
.container-form select {
margin-bottom: 10px;
padding: 10px;
border: 0;
border-bottom: 1px solid #ddd
}

.btn {
width: 100%;
padding: 10px;
border: none;
}

.container-form input[type='submit'],
.btn {
background: #0a4f76;
color: #fff;
border-radius: 10px;
transition: 0.5s all;
cursor: pointer;
}

.container-form input[type='submit']:hover,
.btn:hover {
background: #1878af;
}

.line-carro {
border-bottom: 1px solid #000;
padding: 10px;
}
44 changes: 44 additions & 0 deletions db.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"carros": [
{
"id": "1",
"marca": "Audi",
"modelo": "A4",
"ano": "2020"
},
{
"id": "2",
"marca": "Mercedes",
"modelo": "A190",
"ano": "2002"
},
{
"id": "3",
"marca": "BMW",
"modelo": "X1",
"ano": "2018"
}
],
"marcas": [
{
"id": "1",
"nome": "Mercedes"
},
{
"id": "2",
"nome": "BMW"
},
{
"id": "3",
"nome": "Audi"
},
{
"id": "4",
"nome": "Ferrari"
},
{
"id": "5",
"nome": "Fiat"
}
]
}
10 changes: 8 additions & 2 deletions docker/nginx/default.conf
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ server {
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /var/www;
location /carros/ {
try_files $uri $uri/ /api.php?$args;
}
location /marcas/ {
try_files $uri $uri/ /api.php?$args;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
Expand All @@ -18,7 +24,7 @@ server {
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_read_timeout 3000;
}




}

25 changes: 23 additions & 2 deletions index.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,24 @@
<?php

echo "Hello World!";
//phpinfo();
?>
<!DOCTYPE html>
<html lang="pt">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="js/main.js"></script>
<script src="https://kit.fontawesome.com/69e294efb4.js"></script>
<link rel="stylesheet" href="css/main.css">
<title>MINI API PHP</title>
</head>
<body>
<div class="container-geral j-content-center al-items-center d-flex">
<div class="container">
<div id="content"></div>
<div id="msg"></div>
</div>
</div>
</body>
</html>
Loading