Commit 5e4e0c8e authored by beardedwarrior's avatar beardedwarrior

added resources for json api responses, moved some of the computations to soft…

added resources for json api responses, moved some of the computations to soft attributes, moved the qrcode generation into the project model
parent edf0d4fb
......@@ -2,10 +2,9 @@
namespace App\Http\Controllers;
use App\Deposit;
use App\Http\Resources\ProjectResource;
use App\Project;
use Illuminate\Http\Request;
use Monero\Wallet;
use SimpleSoftwareIO\QrCode\Facades\QrCode;
class FundingController extends Controller
......@@ -13,11 +12,17 @@ class FundingController extends Controller
/**
* Shows all projects
*
* @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory
* @param Request $request
* @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\Resources\Json\AnonymousResourceCollection|\Illuminate\View\View
*/
public function index()
public function index(Request $request)
{
$projects = Project::all();
$projects = Project::paginate(15);
// If the request has header `Accept: */json`, return JSON
if ($request->wantsJson())
{
return ProjectResource::collection($projects);
}
return view('projects.index')
->with('projects', $projects);
}
......@@ -27,23 +32,21 @@ class FundingController extends Controller
*
* @param $paymentId
*
* @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory
* @return ProjectResource|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function show($paymentId)
public function show(Request $request, $paymentId)
{
$project = Project::where('payment_id', $paymentId)->first();
if (!$project) {
abort(404);
}
$contributions = $project->deposits->count();
$amountReceived = $project->deposits->sum('amount');
$percentage = round($amountReceived / $project->target_amount * 100);
if ($request->wantsJson())
{
return new ProjectResource($project);
}
$qrcode = QrCode::format('png')->size(100)->generate($project->uri);
return view('projects.show')
->with('project', $project)
->with('contributions', $contributions)
->with('percentage', $percentage)
->with('qrcode', $qrcode)
->with('amount_received', $amountReceived);
->with('qrcode', $qrcode);
}
}
<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\ResourceCollection;
class ProjectCollection extends ResourceCollection
{
/**
* Transform the resource collection into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return [
'data' => $this->collection,
'links' => [
'self' => 'link-value',
],
];
}
}
<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\JsonResource;
class ProjectResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return [
'payment_id' => $this->payment_id,
'status' => $this->status,
'amount_received' => $this->amount_received,
'target_amount' => $this->target_amount,
'percentage_funded' => $this->percentage_funded,
];
}
}
......@@ -3,10 +3,10 @@
namespace App;
use Illuminate\Database\Eloquent\Model;
use Monero\Wallet;
use SimpleSoftwareIO\QrCode\Facades\QrCode;
/**
* App\Project
* App\ProjectResource
*
* @property int $id
* @property string $payment_id
......@@ -17,6 +17,8 @@ use Monero\Wallet;
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Deposit[] $deposits
* @property-read mixed $amount_received
* @property-read string $uri
* @property-read int $percentage_funded
* @property-read int $contributions
* @method static \Illuminate\Database\Eloquent\Builder|\App\Project whereCreatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Project whereId($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Project wherePaymentId($value)
......@@ -42,4 +44,16 @@ class Project extends Model
public function getUriAttribute() {
return 'monero:'.env('WALLET_ADDRESS').'tx_payment_id='.$this->payment_id;
}
public function getPercentageFundedAttribute() {
return round($this->amount_received / $this->target_amount * 100);
}
public function getContributionsAttribute() {
return $this->deposits->count() ?? 0;
}
public function getQrCodeAttribute() {
return QrCode::format('png')->size(500)->generate($this->uri);
}
}
......@@ -12,7 +12,7 @@
<tr>
<td><a href='{!! url('/projects/'.$project->payment_id); !!}'>{{ $project->payment_id }}</a></td>
<td>{{$project->status}}</td>
<td>{{$project->amountReceived}} XMR</td>
<td>{{$project->amount_received}} XMR</td>
<td>{{$project->target_amount}} XMR</td>
</tr>
@endforeach
......
XMR {{$amount_received}} / XMR {{$project->target_amount}} Target
XMR {{$project->amount_received}} / XMR {{$project->target_amount}} Target
{{$contributions}} contributions made. {{$percentage}}%
{{$project->contributions}} contributions made. {{$project->percentage_funded}}%
<br>
{!! QrCode::size(400)->generate($project->uri); !!}
\ No newline at end of file
<img src="data:image/png;base64,{!! base64_encode($project->qrcode) !!}">
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment