Skip to content

Commit

Permalink
Update and Delete
Browse files Browse the repository at this point in the history
  • Loading branch information
douglas-88 committed Jan 25, 2020
1 parent 8d49f4f commit ed8a654
Show file tree
Hide file tree
Showing 15 changed files with 162 additions and 100 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)
{

$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;
}

}
6 changes: 6 additions & 0 deletions app/Functions/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
use Core\Flash;
use Core\Redirect;

function dd($data){

var_dump($data);
exit;

}

function json($data) {
header('Content-Type: application/json');
Expand Down
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
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,5 @@
"server":"php -S localhost:8080 -t public"
},
"require-dev": {
"symfony/var-dumper": "^5.0"
}
}
80 changes: 2 additions & 78 deletions 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 %}
Loading

0 comments on commit ed8a654

Please sign in to comment.