Skip to content

Commit

Permalink
Start implementing attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
ysbrandB committed Jun 3, 2024
1 parent 88fc8ff commit e57ddf5
Show file tree
Hide file tree
Showing 27 changed files with 584 additions and 142 deletions.
44 changes: 22 additions & 22 deletions app/Http/Controllers/ItemController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace App\Http\Controllers;

use App\Http\Requests\StoreItemRequest;
use App\Http\Requests\UpdateItemRequest;
use App\Models\AttributeType;
use App\Models\Item;
use Illuminate\Support\Facades\Storage;
use Inertia\Inertia;
Expand All @@ -17,10 +17,8 @@ class ItemController extends Controller
public function index()
{
return Inertia::render('Items/Index', [
'items' => Item::all()->each(function ($item) {
$item->hashid = $item->public_id;
$item->photo_url = asset('storage/photos/' . $item->photo);
}),
'items' => Item::all(),
'attributeTypes' => AttributeType::with('attributes')->get(),
]);
}

Expand All @@ -38,25 +36,22 @@ public function create()
public function store(StoreItemRequest $request)
{
$photo = $request->file('photo');
$photo->storeAs( 'photos/'.$photo->hashName(), ['disk' => 'public']);
Item::create([
'name' => $request->name,
'description' => $request->description,
'photo' => $photo->hashName(),
]);

$photo?->storeAs( 'photos/'.$photo->hashName(), ['disk' => 'public']);
$wiringPhoto = $request->file('wiring_photo');
$wiringPhoto?->storeAs( 'photos/'.$photo->hashName(), ['disk' => 'public']);
$item = Item::create($request->all());
$item->photo = $photo?->hashName();
$item->wiring_photo = $wiringPhoto?->hashName();
return redirect(route('items.index'));
}

/**
* Display the specified resource.
*/
public function show(String $hashid)
public function show(String $publicId)
{
$id = Hashids::decode($hashid);
$id = Hashids::decode($publicId);
$item = Item::query()->where('id', $id)->firstOrFail();
$item->hashid = $item->public_id;
$item->photo_url = asset('storage/photos/' . $item->photo);
return Inertia::render('Items/Show', [
'item' => $item,
]);
Expand All @@ -81,17 +76,22 @@ public function update(StoreItemRequest $request, int $id)
$photo = $request->file('photo');
$photo->storeAs( 'photos/'.$photo->hashName(), ['disk' => 'public']);

$photo = $request->file('wiring_photo');
$photo->storeAs( 'photos/'.$photo->hashName(), ['disk' => 'public']);

if($item->photo){
//delete the old photo
Storage::disk('public')->delete('photos/' . $item->photo);
}
$item->update([
'name' => $request->name,
'description' => $request->description,
'photo' => $photo->hashName(),
]);
if($item->wiring_photo){
//delete the old photo
Storage::disk('public')->delete('photos/' . $item->wiring_photo);
}
$item->update($request->all());
$item->photo = $photo->hashName();
$item->wiring_photo = $photo->hashName();
$item->save();
return redirect(route('items.index'));
return redirect()->back();
}

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

namespace App\Http\Controllers;

abstract class ProcessorController
{
//
}
12 changes: 9 additions & 3 deletions app/Http/Requests/StoreItemRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Http\Requests;

use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Auth;

Expand All @@ -18,14 +19,19 @@ public function authorize(): bool
/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
* @return array<string, ValidationRule|array|string>
*/
public function rules(): array
{
return [
//name and description are required
'name' => ['required', 'string'],
'title' => ['required', 'string'],
'description' => ['required', 'string'],
'wiring_instructions' => ['required', 'string'],
'pros' => ['required', 'string'],
'cons' => ['required', 'string'],
'hardware_considerations' => ['required', 'string'],
'software_considerations' => ['required', 'string'],
'example_code' => ['required', 'string'],
];
}
}
12 changes: 10 additions & 2 deletions app/Models/Attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,23 @@

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;

/**
* @property string name
* @property string description
*/
class Attribute extends Model
{
use HasFactory;
public function items()
public function items(): BelongsToMany
{
return $this->belongsToMany(Item::class);
}

public function attributeType(): BelongsTo
{
return $this->belongsToMany(Item::class, 'attribute_items');
return $this->belongsTo(AttributeType::class);
}
}
23 changes: 23 additions & 0 deletions app/Models/AttributeType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;

/**
* @property string title
* @property string description
*/
class AttributeType extends Model
{
use HasFactory;

protected $fillable = ['title', 'description'];

public function attributes(): HasMany
{
return $this->hasMany(Attribute::class);
}
}
21 changes: 19 additions & 2 deletions app/Models/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,29 @@ class Item extends Model
* @var \Illuminate\Support\HigherOrderCollectionProxy|mixed
*/
protected $fillable = [
'name',
'title',
'description',
'pros',
'cons',
'hardware_considerations',
'software_considerations',
'example_code',
'photo',
'is_actuator'
'is_actuator',
];

protected $with = ['attributes'];
protected $appends = ['public_id', 'photo_url', 'wiring_photo_url'];
public function getPhotoUrlAttribute(): string
{
return asset('storage/photos/' . $this->photo);
}

public function getWiringPhotoUrlAttribute(): string
{
return asset('storage/photos/' . $this->wiring_photo);
}

public function getPublicIdAttribute(): string
{
return Hashids::encode($this->id);
Expand Down
5 changes: 3 additions & 2 deletions app/Models/Controller.php → app/Models/Processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@

/**
* @property string title
* @property string description
* @property HasMany item
* @property BelongsToMany attributes
*/
class Controller extends Model
class Processor extends Model
{
use HasFactory;

Expand All @@ -25,6 +26,6 @@ public function item(): BelongsTo

public function attributes(): BelongsToMany
{
return $this->belongsToMany(Attribute::class, 'attribute_controllers');
return $this->belongsToMany(Attribute::class);
}
}
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"mockery/mockery": "^1.6",
"nunomaduro/collision": "^8.0",
"phpunit/phpunit": "^11.0.1",
"rector/rector": "^1.1",
"spatie/laravel-ignition": "^2.4"
},
"autoload": {
Expand Down
119 changes: 118 additions & 1 deletion composer.lock

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

4 changes: 2 additions & 2 deletions database/factories/AttributeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public function definition(): array
return [
'created_at' => Carbon::now(),
'updated_at' => Carbon::now(),
'name' => $this->faker->name(),
'description' => $this->faker->text(),
'title' => $this->faker->name(),
'description' => $this->faker->paragraph(),
];
}
}
Loading

0 comments on commit e57ddf5

Please sign in to comment.