Skip to content

Commit

Permalink
Merge pull request #11 from douglas-88/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
douglas-88 authored Feb 5, 2020
2 parents c6f6c15 + f4e8313 commit 803b65b
Show file tree
Hide file tree
Showing 10 changed files with 240 additions and 29 deletions.
2 changes: 1 addition & 1 deletion app/Controller/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function index(){

$this->view("home",
[
"users" => $this->users->all(),
"users" => $this->users->select()->get(),
"title" => "Listando Usuários"
]
);
Expand Down
2 changes: 1 addition & 1 deletion app/Model/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@

class User extends Model
{
protected $table = "users";
protected $table = "user";
}
38 changes: 30 additions & 8 deletions app/traits/Read.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,57 @@



use App\Model\User;

trait Read
{
private $sql;
private $binds;

public function select($fields = "*"):object{
/**
* Método responsável por
* @param string $fields
* @return User
*/
public function select($fields = "*"):User{
$this->sql = "SELECT {$fields} FROM {$this->table}";

return $this;
}

public function bindAndExecute(){
/**
* Método responsável por Preparar as Instruções SQL e Fazer o Bind
* @return \PDOStatement
*/
public function bindAndExecute():\PDOStatement{
$select = $this->connection->prepare($this->sql);
$select->execute($this->binds);

return $select;
}

/**
* Método responsável por obter apenas o 1º Registro encontrado.
* @return object
*/
public function first(){
$select = $this->bindAndExecute();
return $select->fetch();
}

public function get(){

/**
* Método responsável por obter todos os registros.
* @return array
*/
public function get():array{
$select = $this->bindAndExecute();
return $select->fetchAll();
}

public function where(){
/**
* Melhorar este método para que possa aceitar a o operador AND, por exemplo:
* WHERE field =:field AND field2 =:field2 ...
* @return $this
* @throws \Exception
*/
public function where():User{
$num_args = func_num_args();
$args = func_get_args();

Expand Down
12 changes: 11 additions & 1 deletion bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,21 @@

use Slim\App;
use Dotenv\Dotenv;
use Core\Whoops;


$dotenv = Dotenv::createImmutable(__DIR__);
$dotenv->load();

$config['displayErrorDetails'] = true;
$config['addContentLengthHeader'] = false;

$app = new App(['settings' => $config]);
$app = new App(['settings' => $config]);
$container = $app->getContainer();

$container['UserController'] = function ($container) {
$service = new \App\Controller\UserController;
return $service;
};
$whoops = new Whoops();
$whoops->run($container);
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"require": {
"slim/slim": "3.9",
"twig/twig": "^3.0",
"vlucas/phpdotenv": "^4.1"
"vlucas/phpdotenv": "^4.1",
"dopesong/slim-whoops": "^2.3"
},
"autoload": {
"psr-4": {
Expand Down
159 changes: 158 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 7 additions & 6 deletions public/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

require __DIR__ . "/../bootstrap.php";

$app->get("/","App\Controller\UserController:index");
$app->get("/users/create","App\Controller\UserController:create");
$app->post("/users/store","App\Controller\UserController:store");
$app->get("/users/edit/{id}","App\Controller\UserController:edit");
$app->post("/users/update/{id}","App\Controller\UserController:update");
$app->get("/users/delete/{id}","App\Controller\UserController:delete");
$app->get("/","UserController:index");
$app->get("/users/create","UserController:create");
$app->post("/users/store","UserController:store");
$app->get("/users/edit/{id}","UserController:edit");
$app->post("/users/update/{id}","UserController:update");
$app->get("/users/delete/{id}","UserController:delete");


$app->run();
11 changes: 2 additions & 9 deletions src/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,12 @@ class Model extends Connection

protected $field;
protected $value;
protected $sql;
protected $binds;

use Create,Read,Update,Delete;


public function all(){

$sql = "SELECT * FROM {$this->table}";
$stmt = $this->connection->prepare($sql);
$stmt->execute();

return $stmt->fetchAll();
}

public function find($field,$value){

if((!isset($field) || empty($field))|| (!isset($value) || empty($value))){
Expand Down
27 changes: 27 additions & 0 deletions src/Whoops.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php


namespace Core;
use Dopesong\Slim\Error\Whoops as WhoopsError;

class Whoops extends WhoopsError
{
private function slim($container){

$container['errorHandler'] = function(){
return $this;
};
}

private function php(){

$this->pushHandler(new \Whoops\Handler\PrettyPageHandler);
$this->whoops->register();
}

public function run($app){
$this->slim($app);
$this->php();
}

}
2 changes: 1 addition & 1 deletion views/editar.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ <h2>Usuários - Editar</h2>
{{ erros("telefone") }}
</div>

<button type="submit" class="btn btn-success">Cadastrar</button>
<button type="submit" class="btn btn-success">Editar</button>

</form>

Expand Down

0 comments on commit 803b65b

Please sign in to comment.