Skip to content

Commit

Permalink
Merge pull request #10 from douglas-88/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
douglas-88 authored Jan 25, 2020
2 parents 76ed202 + ed8a654 commit c6f6c15
Show file tree
Hide file tree
Showing 15 changed files with 186 additions and 46 deletions.
55 changes: 46 additions & 9 deletions app/Controller/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,13 @@ public function __construct()
*/
public function index(){


$this->view("home",
[
"users" => $this->users->all(),
"title" => "Listando Usuários"
]
);


}

/**
Expand Down Expand Up @@ -69,10 +67,9 @@ public function store(){
exit;
}

$user = new User();
$user->create($data);
$this->users->create($data);

if(empty($user->getErros())){
if(empty($this->users->getErros())){
flash("message",success("Dados cadastrados com sucesso."));
Redirect::redirect("/");
exit;
Expand All @@ -91,27 +88,67 @@ public function store(){
*/
public function edit($request,$response,$args)
{
dd($args);
$user = (array) $this->users->select()->where("id",$args["id"])->first();

foreach($user as $field => $value){
flash("post_".$field,$user[$field]);
}

$this->view("editar");
}


/**
* Processa o formulário de edição de usuário
* Método de requisição: POST
*/
public function update()
public function update($request,$response,$args)
{

$validate = new Validate();
$data = $validate->validate([
"nome" => "required:max@20",
"email" => "required:email",
"telefone" => "required"
]);

if($validate->hasErros()){
foreach($data as $field => $value){
flash("post_".$field,$data[$field]);
}
back();
exit;
}

$this->users->find("id",$args["id"])->update($data);


if(empty($this->users->getErros())){
flash("message",success("Dados atualizados com sucesso."));
Redirect::redirect("/");
exit;
}else{
flash("message",error("<span class='font-weight-bold'>Falha ao atualizar</span>: ". $this->users->getErros()));
Redirect::redirect("/users/edit/{$args["id"]}");
exit;
}
}


/**
* Remove um usuário
* Método de requisição: POST
*/
public function remove($id)
public function delete($request,$response,$args)
{

$deleted = $this->users->find("id",$args["id"])->delete();
if($deleted){
flash("message",success("Dados removidos com sucesso."));
}else{
flash("message",error("Falha ao deletar dados."));
}
Redirect::redirect("/");
exit;
}

}
20 changes: 20 additions & 0 deletions app/traits/Delete.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php


namespace App\traits;


trait Delete
{
public function delete(){
$this->sql = "DELETE FROM {$this->table} WHERE {$this->field} =:{$this->field}";
$this->binds = [
$this->field => $this->value
];

$delete = $this->connection->prepare($this->sql);
$delete->execute($this->binds);

return $delete->rowCount();
}
}
33 changes: 33 additions & 0 deletions app/traits/Update.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php


namespace App\traits;


