Skip to content
This repository has been archived by the owner on Oct 4, 2021. It is now read-only.

Challenge 5 (Rizki) - Create API from Items #14

Open
wants to merge 9 commits into
base: origin
Choose a base branch
from
18 changes: 18 additions & 0 deletions app/Http/Controllers/APIController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Models\Item;

class APIController extends Controller
{
public function getItems(){
$items = Item::all();
$count = $items->count();
$data = collect(["data"=>$items, "count"=>$count]);
return $data->toJson();
}


}
5 changes: 5 additions & 0 deletions app/Http/Controllers/Admin/ItemBrandCrudController.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ protected function setupListOperation()
'name' => 'group_id',
'type' => 'select_from_array',
'options' => ItemGroup::pluck('name', 'id'),
'orderable' => true,
'orderLogic' => function ($query, $column, $columnDirection) {
return $query->leftJoin('item_groups', 'item_brands.group_id', '=', 'item_groups.id')
->orderBy('item_groups.name', $columnDirection)->select('item_brands.*');
}
],
[
'label' => 'Code',
Expand Down
25 changes: 24 additions & 1 deletion app/Http/Controllers/Admin/ItemCrudController.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,17 @@ public function setup()
protected function setupListOperation()
{
$this->addFilters();

$this->crud->addColumns([
[
'label' => 'Brand',
'name' => 'brand_id',
'type' => 'select_from_array',
'options' => ItemBrand::pluck('name', 'id'),
'orderable' => true,
'orderLogic' => function ($query, $column, $columnDirection) {
return $query->leftJoin('item_brands', 'items.brand_id', '=', 'item_brands.id')
->orderBy('item_brands.name', $columnDirection)->select('items.*');
}
],
[
'label' => 'Code',
Expand All @@ -57,6 +61,15 @@ protected function setupListOperation()
'label' => 'Name',
'name' => 'name',
],

[
'label' => 'Tags',
'name' => 'tags',
'type' => 'relationship',
'entity' => 'tags',
'attribute' => 'name',
'model' => App\Models\Tag::class,
],
]);
}

Expand Down Expand Up @@ -103,6 +116,16 @@ protected function setupCreateOperation()
'label' => 'Name',
'name' => 'name',
],
[
'label' => 'Tags',
'name' => 'tags',
'type' => 'select_multiple',
'entity' => 'tags',
'model' => "App\Models\Tag",
'attribute' => 'name',
'pivot' => true,

]
]);
}

Expand Down
118 changes: 118 additions & 0 deletions app/Http/Controllers/Admin/TagCrudController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?php

namespace App\Http\Controllers\Admin;

use App\Http\Requests\TagRequest;
use Backpack\CRUD\app\Http\Controllers\CrudController;
use Backpack\CRUD\app\Library\CrudPanel\CrudPanelFacade as CRUD;

