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

View File

@ -4,6 +4,7 @@ namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Cache;
class Misc extends Model class Misc extends Model
{ {
@ -14,4 +15,25 @@ class Misc extends Model
protected $table = 'misc_services'; protected $table = 'misc_services';
protected $fillable = ['id', 'name', 'owned_since']; 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-app-layout>
<x-slot name="header"> <x-slot name="header">
Edit {{ $misc[0]->name }} Edit {{ $misc_data->name }}
</x-slot> </x-slot>
<div class="container"> <div class="container">
<x-card class="shadow mt-3"> <x-card class="shadow mt-3">
@ -11,7 +11,7 @@
Go back Go back
</x-back-button> </x-back-button>
<x-errors-alert></x-errors-alert> <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 @csrf
@method('PUT') @method('PUT')
<div class="row"> <div class="row">
@ -20,7 +20,7 @@
<div class="input-group-prepend"><span class="input-group-text">Name</span></div> <div class="input-group-prepend"><span class="input-group-text">Name</span></div>
<input type="text" <input type="text"
class="form-control" 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 }} @error('name') <span class="text-red-500">{{ $message }}
</span>@enderror </span>@enderror
</div> </div>
@ -30,17 +30,17 @@
<x-slot name="title">Price</x-slot> <x-slot name="title">Price</x-slot>
<x-slot name="name">price</x-slot> <x-slot name="name">price</x-slot>
<x-slot name="step">0.01</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> </x-number-input>
</div> </div>
<div class="col-md-3 mb-3"> <div class="col-md-3 mb-3">
<x-term-select> <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> </x-term-select>
</div> </div>
<div class="col-md-2 mb-3"> <div class="col-md-2 mb-3">
<x-currency-select> <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> </x-currency-select>
</div> </div>
</div> </div>
@ -49,20 +49,20 @@
<x-date-input> <x-date-input>
<x-slot name="title">Owned since</x-slot> <x-slot name="title">Owned since</x-slot>
<x-slot name="name">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> </x-date-input>
</div> </div>
<div class="col-12 col-md-4 mb-3"> <div class="col-12 col-md-4 mb-3">
<x-date-input> <x-date-input>
<x-slot name="title">Next due date</x-slot> <x-slot name="title">Next due date</x-slot>
<x-slot name="name">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> </x-date-input>
</div> </div>
</div> </div>
<div class="form-check mt-2"> <div class="form-check mt-2">
<input class="form-check-input" name="is_active" type="checkbox" <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"> <label class="form-check-label">
I still have this service I still have this service
</label> </label>

View File

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