1
0
mirror of https://github.com/cp6/my-idlers.git synced 2024-11-16 15:33:44 +01:00

Updated Misc class to use relationships + cache

Updated Misc class to use relationships + cache
This commit is contained in:
cp6 2022-07-20 14:35:12 +10:00
parent 3d87c645dc
commit 356bd368a4
5 changed files with 68 additions and 57 deletions

View File

@ -14,10 +14,7 @@ class MiscController extends Controller
{
public function index()
{
$misc = DB::table('misc_services as d')
->join('pricings as pr', 'd.id', '=', 'pr.service_id')
->get(['d.*', 'pr.*']);
$misc = Misc::allMisc();
return view('misc.index', compact(['misc']));
}
@ -28,12 +25,8 @@ class MiscController extends Controller
public function show(Misc $misc)
{
$service_extras = DB::table('misc_services as m')
->join('pricings as p', 'm.id', '=', 'p.service_id')
->where('m.id', '=', $misc->id)
->get(['m.*', 'p.*']);
return view('misc.show', compact(['misc', 'service_extras']));
$misc_data = Misc::misc($misc->id)[0];
return view('misc.show', compact(['misc_data']));
}
public function store(Request $request)
@ -45,20 +38,19 @@ class MiscController extends Controller
'next_due_date' => 'required|date'
]);
$ms_id = Str::random(8);
$misc_id = Str::random(8);
$pricing = new Pricing();
$as_usd = $pricing->convertToUSD($request->price, $request->currency);
$pricing->insertPricing(5, $ms_id, $request->currency, $request->price, $request->payment_term, $as_usd, $request->next_due_date);
$pricing->insertPricing(5, $misc_id, $request->currency, $request->price, $request->payment_term, $as_usd, $request->next_due_date);
Misc::create([
'id' => $ms_id,
'id' => $misc_id,
'name' => $request->name,
'owned_since' => $request->owned_since
]);
Cache::forget("all_misc");
Home::homePageCacheForget();
return redirect()->route('misc.index')
@ -67,12 +59,8 @@ class MiscController extends Controller
public function edit(Misc $misc)
{
$misc = DB::table('misc_services as s')
->join('pricings as p', 's.id', '=', 'p.service_id')
->where('s.id', '=', $misc->id)
->get(['s.*', 'p.*']);
return view('misc.edit', compact('misc'));
$misc_data = Misc::misc($misc->id)[0];
return view('misc.edit', compact('misc_data'));
}
public function update(Request $request, Misc $misc)
@ -91,11 +79,11 @@ class MiscController extends Controller
]);
$pricing = new Pricing();
$as_usd = $pricing->convertToUSD($request->price, $request->currency);
$pricing->updatePricing($misc->id, $request->currency, $request->price, $request->payment_term, $as_usd, $request->next_due_date);
Cache::forget("all_misc");
Cache::forget("misc.{$misc->id}");
Home::homePageCacheForget();
return redirect()->route('misc.index')
@ -105,12 +93,13 @@ class MiscController extends Controller
public function destroy(Misc $misc)
{
$items = Misc::find($misc->id);
$items->delete();
$p = new Pricing();
$p->deletePricing($misc->id);
Cache::forget("all_misc");
Cache::forget("misc.{$misc->id}");
Home::homePageCacheForget();
return redirect()->route('misc.index')

View File

@ -4,6 +4,7 @@ namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Cache;
class Misc extends Model
{
@ -14,4 +15,25 @@ class Misc extends Model
protected $table = 'misc_services';
protected $fillable = ['id', 'name', 'owned_since'];
public static function allMisc()
{//All misc and relationships (no using joins)
return Cache::remember("all_misc", now()->addMonth(1), function () {
return Misc::with(['price'])->get();
});
}
public static function misc(string $misc_id)
{//Single misc and relationships (no using joins)
return Cache::remember("misc.$misc_id", now()->addMonth(1), function () use ($misc_id) {
return Misc::where('id', $misc_id)
->with(['price'])->get();
});
}
public function price()
{
return $this->hasOne(Pricing::class, 'service_id', 'id');
}
}

View File

@ -1,7 +1,7 @@
@section('title') {{$misc[0]->name}} {{'edit'}} @endsection
@section('title') {{$misc_data->name}} {{'edit'}} @endsection
<x-app-layout>
<x-slot name="header">
Edit {{ $misc[0]->name }}
Edit {{ $misc_data->name }}
</x-slot>
<div class="container">
<x-card class="shadow mt-3">
@ -11,7 +11,7 @@
Go back
</x-back-button>
<x-errors-alert></x-errors-alert>
<form action="{{ route('misc.update', $misc[0]->service_id) }}" method="POST">
<form action="{{ route('misc.update', $misc_data->id) }}" method="POST">
@csrf
@method('PUT')
<div class="row">
@ -20,7 +20,7 @@
<div class="input-group-prepend"><span class="input-group-text">Name</span></div>
<input type="text"
class="form-control"
name="name" required maxlength="255" value="{{$misc[0]->name}}">
name="name" required maxlength="255" value="{{$misc_data->name}}">
@error('name') <span class="text-red-500">{{ $message }}
</span>@enderror
</div>
@ -30,17 +30,17 @@
<x-slot name="title">Price</x-slot>
<x-slot name="name">price</x-slot>
<x-slot name="step">0.01</x-slot>
<x-slot name="value">{{ $misc[0]->price }}</x-slot>
<x-slot name="value">{{ $misc_data->price->price }}</x-slot>
</x-number-input>
</div>
<div class="col-md-3 mb-3">
<x-term-select>
<x-slot name="current">{{$misc[0]->term}}</x-slot>
<x-slot name="current">{{$misc_data->price->term}}</x-slot>
</x-term-select>
</div>
<div class="col-md-2 mb-3">
<x-currency-select>
<x-slot name="current">{{$misc[0]->currency}}</x-slot>
<x-slot name="current">{{$misc_data->price->currency}}</x-slot>
</x-currency-select>
</div>
</div>
@ -49,20 +49,20 @@
<x-date-input>
<x-slot name="title">Owned since</x-slot>
<x-slot name="name">owned_since</x-slot>
<x-slot name="value">{{$misc[0]->owned_since }}</x-slot>
<x-slot name="value">{{$misc_data->owned_since }}</x-slot>
</x-date-input>
</div>
<div class="col-12 col-md-4 mb-3">
<x-date-input>
<x-slot name="title">Next due date</x-slot>
<x-slot name="name">next_due_date</x-slot>
<x-slot name="value">{{$misc[0]->next_due_date}}</x-slot>
<x-slot name="value">{{$misc_data->price->next_due_date}}</x-slot>
</x-date-input>
</div>
</div>
<div class="form-check mt-2">
<input class="form-check-input" name="is_active" type="checkbox"
value="1" {{ ($misc[0]->active === 1) ? 'checked' : '' }}>
value="1" {{ ($misc_data->active === 1) ? 'checked' : '' }}>
<label class="form-check-label">
I still have this service
</label>

View File

@ -36,23 +36,23 @@
{{ date_format(new DateTime($m->owned_since), 'jS F Y') }}
@endif
</td>
<td class="text-nowrap">{{ now()->diffInDays($m->next_due_date) }}
<td class="text-nowrap">{{ now()->diffInDays($m->price->next_due_date) }}
<small>days</small></td>
<td class="text-nowrap">{{$m->price}} {{$m->currency}}
<small>{{\App\Process::paymentTermIntToString($m->term)}}</small></td>
<td class="text-nowrap">{{$m->price->price}} {{$m->price->currency}}
<small>{{\App\Process::paymentTermIntToString($m->price->term)}}</small></td>
<td class="text-nowrap">
<form action="{{ route('misc.destroy', $m->service_id) }}" method="POST">
<a href="{{ route('misc.edit', $m->service_id) }}"
<form action="{{ route('misc.destroy', $m->id) }}" method="POST">
<a href="{{ route('misc.edit', $m->id) }}"
class="text-body mx-1">
<i class="fas fa-pen" title="edit"></i></a>
<a href="{{ route('misc.show', $m->service_id) }}"
<a href="{{ route('misc.show', $m->id) }}"
class="text-body mx-1">
<i class="fas fa-eye" title="view"></i>
</a>
@csrf
@method('DELETE')
<i class="fas fa-trash text-danger ms-3" @click="modalForm"
id="btn-{{$m->name}}" title="{{$m->service_id}}"></i>
id="btn-{{$m->name}}" title="{{$m->id}}"></i>
</form>
</td>
</tr>

View File

@ -1,4 +1,4 @@
@section('title') {{ $service_extras[0]->name }} {{'service'}} @endsection
@section('title') {{ $misc_data->name }} {{'service'}} @endsection
<x-app-layout>
<x-slot name="header">
{{ __('Misc details') }}
@ -7,11 +7,11 @@
<x-card class="shadow mt-3">
<div class="row">
<div class="col-12 col-md-6 mb-2">
<h2>{{ $service_extras[0]->name}}</h2>
<h2>{{ $misc_data->name}}</h2>
</div>
<div class="col-12 col-md-6 text-md-end">
<h6 class="text-muted pe-lg-4">{{ $service_extras[0]->service_id }}</h6>
@if($service_extras[0]->active !== 1)
<h6 class="text-muted pe-lg-4">{{ $misc_data->id }}</h6>
@if($misc_data->active !== 1)
<h6 class="text-danger pe-lg-4">not active</h6>
@endif
</div>
@ -23,41 +23,41 @@
<tbody>
<tr>
<td class="px-2 py-2 font-bold text-muted">Service</td>
<td>{{ $service_extras[0]->name }}</td>
<td>{{ $misc_data->name }}</td>
</tr>
<tr>
<td class="px-2 py-2 font-bold text-muted">Price</td>
<td>{{ $service_extras[0]->price }} {{ $service_extras[0]->currency }}
<small>{{\App\Process::paymentTermIntToString($service_extras[0]->term)}}</small>
<td>{{ $misc_data->price->price }} {{ $misc_data->price->currency }}
<small>{{\App\Process::paymentTermIntToString($misc_data->price->term)}}</small>
</td>
</tr>
<tr>
<td class="px-2 py-2 font-bold text-muted">Owned since</td>
<td>
@if(!is_null($service_extras[0]->owned_since))
{{ date_format(new DateTime($service_extras[0]->owned_since), 'jS F Y') }}
@if(!is_null($misc_data->owned_since))
{{ date_format(new DateTime($misc_data->owned_since), 'jS F Y') }}
@endif
</td>
</tr>
<tr>
<td class="px-2 py-2 font-bold text-muted">Next due date</td>
<td>{{Carbon\Carbon::parse($service_extras[0]->next_due_date)->diffForHumans()}}
({{Carbon\Carbon::parse($service_extras[0]->next_due_date)->format('d/m/Y')}})
<td>{{Carbon\Carbon::parse($misc_data->price->next_due_date)->diffForHumans()}}
({{Carbon\Carbon::parse($misc_data->price->next_due_date)->format('d/m/Y')}})
</td>
</tr>
<tr>
<td class="px-2 py-2 font-bold text-muted">Inserted</td>
<td>
@if(!is_null($service_extras[0]->created_at))
{{ date_format(new DateTime($service_extras[0]->created_at), 'jS M y g:i a') }}
@if(!is_null($misc_data->created_at))
{{ date_format(new DateTime($misc_data->created_at), 'jS M y g:i a') }}
@endif
</td>
</tr>
<tr>
<td class="px-2 py-2 font-bold text-muted">Updated</td>
<td>
@if(!is_null($service_extras[0]->updated_at))
{{ date_format(new DateTime($service_extras[0]->updated_at), 'jS M y g:i a') }}
@if(!is_null($misc_data->updated_at))
{{ date_format(new DateTime($misc_data->updated_at), 'jS M y g:i a') }}
@endif
</td>
</tr>
@ -70,7 +70,7 @@
class="btn btn-success btn-sm mx-2">
Go back
</a>
<a href="{{ route('misc.edit', $service_extras[0]->service_id) }}"
<a href="{{ route('misc.edit', $misc_data->id) }}"
class="btn btn-primary btn-sm mx-2">
Edit
</a>