Skip to content

Commit 2aef1f7

Browse files
authored
Merge pull request #26 from flyingluscas/improvement/categories
Categories resource controller
2 parents 8f089b5 + aa82ada commit 2aef1f7

File tree

6 files changed

+71
-42
lines changed

6 files changed

+71
-42
lines changed

client/src/app/categories/form.vue

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
* Fetch the category from the server
7373
*/
7474
this.setFetching({ fetching: true })
75-
this.$http.get(`categories/${id}/get`).then((res) => {
75+
this.$http.get(`categories/${id}`).then((res) => {
7676
const { id: _id, name } = res.data.category // http://wesbos.com/destructuring-renaming/
7777
this.category.id = _id
7878
this.category.name = name
@@ -98,7 +98,7 @@
9898
}
9999
},
100100
save() {
101-
this.$http.post('categories/create', { name: this.category.name }).then(() => {
101+
this.$http.post('categories', { name: this.category.name }).then(() => {
102102
/**
103103
* This event will notify the world about
104104
* the category creation. In this case
@@ -124,7 +124,7 @@
124124
})
125125
},
126126
update() {
127-
this.$http.put(`categories/${this.category.id}/update`, this.category).then(() => {
127+
this.$http.put(`categories/${this.category.id}`, this.category).then(() => {
128128
/**
129129
* This event will notify the world about
130130
* the category creation. In this case

client/src/app/categories/main.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@
111111
* Makes the HTTP requesto to the API
112112
*/
113113
remove(category) {
114-
this.$http.delete(`categories/${category.id}/remove`).then(() => {
114+
this.$http.delete(`categories/${category.id}`).then(() => {
115115
/**
116116
* On success fetch a new set of Categories
117117
* based on current page number

webservice/app/Http/Controllers/CategoriesController.php

Lines changed: 48 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@
22

33
namespace App\Http\Controllers;
44

5-
use Illuminate\Http\Request;
65
use App\Category;
76
use App\Http\Requests\CategoryRequest;
87

98
class CategoriesController extends Controller
109
{
11-
public function all(Request $request)
10+
/**
11+
* Display a listing of the resource.
12+
*
13+
* @return \Illuminate\Http\Response
14+
*/
15+
public function index()
1216
{
1317
try {
1418
$pager = Category::paginate(10);
@@ -21,44 +25,63 @@ public function all(Request $request)
2125
}
2226
}
2327

24-
public function get($id)
28+
/**
29+
* Store a newly created resource in storage.
30+
*
31+
* @param \Illuminate\Http\Request $request
32+
* @return \Illuminate\Http\Response
33+
*/
34+
public function store(CategoryRequest $request)
2535
{
2636
try {
27-
$category = Category::find($id);
28-
29-
if (! $category) {
30-
return response()->json([
31-
'messages' => ['Category not found'],
32-
], 404);
33-
}
37+
Category::create($request->only('name'));
3438

3539
return response()->json([
3640
'result' => 'success',
37-
'category' => $category,
3841
], 200);
39-
} catch (\Exception $e) {
42+
} catch(\Exception $e) {
4043
return response()->json([
41-
'messages' => ['Failed to fetch category'],
44+
'messages' => ['Failed to create category'],
4245
], 500);
4346
}
4447
}
4548

46-
public function create(CategoryRequest $request)
49+
/**
50+
* Display the specified resource.
51+
*
52+
* @param int $id
53+
* @return \Illuminate\Http\Response
54+
*/
55+
public function show($id)
4756
{
4857
try {
49-
Category::create($request->only('name'));
58+
$category = Category::find($id);
59+
60+
if (! $category) {
61+
return response()->json([
62+
'messages' => ['Category not found'],
63+
], 404);
64+
}
5065

5166
return response()->json([
5267
'result' => 'success',
68+
'category' => $category,
5369
], 200);
54-
} catch(\Exception $e) {
70+
} catch (\Exception $e) {
5571
return response()->json([
56-
'messages' => ['Failed to create category'],
72+
'messages' => ['Failed to fetch category'],
5773
], 500);
5874
}
5975
}
6076

61-
public function update($id, CategoryRequest $request)
77+
/**
78+
* Update the specified resource in storage.
79+
*
80+
* @param \Illuminate\Http\Request $request
81+
* @param int $id
82+
* @return \Illuminate\Http\Response
83+
*/
84+
public function update(CategoryRequest $request, $id)
6285
{
6386
try {
6487
$category = Category::find($id);
@@ -82,7 +105,13 @@ public function update($id, CategoryRequest $request)
82105
}
83106
}
84107

85-
public function remove($id)
108+
/**
109+
* Remove the specified resource from storage.
110+
*
111+
* @param int $id
112+
* @return \Illuminate\Http\Response
113+
*/
114+
public function destroy($id)
86115
{
87116
try {
88117
$category = Category::find($id);

webservice/composer.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

webservice/routes/api.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
<?php
22

3-
Route::group(['middleware' => ['cors', 'api']], function () {
4-
Route::post('login', ['uses' => 'LoginController@login']);
5-
Route::group(['middleware' => 'auth:api'], function () {
6-
Route::group(['prefix' => 'categories'], function () {
7-
Route::get('', ['uses' => 'CategoriesController@all']);
8-
Route::get('{id}/get', ['uses' => 'CategoriesController@get']);
9-
Route::post('create', ['uses' => 'CategoriesController@create']);
10-
Route::put('{id}/update', ['uses' => 'CategoriesController@update']);
11-
Route::delete('{id}/remove', ['uses' => 'CategoriesController@remove']);
12-
});
3+
Route::group([
4+
'middleware' => ['cors', 'api'],
5+
], function () {
6+
Route::post('/login', 'LoginController@login');
7+
8+
Route::group([
9+
'middleware' => 'auth:api',
10+
], function () {
11+
Route::resource('/categories', 'CategoriesController', [
12+
'except' => ['create', 'edit'],
13+
]);
1314
});
1415
});
15-

webservice/tests/Controllers/CategoriesControllerTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public function can_delete_category()
1616
{
1717
$this->create(Category::class);
1818

19-
$this->json('DELETE', '/api/categories/1/remove');
19+
$this->json('DELETE', '/api/categories/1');
2020

2121
$this->assertResponseOk();
2222
$this->dontSeeInDatabase('categories', ['id' => 1]);
@@ -29,7 +29,7 @@ public function can_update_category()
2929
{
3030
$this->create(Category::class);
3131

32-
$this->json('PUT', '/api/categories/1/update', [
32+
$this->json('PUT', '/api/categories/1', [
3333
'name' => 'Dummy',
3434
]);
3535

@@ -64,9 +64,9 @@ public function get_not_found_if_category_dont_exist($method, $url)
6464
public function urlProvider()
6565
{
6666
return [
67-
['GET', '/api/categories/1/get'],
68-
['PUT', '/api/categories/1/update'],
69-
['DELETE', '/api/categories/1/remove'],
67+
['GET', '/api/categories/1'],
68+
['PUT', '/api/categories/1'],
69+
['DELETE', '/api/categories/1'],
7070
];
7171
}
7272

@@ -79,7 +79,7 @@ public function can_get_category()
7979
'name' => 'Dummy',
8080
]);
8181

82-
$this->json('GET', '/api/categories/1/get');
82+
$this->json('GET', '/api/categories/1');
8383

8484
$this->assertResponseOk();
8585

@@ -99,7 +99,7 @@ public function can_get_category()
9999
*/
100100
public function can_create_category()
101101
{
102-
$this->json('POST', '/api/categories/create', [
102+
$this->json('POST', '/api/categories', [
103103
'name' => 'Dummy name',
104104
]);
105105

0 commit comments

Comments
 (0)