Skip to content

Commit

Permalink
Merge pull request #25 from douglas-88/dev
Browse files Browse the repository at this point in the history
CRUD POST's
  • Loading branch information
douglas-88 authored Apr 5, 2020
2 parents 70ef1f5 + 9c80dbb commit f8bf335
Show file tree
Hide file tree
Showing 30 changed files with 476 additions and 81 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.idea
.env
vendor
vendor
/public/images/uploads/
40 changes: 20 additions & 20 deletions app/Controller/Admin/CategoryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Core\Controller;
use Slim\Http\Request;
use Slim\Http\Response;
use App\Model\Category;
use App\Model\Categories;
use Core\Validate;
use Core\Redirect;

Expand All @@ -15,7 +15,7 @@ class CategoryController extends Controller {
*/
public function index() {

$category = new Category;
$category = new Categories;
$categories = $category->select()->busca("name")->paginate(3)->get();
$this->view("admin/categories/index", [
"template_admin" => $this->templateAdmin,
Expand All @@ -37,19 +37,16 @@ public function create() {
*/
public function store(Request $request, Response $response) {
//$data = filter_input_array(($_SERVER['REQUEST_METHOD'] == "POST") ? INPUT_POST : INPUT_GET, FILTER_SANITIZE_STRING);
$validate = new Validate();
$validate = new Validate($_POST);
$data = $validate->validate([
"name" => "required"
]);

if($validate->hasErros()){
foreach($data as $field => $value){
flash("post_".$field,$data[$field]);
}
back();
return $this->view("admin/categories/create", ["template_admin" => $this->templateAdmin,"post" => $_POST]);
}

$category = new Category;
$category = new Categories;
$category->create($data);
if($category->lastCreated > 0){
flash("name",success("Cadastrado com sucesso"));
Expand All @@ -68,7 +65,7 @@ public function show($id) {
* Exibe o formulário de edição
*/
public function edit(Request $request, Response $response, $args) {
$category = new Category;
$category = new Categories;
$category = $category->select()->where2(["id","=",$args["id"]])->first();
return $response->write($this->view("admin/categories/edit", [
"template_admin" => $this->templateAdmin,
Expand All @@ -82,30 +79,33 @@ public function edit(Request $request, Response $response, $args) {
*/
public function update(Request $request, Response $response, $args) {

$validate = new Validate();
$validate = new Validate($_POST);
$data = $validate->validate([
"name" => "required"
]);


$category = new Categories;

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

$category = new Category;
$category->update2(["name" => $data["name"]])->where2(["id","=",$args["id"]])->exec();
return Redirect::redirect("/painel/admin/category");

$category = $category->select()->where2(["id","=",$args["id"]])->first();
return $response->write($this->view("admin/categories/edit", [
"template_admin" => $this->templateAdmin,
"category" => $category,
"post" => $_POST
]));

}
$category->update2(["name" => $data["name"]])->where2(["id","=",$args["id"]])->exec();
return Redirect::redirect("/painel/admin/category");
}

/**
* Remove dados do Banco
*/
public function destroy(Request $request, Response $response, $args) {
$category = new Category();
$category = new Categories();
$category->delete2()->where2(["id","=",$args["id"]])->exec();

return Redirect::redirect("/painel/admin/category");
Expand Down
2 changes: 1 addition & 1 deletion app/Controller/Admin/LoginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function create()
*/
public function store(Request $request, Response $response)
{
$validate = new Validate();
$validate = new Validate($_POST);
$data = $validate->validate([
"email" => "required:email",
"password" => "required"
Expand Down
105 changes: 97 additions & 8 deletions app/Controller/Admin/PostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,75 @@

namespace App\Controller\Admin;

use App\Model\Categories;
use App\Model\User;
use Core\Controller;
use Core\Image;
use Core\Redirect;
use Core\Validate;
use Slim\Http\Request;
use Slim\Http\Response;
use App\Model\Post;

class PostController extends Controller {
/**
* Página Inicial
*/
public function index() {

$this->view("admin/post/index", ["template_admin" => $this->templateAdmin]);
$posts = new Post();

$this->view("admin/post/index", [
"template_admin" => $this->templateAdmin,
"posts" => $posts->list(),
"links" => $posts->links()
]);

}

/**
* Exibe o formulário de criação
*/
public function create() {
$this->view("admin/post/create", ["template_admin" => $this->templateAdmin]);

$categories = new Categories();
$categories = $categories->getAll();

$this->view("admin/post/create",
[
"template_admin" => $this->templateAdmin,
"categories" => $categories
]);
}

/**
* Processa Formulário de criação
*/
public function store(Request $request, Response $response) {
echo 'store';


$validate = new Validate($_POST);
$data = $validate->validate([
"title" => "required",
"content" => "required",
"category_id" => "required"
]);
if($validate->hasErros()){
foreach($data as $field => $value){
flash("post_".$field,$data[$field]);
}
back();
}

$post = new Post;
$image = new Image("thumbnail");
$data["thumbnail"] = $image->size("capa")->upload();
$data["user_id"] = (new User)->user()->id;
$post->create($data);
if($post->lastCreated > 0){
flash("name",success("Cadastrado com sucesso"));
Redirect::redirect("/painel/admin/posts");
}
}

/**
Expand All @@ -40,22 +83,68 @@ public function show($id) {
/**
* Exibe o formulário de edição
*/
public function edit($id) {
echo 'edit';
public function edit(Request $request, Response $response, $args) {

$bodyRequest = $request->getParsedBody();
$categories = new Categories();
$categories = $categories->getAll();

$post = new Post();
$post = $post->select()->where2(["id","=",$args["id"]])->first();

if(!is_null($bodyRequest)){
$post = get_object_vars($post);
foreach ($post as $key => $value){
if(!key_exists($key,$bodyRequest)){
$bodyRequest[$key] = $value;
}
}
}

$this->view("admin/post/edit",
[
"template_admin" => $this->templateAdmin,
"categories" => $categories,
"post" => (is_null($bodyRequest)) ? $post : $bodyRequest
]);
}

/**
* Processa o formulário de edição
*/
public function update(Request $request, Response $response, $args) {
echo 'update';
$validate = new Validate($request->getParsedBody());
$data = $validate->validate([
"title" => "required",
"content" => "required",
"category_id" => "required"
]);
if($validate->hasErros()){
$this->edit($request,$response,$args);
exit;
}

$post = new Post;
if(!$_FILES["thumbnail"]["error"]){
$image = new Image("thumbnail");
$data["thumbnail"] = $image->size("capa")->upload();
}
$data["user_id"] = (new User)->user()->id;
$result = $post->update2($data)->where2(["id","=", $args["id"]])->exec();
if($result){
Redirect::redirect("/painel/admin/posts");
}

}

/**
* Remove dados do Banco
*/
public function destroy($id) {
echo 'destroy';
public function destroy(Request $request, Response $response, $args) {
$post = new Post;
$post->delete2()->where2(["id","=",$args["id"]])->exec();

return Redirect::redirect("/painel/admin/posts");
}

}
1 change: 0 additions & 1 deletion app/Functions/twig.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

$sent = new TwigFunction("sent",function($index){
echo Flash::get("post_".$index);

});

$message = new TwigFunction("message",function($index) {
Expand Down
2 changes: 1 addition & 1 deletion app/Model/Category.php → app/Model/Categories.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Core\Model;

class Category extends Model {
class Categories extends Model {
protected $table = "categories";

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

namespace App\Model;

use Core\Model;

class Post extends Model {
protected $table = "posts";

public function list(){
return $this->select("posts.id,
posts.title,
posts.content,
posts.thumbnail as capa,
posts.status as status,
categories.name as category,
users.name as user,
posts.created_at as criacao")
->join("categories","posts.category_id","categories.id")
->join("users","users.id","posts.user_id")
->order("order by posts.id desc")
->busca("title,content,status")
->paginate(1)
->get();
}
}
Loading

0 comments on commit f8bf335

Please sign in to comment.