Skip to content

Commit

Permalink
Add edges, finish the update and creation of the attributes and types…
Browse files Browse the repository at this point in the history
… and add some responsiveness
  • Loading branch information
ysbrandB committed Jun 10, 2024
1 parent 1dba9db commit 26793eb
Show file tree
Hide file tree
Showing 27 changed files with 861 additions and 311 deletions.
55 changes: 55 additions & 0 deletions app/Http/Controllers/AttributeController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace App\Http\Controllers;

use App\Models\Attribute;
use App\Models\AttributeType;
use Illuminate\Http\Request;
use Inertia\Inertia;

class AttributeController extends Controller
{
public function index()
{
return Inertia::render('Attributes/Index', [
'attributeTypes' => AttributeType::with('attributes')->get(),
]);
}

public function create()
{
return Inertia::render('Attributes/Edit', [
'attributeTypes' => AttributeType::all(),
]);
}

public function store(Request $request)
{
$id = Attribute::create($request->all())->id;
return to_route('attributes.edit',$id);
}

public function show($id)
{
}

public function edit($id)
{
$attribute = Attribute::findOrFail($id);
return Inertia::render('Attributes/Edit', [
'attributeTypes' => AttributeType::all(),
'attribute' => $attribute,
]);
}

public function update(Request $request, $id)
{
$attribute = Attribute::findOrFail($id);
$attribute->update($request->all());
return to_route('attributes.index');
}

public function destroy($id)
{
}
}
14 changes: 11 additions & 3 deletions app/Http/Controllers/AttributeTypeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,20 @@ class AttributeTypeController extends Controller
{
public function index()
{
return Inertia::render('AttributeType/Index', [
'attributeTypes' => AttributeType::all(),
return Inertia::render('AttributeTypes/Index', [
'attributeTypes' => AttributeType::with('attributes')->get(),
]);
}

public function create()
{
return Inertia::render('Attributes/Edit', ['colors' => AttributeType::$colors]);
return Inertia::render('AttributeTypes/Edit', ['colors' => AttributeType::$colors]);
}

public function store(Request $request)
{
$id = AttributeType::create($request->all())->id;
return to_route('attribute_types.edit',$id);
}

public function show($id)
Expand All @@ -31,10 +33,16 @@ public function show($id)

public function edit($id)
{
$attributeType = AttributeType::findOrFail($id);
return Inertia::render('AttributeTypes/Edit',
['colors' => AttributeType::$colors, 'attributeType' => $attributeType]);
}

public function update(Request $request, $id)
{
$attributeType = AttributeType::findOrFail($id);
$attributeType->update($request->all());
return to_route('attribute_types.index');
}

public function destroy($id)
Expand Down
64 changes: 48 additions & 16 deletions app/Http/Controllers/ItemController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Http\Controllers;

use App\Http\Requests\StoreItemRequest;
use App\Models\Attribute;
use App\Models\AttributeType;
use App\Models\Item;
use Illuminate\Http\Request;
Expand Down Expand Up @@ -39,7 +40,10 @@ public function index(Request $request)
*/
public function create()
{
return Inertia::render('Items/Edit', []);
return Inertia::render('Items/Edit', [
'items' => Item::query()->get(),
'attributeTypes' => AttributeType::with('attributes')->get(),
]);
}

/**
Expand All @@ -51,9 +55,12 @@ public function store(StoreItemRequest $request)
$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 = Item::create($request->except('attributes', 'photo', 'wiring_photo'));
$item->attributes()->sync(explode(',', $request->input('attributes')));
$item->photo = $photo?->hashName();
$item->wiring_photo = $wiringPhoto?->hashName();
$item->save();
return redirect(route('items.index'));
}

Expand All @@ -75,7 +82,18 @@ public function show(string $publicId)
public function edit(int $id)
{
return Inertia::render('Items/Edit', [
'item' => Item::findorFail($id),
'item' => Item::with('attributes')->findorFail($id),
'items' => Item::query()->select('id', 'title')->get(),
'attributeTypes' => AttributeType::with('attributes')->get(),
'myAttributes' => AttributeType::whereHas('attributes', function ($attributes) use ($id) {
$attributes->whereHas('items', function ($query) use ($id) {
$query->where('items.id', $id);
});
})->with(['attributes' => function ($attributes) use ($id) {
$attributes->whereHas('items', function ($query) use ($id) {
$query->where('items.id', $id);
});
}])->get(),
]);
}

Expand All @@ -85,23 +103,31 @@ public function edit(int $id)
public function update(StoreItemRequest $request, int $id)
{
$item = Item::findOrFail($id);
$photo = $request->file('photo');
$photo->storeAs('photos/' . $photo->hashName(), ['disk' => 'public']);

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

if ($item->photo) {
//delete the old photo
Storage::disk('public')->delete('photos/' . $item->photo);
if ($item->photo) {
//delete the old photo
Storage::disk('public')->delete('photos/' . $item->photo);
}
$item->photo = $photo->hashName();
}
if ($item->wiring_photo) {
//delete the old photo
Storage::disk('public')->delete('photos/' . $item->wiring_photo);

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

if ($item->wiring_photo) {
//delete the old photo
Storage::disk('public')->delete('photos/' . $item->wiring_photo);
}
$item->wiring_photo = $wiring_photo->hashName();
}
$item->update($request->all());
$item->photo = $photo->hashName();
$item->wiring_photo = $photo->hashName();

$item->update($request->except('attributes', 'photo', 'wiring_photo'));
$item->attributes()->sync(explode(',', $request->input('attributes')));
$item->save();
return redirect()->back();
}
Expand All @@ -115,6 +141,12 @@ public function destroy(Item $item)
//delete the old photo
Storage::disk('public')->delete('photos/' . $item->photo);
}
if ($item->wiring_photo) {
//delete the old photo
Storage::disk('public')->delete('photos/' . $item->wiring_photo);
}
$item->attributes()->detach();
$item->edges()->delete();
$item->delete();
}
}
4 changes: 3 additions & 1 deletion app/Models/Attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
use Illuminate\Database\Eloquent\Relations\BelongsToMany;

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

protected $fillable = ['title', 'description', 'attribute_type_id'];
public function items(): BelongsToMany
{
return $this->belongsToMany(Item::class);
Expand Down
33 changes: 33 additions & 0 deletions app/Models/Edge.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace App\Models;

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

class Edge extends Model
{
use HasFactory;

protected $fillable = [
'from_item_id',
'to_item_id',
'belongsto_item_id'
];

public function fromItem(): BelongsTo
{
return $this->belongsTo(Attribute::class, 'from_item_id');
}

public function toItem(): BelongsTo
{
return $this->belongsTo(Attribute::class, 'to_item_id');
}

public function belongsToItem(): BelongsTo
{
return $this->belongsTo(Attribute::class, 'belongsto_item_id');
}
}
6 changes: 6 additions & 0 deletions app/Models/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Models;

use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Support\HigherOrderCollectionProxy;
use Vinkla\Hashids\Facades\Hashids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
Expand Down Expand Up @@ -51,6 +52,11 @@ public function attributes(): BelongsToMany
return $this->belongsToMany(Attribute::class);
}

public function edges(): HasMany
{
return $this->hasMany(Edge::class);
}

public function getPhotoUrlAttribute(): string
{
return asset('storage/photos/' . $this->photo);
Expand Down
24 changes: 24 additions & 0 deletions database/factories/EdgeFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Database\Factories;

use App\Models\Attribute;
use App\Models\Edge;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Carbon;

class EdgeFactory extends Factory
{
protected $model = Edge::class;

public function definition(): array
{
return [
'created_at' => Carbon::now(),
'updated_at' => Carbon::now(),

'from_attribute_id' => Attribute::factory(),
'to_attribute_id' => Attribute::factory(),
];
}
}
23 changes: 23 additions & 0 deletions database/migrations/2024_06_09_223050_create_edges_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

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

return new class extends Migration {
public function up(): void
{
Schema::create('edges', function (Blueprint $table) {
$table->id();
$table->foreignId('from_item_id');
$table->foreignId('to_item_id');
$table->foreignId('belongsto_item_id');
$table->timestamps();
});
}

public function down(): void
{
Schema::dropIfExists('edges');
}
};
48 changes: 36 additions & 12 deletions database/seeders/DatabaseSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Models\Attribute;
use App\Models\AttributeType;
use App\Models\Edge;
use App\Models\Item;
use App\Models\Processor;
use App\Models\User;
Expand Down Expand Up @@ -31,25 +32,48 @@ public function run(): void
'email' => '[email protected]',
]);

$processors = AttributeType::create(
[
'title' => 'Controllers',
'description' => 'All the controllers you can use to interact with the sensors and actuators',
'color' => 'blue',
]
);

Attribute::create(
[
'attribute_type_id' => $processors->id,
'title' => 'Arduino',
'description' => 'A microcontroller that can be used to interact with sensors and actuators',
]
);

Attribute::create(
[
'attribute_type_id' => $processors->id,
'title' => 'ESP8266',
'description' => 'The ESP8266 is a low-cost Wi-Fi microchip with full TCP/IP stack and microcontroller capability produced by Espressif Systems',
]
);

AttributeType::factory(10)->has(
Attribute::factory()->count(5)
)->create();

Item::factory(10)->create();
foreach (Item::all() as $item) {
$items = Item::factory(10)->create();
$attributes = Attribute::all();
foreach ($items as $item) {
$item->attributes()->attach(
Attribute::all()->random(3)
$attributes->random(3)
);
}


Processor::create([
'name' => 'Laptop',
'description' => 'A portable computer',
]);




for($i = 0; $i < sizeof($items) - 1; $i++) {
Edge::create([
'from_item_id' => $items->get($i)->id,
'to_item_id' => $items->get($i+1)->id,
'belongsto_item_id' => $items->first()->id,
]);
}
}
}
Loading

0 comments on commit 26793eb

Please sign in to comment.