/**
* Class TagCrudController
* @package App\Http\Controllers\Admin
* @property-read \Backpack\CRUD\app\Library\CrudPanel\CrudPanel $crud
*/
class TagCrudController extends CrudController
{
use \Backpack\CRUD\app\Http\Controllers\Operations\ListOperation;
use \Backpack\CRUD\app\Http\Controllers\Operations\CreateOperation;
use \Backpack\CRUD\app\Http\Controllers\Operations\UpdateOperation;
use \Backpack\CRUD\app\Http\Controllers\Operations\DeleteOperation;
use \Backpack\CRUD\app\Http\Controllers\Operations\ShowOperation;

/**
* Configure the CrudPanel object. Apply settings to all operations.
*
* @return void
*/
public function setup()
{
CRUD::setModel(\App\Models\Tag::class);
CRUD::setRoute(config('backpack.base.route_prefix') . '/tag');
CRUD::setEntityNameStrings('tag', 'tags');
}

/**
* Define what happens when the List operation is loaded.
*
* @see https://backpackforlaravel.com/docs/crud-operation-list-entries
* @return void
*/
protected function setupListOperation()
{
CRUD::setFromDb(); // columns
$this->crud->addColumn(
[
'label' => 'Items',
'name' => 'items',
'type' => 'relationship',
'entity' => 'items',
'attribute' => 'name',
'model' => App\Models\Item::class,
],
);

/**
* Columns can be defined using the fluent syntax or array syntax:
* - CRUD::column('price')->type('number');
* - CRUD::addColumn(['name' => 'price', 'type' => 'number']);
*/
}

/**
* Define what happens when the Create operation is loaded.
*
* @see https://backpackforlaravel.com/docs/crud-operation-create
* @return void
*/
protected function setupCreateOperation()
{
CRUD::setValidation(TagRequest::class);

CRUD::setFromDb(); // fields
$this->crud->addFields([
[
'label' => 'Items',
'name' => 'items',
'type' => 'select_multiple',
'entity' => 'items',
'model' => "App\Models\Item",
'attribute' => 'name',
'pivot' => true,

]
]);

/**
* Fields can be defined using the fluent syntax or array syntax:
* - CRUD::field('price')->type('number');
* - CRUD::addField(['name' => 'price', 'type' => 'number']));
*/
}

protected function setupShowOperation()
{
CRUD::setFromDb();
$this->crud->addColumn(
[
'label' => 'Items',
'name' => 'items',
'type' => 'relationship',
'entity' => 'items',
'attribute' => 'name',
'model' => App\Models\Item::class,
],
);

}

/**
* Define what happens when the Update operation is loaded.
*
* @see https://backpackforlaravel.com/docs/crud-operation-update
* @return void
*/
protected function setupUpdateOperation()
{
$this->setupCreateOperation();
}
}
2 changes: 2 additions & 0 deletions app/Http/Requests/ItemRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ public function authorize()
public function rules()
{
return [
'brand_id' => 'required',
'code' => [
'required',
'string',
'unique:items,code',
'size:8',
],
'name' => 'required|min:5|max:255',
Expand Down
56 changes: 56 additions & 0 deletions app/Http/Requests/TagRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace App\Http\Requests;

use App\Http\Requests\Request;
use Illuminate\Foundation\Http\FormRequest;

class TagRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
// only allow updates if the user is logged in
return backpack_auth()->check();
}

/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'name' => 'required|min:5|max:255'
];
}

/**
* Get the validation attributes that apply to the request.
*
* @return array
*/
public function attributes()
{
return [
//
];
}

/**
* Get the validation messages that apply to the request.
*
* @return array
*/
public function messages()
{
return [
//
];
}
}
5 changes: 5 additions & 0 deletions app/Models/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ class Item extends Model
|--------------------------------------------------------------------------
*/

public function tags()
{
return $this->belongsToMany(Tag::class, 'item_tags', 'item_id', 'tag_id');
}

/*
|--------------------------------------------------------------------------
| RELATIONS
Expand Down
58 changes: 58 additions & 0 deletions app/Models/Tag.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace App\Models;

use Backpack\CRUD\app\Models\Traits\CrudTrait;
use Illuminate\Database\Eloquent\Model;

class Tag extends Model
{
use CrudTrait;

/*
|--------------------------------------------------------------------------
| GLOBAL VARIABLES
|--------------------------------------------------------------------------
*/

protected $table = 'tags';
protected $primaryKey = 'id';
protected $guarded = ['id'];
protected $fillable = ['name'];
protected $dates = ['created_at', 'updated_at',];

/*
|--------------------------------------------------------------------------
| FUNCTIONS
|--------------------------------------------------------------------------
*/

public function items()
{
return $this->belongsToMany(Item::class, 'item_tags', 'tag_id', 'item_id');
}

/*
|--------------------------------------------------------------------------
| RELATIONS
|--------------------------------------------------------------------------
*/

/*
|--------------------------------------------------------------------------
| SCOPES
|--------------------------------------------------------------------------
*/

/*
|--------------------------------------------------------------------------
| ACCESSORS
|--------------------------------------------------------------------------
*/

/*
|--------------------------------------------------------------------------
| MUTATORS
|--------------------------------------------------------------------------
*/
}
2 changes: 1 addition & 1 deletion config/database.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@

'redis' => [

'client' => env('REDIS_CLIENT', 'phpredis'),
'client' => env('REDIS_CLIENT', 'predis'),

'options' => [
'cluster' => env('REDIS_CLUSTER', 'redis'),
Expand Down
32 changes: 32 additions & 0 deletions database/migrations/2021_09_12_125849_create_tags_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateTagsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('tags', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('tags');
}
}
Loading