trait Update
{
public function update(array $attributes):object{
$this->sql = "UPDATE {$this->table} SET ";

foreach ($attributes as $field => $value){
$this->sql .= "{$field} =:{$field},";
}

$this->sql = rtrim($this->sql,",");
$this->sql.= " WHERE {$this->field} =:{$this->field}";

$attributes["id"] = $this->value;
$this->binds = $attributes;

$update = $this->connection->prepare($this->sql);
try {
$update->execute($this->binds);
$this->lastCreated = $this->connection->lastInsertId();
}catch(\PDOException $e){
$this->setErros($e->getMessage());
} finally {
return $this;
}

}
}
2 changes: 1 addition & 1 deletion app/traits/Validations.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ protected function max($field,$param){
protected function unique($field,$model){
$model = "app\\Model\\".ucfirst($model);
$model = new $model;
$unique = $model->where($field,$_POST[$field]);
$unique = $model->select()->where($field,$_POST[$field])->first();
if($unique){
$this->erros[$field][] = flash($field, error("Já existe um cadastro com esse valor."));
}
Expand Down
4 changes: 2 additions & 2 deletions app/traits/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ trait View{
protected function twig(){

$loader = new FilesystemLoader(__DIR__ . "/../../views");
$this->twig = new Environment($loader);

$this->twig = new Environment($loader, ['debug' => true]);
$this->twig->addExtension(new \Twig\Extension\DebugExtension());
}

protected function functions(){
Expand Down
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,7 @@
},
"scripts":{
"server":"php -S localhost:8080 -t public"
},
"require-dev": {
}
}
2 changes: 1 addition & 1 deletion composer.lock

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

1 change: 1 addition & 0 deletions public/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
$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->run();
2 changes: 1 addition & 1 deletion src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

abstract class Connection
{
private $erros = null;
private $erros;
private $host;
private $dbname;
private $userdb;
Expand Down
22 changes: 15 additions & 7 deletions src/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
namespace Core;
use App\traits\Create;
use App\traits\Read;
use App\traits\Update;
use App\traits\Delete;

/**
* Class Model
Expand All @@ -12,7 +14,11 @@
class Model extends Connection
{

use Create,Read;
protected $field;
protected $value;

use Create,Read,Update,Delete;


public function all(){

Expand All @@ -24,13 +30,15 @@ public function all(){
}

public function find($field,$value){
$find = "SELECT * FROM {$this->table} WHERE {$field} =:{$field}";
$stmt = $this->connection->prepare($find);
$stmt->bindValue(":{$field}",$value);
$stmt->execute();
$find = $stmt->fetch();

return $find;
if((!isset($field) || empty($field))|| (!isset($value) || empty($value))){
throw new \Exception("Opa, favor informar um FIELD e um VALUE ao chamar o método find().");
}

$this->field = filter_var($field,FILTER_SANITIZE_STRING);
$this->value = filter_var($value,FILTER_SANITIZE_NUMBER_INT);

return $this;
}


Expand Down
2 changes: 1 addition & 1 deletion views/cadastro.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

{% block master %}

<h2>Cadastro - Sistema de Validação</h2>
<h2>Usuários - Cadastro</h2>

<hr>
<a class="btn btn-secondary align-self-end" href="/">LISTAR</a>
Expand Down
2 changes: 1 addition & 1 deletion views/contato.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{% extends 'master.html' %}

{% block master %}
<h1>Página Contati</h1>
<h1>Página Contato</h1>
{% endblock %}
33 changes: 33 additions & 0 deletions views/editar.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{% extends 'master.html' %}

{% block master %}

<h2>Usuários - Editar</h2>

<hr>
<a class="btn btn-secondary align-self-end" href="/">LISTAR</a>
{{ message("message") }}
<form action="/users/update/{{ sent('id') }}" method="post">
<div class="form-group">
<label for="">Name</label>
<input type="text" name="nome" class="form-control" value="{{ sent('nome') }}">
{{ erros("nome") }}
</div>

<div class="form-group">
<label for="">Email</label>
<input type="text" name="email" class="form-control" value="{{ sent('email') }}">
{{ erros("email") }}
</div>

<div class="form-group">
<label for="">Telefone</label>
<input type="text" name="telefone" class="form-control" value="{{ sent('telefone') }}">
{{ erros("telefone") }}
</div>

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

</form>

{% endblock %}
51 changes: 28 additions & 23 deletions views/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,34 @@
{% block master %}
<div class="row">
<div class="col">
<h1>Usuários</h1>
<a class="btn btn-secondary align-self-end" href="/users/create" >NOVO</a>
{{ message("message") }}
<table class="table">
<thead class="thead-dark">
<tr>
<th scope="col">ID</th>
<th scope="col">NOME</th>
<th scope="col">EMAIL</th>
<th scope="col">TELEFONE</th>
</tr>
</thead>
<tbody>
{% for user in users %}
<tr>
<td>{{ user.id }}</td>
<td>{{ user.nome }}</td>
<td>{{ user.email }}</td>
<td>{{ user.telefone }}</td>
</tr>
{% endfor %}
</tbody>
</table>
<h1>Usuários</h1>
<a class="btn btn-primary my-3" href="/users/create" >NOVO</a>
{{ message("message") }}
<table class="table table-striped">
<thead class="thead-dark">
<tr>
<th scope="col">ID</th>
<th scope="col">NOME</th>
<th scope="col">EMAIL</th>
<th scope="col">TELEFONE</th>
<th scope="col">AÇÔES</th>
</tr>
</thead>
<tbody>
{% for user in users %}
<tr>
<td>{{ user.id }}</td>
<td>{{ user.nome }}</td>
<td>{{ user.email }}</td>
<td>{{ user.telefone }}</td>
<td>
<a href="/users/edit/{{ user.id }}" class="btn btn-warning">EDITAR</a>
<a href="/users/delete/{{ user.id }}" class="btn btn-danger">EXCLUIR</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
{% endblock %}
1 change: 1 addition & 0 deletions views/master.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<link rel='stylesheet' href='https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0/css/bootstrap.min.css'/>
</head>
<body>

<div class="container">
{% block master %}{% endblock %}
</div>
Expand Down

0 comments on commit c6f6c15

Please sign in to comment.