forked from metin2/web
WIP: started implementing shop, multi-language
This commit is contained in:
19
app/Http/Controllers/Shop/CategoryController.php
Normal file
19
app/Http/Controllers/Shop/CategoryController.php
Normal file
@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Shop;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Shop\ShopCategory;
|
||||
use Illuminate\View\View;
|
||||
|
||||
class CategoryController extends Controller
|
||||
{
|
||||
public function show(int $id): View
|
||||
{
|
||||
$category = ShopCategory::find($id);
|
||||
|
||||
return view('shop/category', [
|
||||
'category' => $category
|
||||
]);
|
||||
}
|
||||
}
|
20
app/Http/Controllers/Shop/ItemController.php
Normal file
20
app/Http/Controllers/Shop/ItemController.php
Normal file
@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Shop;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Shop\ShopCategory;
|
||||
use App\Models\Shop\ShopItem;
|
||||
use Illuminate\View\View;
|
||||
|
||||
class ItemController extends Controller
|
||||
{
|
||||
public function show(int $id): View
|
||||
{
|
||||
$item = ShopItem::find($id);
|
||||
|
||||
return view('shop/item/show', [
|
||||
'item' => $item
|
||||
]);
|
||||
}
|
||||
}
|
17
app/Models/Enums/ShopItemPricingEnum.php
Normal file
17
app/Models/Enums/ShopItemPricingEnum.php
Normal file
@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Enums;
|
||||
|
||||
enum ShopItemPricingEnum: string
|
||||
{
|
||||
case CASH = 'CASH';
|
||||
case MILEAGE = 'MILEAGE';
|
||||
|
||||
public function description(): string
|
||||
{
|
||||
return match($this) {
|
||||
self::CASH => __('shop/main.currency.cash'),
|
||||
self::MILEAGE => __('shop/main.currency.mileage')
|
||||
};
|
||||
}
|
||||
}
|
53
app/Models/Shop/ShopCategory.php
Normal file
53
app/Models/Shop/ShopCategory.php
Normal file
@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Shop;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
|
||||
class ShopCategory extends Model
|
||||
{
|
||||
/**
|
||||
* The connection name for the model.
|
||||
*
|
||||
* @var string|null
|
||||
*/
|
||||
protected $connection = 'website';
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*
|
||||
* @var array<int, string>
|
||||
*/
|
||||
protected $fillable = [
|
||||
'name',
|
||||
];
|
||||
|
||||
/**
|
||||
* The attributes that should be hidden for serialization.
|
||||
*
|
||||
* @var array<int, string>
|
||||
*/
|
||||
protected $hidden = [
|
||||
|
||||
];
|
||||
|
||||
/**
|
||||
* The attributes that should be cast.
|
||||
*
|
||||
* @var array<string, string>
|
||||
*/
|
||||
protected $casts = [
|
||||
|
||||
];
|
||||
|
||||
/**
|
||||
* Get the items in this category
|
||||
*
|
||||
* @return HasMany
|
||||
*/
|
||||
public function items(): HasMany
|
||||
{
|
||||
return $this->hasMany(ShopItem::class, 'category_id', 'id');
|
||||
}
|
||||
}
|
67
app/Models/Shop/ShopItem.php
Normal file
67
app/Models/Shop/ShopItem.php
Normal file
@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Shop;
|
||||
|
||||
use App\Models\Enums\ShopItemPricingEnum;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
|
||||
class ShopItem extends Model
|
||||
{
|
||||
/**
|
||||
* The connection name for the model.
|
||||
*
|
||||
* @var string|null
|
||||
*/
|
||||
protected $connection = 'website';
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*
|
||||
* @var array<int, string>
|
||||
*/
|
||||
protected $fillable = [
|
||||
'id',
|
||||
'category_id',
|
||||
'old_price',
|
||||
'price',
|
||||
'pricing',
|
||||
'quantity',
|
||||
'image',
|
||||
'description',
|
||||
'other',
|
||||
];
|
||||
|
||||
/**
|
||||
* The attributes that should be hidden for serialization.
|
||||
*
|
||||
* @var array<int, string>
|
||||
*/
|
||||
protected $hidden = [
|
||||
|
||||
];
|
||||
|
||||
/**
|
||||
* The attributes that should be cast.
|
||||
*
|
||||
* @var array<string, string>
|
||||
*/
|
||||
protected $casts = [
|
||||
'pricing' => ShopItemPricingEnum::class,
|
||||
];
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function userCanBuy(): bool
|
||||
{
|
||||
$user = Auth::user();
|
||||
|
||||
if ($this->pricing == ShopItemPricingEnum::CASH)
|
||||
return $user->cash >= $this->price;
|
||||
elseif ($this->pricing == ShopItemPricingEnum::MILEAGE)
|
||||
return $user->mileage >= $this->price;
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
26
app/Providers/ViewServiceProvider.php
Normal file
26
app/Providers/ViewServiceProvider.php
Normal file
@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
namespace App\Providers;
|
||||
|
||||
use App\View\Composers\ShopComposer;
|
||||
use Illuminate\Support\Facades;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
|
||||
class ViewServiceProvider extends ServiceProvider
|
||||
{
|
||||
/**
|
||||
* Register any application services.
|
||||
*/
|
||||
public function register(): void
|
||||
{
|
||||
// ...
|
||||
}
|
||||
|
||||
/**
|
||||
* Bootstrap any application services.
|
||||
*/
|
||||
public function boot(): void
|
||||
{
|
||||
Facades\View::composer('layouts.shop', ShopComposer::class);
|
||||
}
|
||||
}
|
24
app/View/Composers/ShopComposer.php
Normal file
24
app/View/Composers/ShopComposer.php
Normal file
@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace App\View\Composers;
|
||||
|
||||
use App\Models\Shop\ShopCategory;
|
||||
use Illuminate\View\View;
|
||||
|
||||
class ShopComposer
|
||||
{
|
||||
/**
|
||||
* Bind data to the view.
|
||||
*/
|
||||
public function compose(View $view): void
|
||||
{
|
||||
// Fetch the amount of items in storage
|
||||
$view->with('storageCount', 10);
|
||||
|
||||
// Fetch the amount of items in storage
|
||||
$view->with('discountDesc', "20% reducere la chipsuri");
|
||||
|
||||
// Fetch the shop categories
|
||||
$view->with('categories', ShopCategory::all());
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user