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

(Challenge 5) Added API to get items #6

Open
wants to merge 5 commits into
base: origin
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions app/Http/Controllers/Admin/ItemBrandCrudController.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ protected function setupListOperation()
'name' => 'group_id',
'type' => 'select_from_array',
'options' => ItemGroup::pluck('name', 'id'),
'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
36 changes: 36 additions & 0 deletions app/Http/Controllers/Admin/ItemCrudController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Http\Requests\ItemRequest;
use App\Models\ItemBrand;
use App\Models\ItemTag;
use Backpack\CRUD\app\Http\Controllers\CrudController;
use Backpack\CRUD\app\Library\CrudPanel\CrudPanelFacade as CRUD;

Expand Down Expand Up @@ -48,6 +49,10 @@ protected function setupListOperation()
'name' => 'brand_id',
'type' => 'select_from_array',
'options' => ItemBrand::pluck('name', 'id'),
'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 +62,20 @@ protected function setupListOperation()
'label' => 'Name',
'name' => 'name',
],
[
'label' => 'Tags',
'name' => 'tag',
],
]);
}

protected function setupShowOperation()
{
$this->crud->addColumns([
[
'label' => 'Tags',
'name' => 'tag',
],
]);
}

Expand All @@ -74,6 +93,18 @@ private function addFilters()
}, function ($value) {
$this->crud->addClause('where', 'brand_id', $value);
});

$this->crud->addFilter([
'name' => 'tag',
'type' => 'dropdown',
'label' => 'Tag',
], function () {
return ItemTag::orderBy('name', 'ASC')->pluck('name', 'id')->toArray();
}, function ($value) {
$this->crud->query = $this->crud->query->whereHas('tag', function ($query) use ($value) {
$query->where('tag_id', $value);
});
});
}

/**
Expand Down Expand Up @@ -103,6 +134,11 @@ protected function setupCreateOperation()
'label' => 'Name',
'name' => 'name',
],
[
'label' => 'Tags',
'name' => 'tag',
'type' => 'relationship',
],
]);
}

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

namespace App\Http\Controllers\Admin;

use App\Http\Requests\ItemTagRequest;
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 ItemTagCrudController 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;

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

/**
* Define what happens when the List operation is loaded.
*
* @see https://backpackforlaravel.com/docs/crud-operation-list-entries
* @return void
*/
protected function setupListOperation()
{
$this->addFilters();

$this->crud->addColumns([
[
'label' => 'Name',
'name' => 'name',
],
]);
}

/**
* Add filters.
*/
private function addFilters()
{

}

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

$this->crud->addFields([
[
'label' => 'Name',
'name' => 'name',
],
]);
}

/**
* Define what happens when the Update operation is loaded.
*
* @see https://backpackforlaravel.com/docs/crud-operation-update
* @return void
*/
protected function setupUpdateOperation()
{
$this->setupCreateOperation();
}
}
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 Illuminate\Http\Request;
use App\Models\Item;

class ApiController extends Controller
{
public function getItems() {
$response = array();
$response['data'] = Item::all();
$response['count'] = Item::count();
return response()->json($response);
// $items = Item::get()->toJson(JSON_PRETTY_PRINT);
// return response($items, 200);
}
}
4 changes: 4 additions & 0 deletions app/Http/Requests/ItemRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

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

class ItemRequest extends FormRequest
{
Expand All @@ -26,12 +27,15 @@ public function authorize()
public function rules()
{
return [
'brand_id' => 'required',
'code' => [
'required',
'string',
'size:8',
Rule::unique('items', 'code')->ignore(request()->id),
],
'name' => 'required|min:5|max:255',
'tag' => 'required',
];
}

Expand Down
57 changes: 57 additions & 0 deletions app/Http/Requests/ItemTagRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace App\Http\Requests;

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

class ItemTagRequest 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 @@ -49,6 +49,11 @@ public function group()
return $this->belongsTo(ItemBrand::class, 'brand_id', 'id');
}

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

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

namespace App\Models;

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

class ItemTag extends Model
{
use CrudTrait;
use HasFactory;

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

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

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

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

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

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

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

/*
|--------------------------------------------------------------------------
| MUTATORS
|--------------------------------------------------------------------------
*/
}
Loading