Skip to content

Commit

Permalink
Merge pull request #26 from flyingluscas/improvement/categories
Browse files Browse the repository at this point in the history
Categories resource controller
  • Loading branch information
vedovelli authored Dec 2, 2016
2 parents 8f089b5 + aa82ada commit 2aef1f7
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 42 deletions.
6 changes: 3 additions & 3 deletions client/src/app/categories/form.vue
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
* Fetch the category from the server
*/
this.setFetching({ fetching: true })
this.$http.get(`categories/${id}/get`).then((res) => {
this.$http.get(`categories/${id}`).then((res) => {
const { id: _id, name } = res.data.category // http://wesbos.com/destructuring-renaming/
this.category.id = _id
this.category.name = name
Expand All @@ -98,7 +98,7 @@
}
},
save() {
this.$http.post('categories/create', { name: this.category.name }).then(() => {
this.$http.post('categories', { name: this.category.name }).then(() => {
/**
* This event will notify the world about
* the category creation. In this case
Expand All @@ -124,7 +124,7 @@
})
},
update() {
this.$http.put(`categories/${this.category.id}/update`, this.category).then(() => {
this.$http.put(`categories/${this.category.id}`, this.category).then(() => {
/**
* This event will notify the world about
* the category creation. In this case
Expand Down
2 changes: 1 addition & 1 deletion client/src/app/categories/main.vue
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@
* Makes the HTTP requesto to the API
*/
remove(category) {
this.$http.delete(`categories/${category.id}/remove`).then(() => {
this.$http.delete(`categories/${category.id}`).then(() => {
/**
* On success fetch a new set of Categories
* based on current page number
Expand Down
67 changes: 48 additions & 19 deletions webservice/app/Http/Controllers/CategoriesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Category;
use App\Http\Requests\CategoryRequest;

class CategoriesController extends Controller
{
public function all(Request $request)
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
try {
$pager = Category::paginate(10);
Expand All @@ -21,44 +25,63 @@ public function all(Request $request)
}
}

public function get($id)
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(CategoryRequest $request)
{
try {
$category = Category::find($id);

if (! $category) {
return response()->json([
'messages' => ['Category not found'],
], 404);
}
Category::create($request->only('name'));

return response()->json([
'result' => 'success',
'category' => $category,
], 200);
} catch (\Exception $e) {
} catch(\Exception $e) {
return response()->json([
'messages' => ['Failed to fetch category'],
'messages' => ['Failed to create category'],
], 500);
}
}

public function create(CategoryRequest $request)
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
try {
Category::create($request->only('name'));
$category = Category::find($id);

if (! $category) {
return response()->json([
'messages' => ['Category not found'],
], 404);
}

return response()->json([
'result' => 'success',
'category' => $category,
], 200);
} catch(\Exception $e) {
} catch (\Exception $e) {
return response()->json([
'messages' => ['Failed to create category'],
'messages' => ['Failed to fetch category'],
], 500);
}
}

public function update($id, CategoryRequest $request)
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(CategoryRequest $request, $id)
{
try {
$category = Category::find($id);
Expand All @@ -82,7 +105,13 @@ public function update($id, CategoryRequest $request)
}
}

public function remove($id)
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
try {
$category = Category::find($id);
Expand Down
2 changes: 1 addition & 1 deletion webservice/composer.lock

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

22 changes: 11 additions & 11 deletions webservice/routes/api.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<?php

Route::group(['middleware' => ['cors', 'api']], function () {
Route::post('login', ['uses' => 'LoginController@login']);
Route::group(['middleware' => 'auth:api'], function () {
Route::group(['prefix' => 'categories'], function () {
Route::get('', ['uses' => 'CategoriesController@all']);
Route::get('{id}/get', ['uses' => 'CategoriesController@get']);
Route::post('create', ['uses' => 'CategoriesController@create']);
Route::put('{id}/update', ['uses' => 'CategoriesController@update']);
Route::delete('{id}/remove', ['uses' => 'CategoriesController@remove']);
});
Route::group([
'middleware' => ['cors', 'api'],
], function () {
Route::post('/login', 'LoginController@login');

Route::group([
'middleware' => 'auth:api',
], function () {
Route::resource('/categories', 'CategoriesController', [
'except' => ['create', 'edit'],
]);
});
});

14 changes: 7 additions & 7 deletions webservice/tests/Controllers/CategoriesControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function can_delete_category()
{
$this->create(Category::class);

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

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

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

Expand Down Expand Up @@ -64,9 +64,9 @@ public function get_not_found_if_category_dont_exist($method, $url)
public function urlProvider()
{
return [
['GET', '/api/categories/1/get'],
['PUT', '/api/categories/1/update'],
['DELETE', '/api/categories/1/remove'],
['GET', '/api/categories/1'],
['PUT', '/api/categories/1'],
['DELETE', '/api/categories/1'],
];
}

Expand All @@ -79,7 +79,7 @@ public function can_get_category()
'name' => 'Dummy',
]);

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

$this->assertResponseOk();

Expand All @@ -99,7 +99,7 @@ public function can_get_category()
*/
public function can_create_category()
{
$this->json('POST', '/api/categories/create', [
$this->json('POST', '/api/categories', [
'name' => 'Dummy name',
]);

Expand Down

0 comments on commit 2aef1f7

Please sign in to comment.