Skip to content

Commit 2cfa794

Browse files
committed
Books now the default cmd
1 parent 0cd357b commit 2cfa794

12 files changed

Lines changed: 82 additions & 126 deletions

File tree

File renamed without changes.

bin/src/ConfirmCommand.php

Lines changed: 0 additions & 40 deletions
This file was deleted.

bin/src/ControllerCommand.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ class ControllerCommand extends Command{
1515

1616
protected function configure(){
1717
$this->setName("make:controller")
18-
->setDescription("Hashes a given string using Bcrypt")
19-
->addArgument('controllerName', InputArgument::REQUIRED, 'What is the name of the controller you wish to create? Controller name must be capitalized and Plural with ending with the suffix Controller. eg. PostsController)');
18+
->setDescription("Creates a controller file in app/Controllers")
19+
->addArgument('controllerName', InputArgument::REQUIRED, 'What is the name of the controller you wish to create? Controller name must be capitalized and Plural with ending with the suffix Controller. eg. BooksController)');
2020
}
2121

2222
protected function execute(InputInterface $input, OutputInterface $output){
@@ -28,16 +28,16 @@ protected function execute(InputInterface $input, OutputInterface $output){
2828

2929
//confirm that it contains the word controller
3030
if( strpos( $input, "Controller" ) == false ) {
31-
$output->writeln('Error: Your controller name is not named correctly. It should be in plural and should end with the word Controller. Eg. PostsController');
31+
$output->writeln('<error>Error: Your controller name is not named correctly. It should be in plural and should end with the word Controller. Eg. BooksController </error>');
3232
}else if($input{0} !== strtoupper($input{0})){
3333
//the first character does not start with an upper case
34-
$output->writeln('Error: Your controller name must start with a capital letter eg. PostsController');
34+
$output->writeln('Error: Your controller name must start with a capital letter eg. BooksController');
3535
}else if( strpos( $input, "." ) !== false ) {
36-
$output->writeln('Error: Your controller name cannot contain a dot. Here is an example of a good controller name : PostsController');
36+
$output->writeln('<fg=red>Error: Your controller name cannot contain a dot. Here is an example of a good controller name : BooksController </>');
3737
}else{
3838
$result = $make->makeController($input);
3939

40-
$output->writeln('Your controller has been created in app/Controllers/' . $input.'.php');
40+
$output->writeln('< fg=green >Your controller has been created in app/Controllers/ </>' . $input.'.php');
4141

4242
}
4343

bin/src/Make.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ class Make{
1212
*/
1313
public static function makeController( $controllerName ){
1414
//controller name must be camelcased and plural
15-
//model file name must be CamelCased and singular eg. Post
15+
//model file name must be CamelCased and singular eg. Book
1616

17-
//model file name must be CamelCased and singular eg. Post
17+
//model file name must be CamelCased and singular eg. Book
1818

1919
//get a sample copy of the text to be written to the controller config
2020
$controllerFile = file_get_contents (__DIR__ .'/templates/Controllers.php');
@@ -84,7 +84,7 @@ public static function makeView( $viewName ){
8484
* @return
8585
*/
8686
public static function makeModel( $modelName ){
87-
//model file name must be CamelCased and singular eg. Post
87+
//model file name must be CamelCased and singular eg. Book
8888

8989
//get a sample copy of the model
9090
$modelFile = file_get_contents (__DIR__ .'/templates/Models.php');

bin/src/ModelCommand.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ protected function execute(InputInterface $input, OutputInterface $output){
2626

2727
//Check if model name has a dot, throw error
2828
if( strpos( $input, "." ) !== false ) {
29-
$output->writeln('Error: Your model name cannot contain a dot. Example of a good model name: Post');
29+
$output->writeln('Error: Your model name cannot contain a dot. Example of a good model name: Book');
3030
} else if (strtolower($input[0]) == $input[0]){
3131
//the model name does not start with a capital letter
3232

33-
$output->writeln('Error: Your model name needs to start with a capital letter. Example of a good model name: Post ');
33+
$output->writeln('Error: Your model name needs to start with a capital letter. Example of a good model name: Book ');
3434
} else{
3535
$result = $make->makeModel($input);
3636
$output->writeln('Your model has been created in app/Models/' . $input.'php - here is the result '.$result);

bin/src/templates/Controllers.php

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
use App\Controllers\Controller;
55
use App\Models\User;
66
use Respect\Validation\Validator as v;
7-
use App\Models\Post;
7+
use App\Models\Book;
88

9-
class PostsController extends Controller{
9+
class BooksController extends Controller{
1010

1111
/**
1212
* List all users
@@ -15,45 +15,45 @@ class PostsController extends Controller{
1515
*/
1616
public function index($request, $response, $args){
1717

18-
//find all posts by the user with this ID
18+
//find all books by the user with this ID
1919
if(isset($args['user_id'])){
20-
$posts = Post::where('user_id',$args['user_id'] )->get();
20+
$books = Book::where('user_id',$args['user_id'] )->get();
2121
//get the user's details
2222
$user = User::find($args['user_id']);
2323

24-
return $this->view->render($response,'posts/index.twig', ['posts'=>$posts, 'user'=>$user]);
24+
return $this->view->render($response,'books/index.twig', ['books'=>$books, 'user'=>$user]);
2525
}else{
26-
$posts = Post::all();
27-
return $this->view->render($response,'posts/index.twig', ['posts'=>$posts]);
26+
$books = Book::all();
27+
return $this->view->render($response,'books/index.twig', ['books'=>$books]);
2828
}
2929

3030
}
3131

3232

3333

3434
/**
35-
* Display a post
35+
* Display a book
3636
*
3737
* @return
3838
*/
3939
public function view($request, $response, $args){
4040

41-
$post = Post::find( $args['id']);
41+
$book = Book::find( $args['id']);
4242

43-
return $this->view->render($response,'posts/view.twig', ['post'=>$post]);
43+
return $this->view->render($response,'books/view.twig', ['book'=>$book]);
4444

4545
}
4646

4747

4848

4949
/**
50-
* Create A New Post
50+
* Create A New Book
5151
*
5252
* @return
5353
*/
5454
public function add($request, $response, $args){
5555

56-
if($request->isPost()){
56+
if($request->isBook()){
5757

5858
/**
5959
* validate input before submission
@@ -70,47 +70,47 @@ public function add($request, $response, $args){
7070
if($validation->failed()){
7171
$this->flash->addMessage('error', 'Validation Failed!');
7272

73-
return $response->withRedirect($this->router->pathFor('posts/add.twig'));
73+
return $response->withRedirect($this->router->pathFor('books/add.twig'));
7474
}
7575

76-
$post = Post::create([
76+
$book = Book::create([
7777
'title' => $request->getParam('title'),
7878
'body' => $request->getParam('body'),
7979
'user_id' => $this->auth->user()->id,
8080
]);
8181

82-
$this->flash->addMessage('success', 'Post Added Successfully');
83-
//redirect to eg. posts/view/8
84-
return $response->withRedirect($this->router->pathFor('posts.view', ['id'=>$post->id]));
82+
$this->flash->addMessage('success', 'Book Added Successfully');
83+
//redirect to eg. books/view/8
84+
return $response->withRedirect($this->router->pathFor('books.view', ['id'=>$book->id]));
8585

8686
}
87-
return $this->view->render($response,'posts/add.twig');
87+
return $this->view->render($response,'books/add.twig');
8888

8989
}
9090

9191

9292

9393
/**
94-
* Edit post
94+
* Edit book
9595
*
9696
* @return
9797
*/
9898
public function edit($request, $response, $args){
9999

100-
//find the post
101-
$post = Post::find( $args['id']);
100+
//find the book
101+
$book = Book::find( $args['id']);
102102

103-
//only admin and the person that created the post can edit or delete it.
104-
if(($this->auth->user()->id != $post->user_id) OR ($this->auth->user()->role_id < 3) ){
103+
//only admin and the person that created the book can edit or delete it.
104+
if(($this->auth->user()->id != $book->user_id) OR ($this->auth->user()->role_id < 3) ){
105105

106106
$this->flash->addMessage('error', 'You are not allowed to perform this action!');
107107

108-
return $this->view->render($response,'posts/edit.twig', ['post'=>$post]);
108+
return $this->view->render($response,'books/edit.twig', ['book'=>$book]);
109109

110110
}
111111

112112
//if form was submitted
113-
if($request->isPost()){
113+
if($request->isBook()){
114114

115115
$validation = $this->validator->validate($request, [
116116
'title' => v::notEmpty(),
@@ -120,39 +120,39 @@ public function edit($request, $response, $args){
120120
if($validation->failed()){
121121
$this->flash->addMessage('error', 'Validation Failed!');
122122

123-
return $this->view->render($response,'posts/edit.twig', ['post'=>$post]);
123+
return $this->view->render($response,'books/edit.twig', ['book'=>$book]);
124124
}
125125

126126
//save Data
127-
$post = Post::where('id', $args['id'])
127+
$book = Book::where('id', $args['id'])
128128
->update([
129129
'title' => $request->getParam('title'),
130130
'body' => $request->getParam('body')
131131
]);
132132

133-
if($post){
134-
$this->flash->addMessage('success', 'Post Updated Successfully');
135-
//redirect to eg. posts/view/8
136-
return $response->withRedirect($this->router->pathFor('posts.view', ['id'=>$args['id']]));
133+
if($book){
134+
$this->flash->addMessage('success', 'Book Updated Successfully');
135+
//redirect to eg. books/view/8
136+
return $response->withRedirect($this->router->pathFor('books.view', ['id'=>$args['id']]));
137137
}
138138
}
139139

140140

141-
return $this->view->render($response,'posts/edit.twig', ['post'=>$post]);
141+
return $this->view->render($response,'books/edit.twig', ['book'=>$book]);
142142

143143
}
144144

145145

146146
/**
147-
* Delete a post
147+
* Delete a book
148148
*
149149
* @return
150150
*/
151151
public function delete($request, $response, $args){
152-
$user = Post::find( $args['id']);
152+
$user = Book::find( $args['id']);
153153
if($user->delete()){
154-
$this->flash->addMessage('success', 'Post Deleted Successfully');
155-
return $response->withRedirect($this->router->pathFor('posts.index', ['user_id'=>$this->auth->user()->id]));
154+
$this->flash->addMessage('success', 'Book Deleted Successfully');
155+
return $response->withRedirect($this->router->pathFor('books.index', ['user_id'=>$this->auth->user()->id]));
156156
}
157157
}
158158

bin/src/templates/Models.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@
1717
//use Cake\ORM\TableRegistry;
1818

1919

20-
class Post extends Model
20+
class Book extends Model
2121
{
2222

23-
//optional: define table name if different from 'posts'
24-
protected $table = 'posts';
23+
//optional: define database table name if different from 'books'
24+
protected $table = 'books';
2525

2626
protected $fillable = [
2727
'id',
@@ -31,8 +31,8 @@ class Post extends Model
3131
];
3232

3333
/**
34-
* Every post belongs to a user.
35-
* That is, posts table has a user_id column which refers to the id of the user
34+
* Every book belongs to a user.
35+
* That is, books table has a user_id column which refers to the id of the user
3636
* So let's define the relationship below
3737
*/
3838
public function user(){

bin/src/templates/Views/add.twig

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@
33
{% block content %}
44
<div class="row">
55
<div class="col-md-10">
6-
<div id="postlist">
6+
<div id="booklist">
77
<div class="panel">
88
<div class="panel-heading">
99
<div class="text-center">
1010
<div class="row">
1111
<div class="col-sm-9">
12-
<h3 class="pull-left">Create new post</h3>
12+
<h3 class="pull-left">Create new book</h3>
1313
</div>
1414
<div class="col-sm-3">
1515
<h4 class="pull-right">
16-
<small> {{ post.created_at }} </small>
16+
<small> {{ book.created_at }} </small>
1717
</h4>
1818
</div>
1919
</div>
@@ -22,7 +22,7 @@
2222

2323
<div class="panel-body">
2424
<div class="login-form" >
25-
<form action="{{ path_for('posts.add') }}" method="post" autocomplete="off">
25+
<form action="{{ path_for('books.add') }}" method="book" autocomplete="off">
2626
<div class="form-group {{ errors.title ? 'has-error' : '' }}">
2727
<input type="text" class="form-control login-field" name="title" placeholder="Enter your title" >
2828
<label class="login-field-icon fui-user" for="login-first-name"></label>

bin/src/templates/Views/edit.twig

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@
33
{% block content %}
44
<div class="row">
55
<div class="col-md-10">
6-
<div id="postlist">
6+
<div id="booklist">
77
<div class="panel">
88
<div class="panel-heading">
99
<div class="text-center">
1010
<div class="row">
1111
<div class="col-sm-9">
12-
<h3 class="pull-left">{{ post.title }}</h3>
12+
<h3 class="pull-left">{{ book.title }}</h3>
1313
</div>
1414
<div class="col-sm-3">
1515
<h4 class="pull-right">
16-
<small> {{ post.created_at }} </small>
16+
<small> {{ book.created_at }} </small>
1717
</h4>
1818
</div>
1919
</div>
@@ -22,9 +22,9 @@
2222

2323
<div class="panel-body">
2424
<div class="login-form" >
25-
<form action="{{ path_for('posts.edit', {'id': post.id}) }}" method="post" autocomplete="off">
25+
<form action="{{ path_for('books.edit', {'id': book.id}) }}" method="book" autocomplete="off">
2626
<div class="form-group {{ errors.title ? 'has-error' : '' }}">
27-
<input type="text" class="form-control login-field" value="{{ post.title }}" name="title" placeholder="Enter your title" >
27+
<input type="text" class="form-control login-field" value="{{ book.title }}" name="title" placeholder="Enter your title" >
2828
<label class="login-field-icon fui-user" for="login-first-name"></label>
2929
{% if errors.title %}
3030
<span class="help-block">{{ errors.title | first }}</span>
@@ -33,7 +33,7 @@
3333

3434
<div class="form-group {{ errors.body ? 'has-error' : '' }}">
3535

36-
<textarea rows="15" class="form-control login-field" name="body" placeholder="Enter your content" >{{ post.body }}" </textarea>
36+
<textarea rows="15" class="form-control login-field" name="body" placeholder="Enter your content" >{{ book.body }}" </textarea>
3737

3838
<label class="login-field-icon fui-user" for="login-last-name"></label>
3939

@@ -54,7 +54,7 @@
5454
</div>
5555
</div>
5656
<div class="text-center">
57-
<a href="{{ path_for('posts.add') }}" class="btn btn-primary">New Post</a>
57+
<a href="{{ path_for('books.add') }}" class="btn btn-primary">New Book</a>
5858
</div>
5959
</div>
6060
<div class="col-md-1"></div>

0 commit comments

Comments
 (0)