initial commit
This commit is contained in:
137
app/Http/Controllers/AveragesController.php
Normal file
137
app/Http/Controllers/AveragesController.php
Normal file
@@ -0,0 +1,137 @@
|
||||
<?php
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Carbon;
|
||||
use App\Models\Sale;
|
||||
use App\Support\ChartJsFormatter;
|
||||
|
||||
class AveragesController extends Controller
|
||||
{
|
||||
public function salePriceByCounty(Request $request)
|
||||
{
|
||||
$range = $request->query('range', 'all');
|
||||
|
||||
$query = Sale::selectRaw('property_county, AVG(sale_price) as avg_price')
|
||||
->whereNotNull('sale_price');
|
||||
|
||||
if ($range !== 'all') {
|
||||
$query->where('sale_date', '>=', $this->getDateRange($range));
|
||||
}
|
||||
|
||||
$data = $query->groupBy('property_county')
|
||||
->orderByDesc('avg_price')
|
||||
->limit(20)
|
||||
->get();
|
||||
|
||||
return response()->json(
|
||||
ChartJsFormatter::barChart(
|
||||
$data->pluck('property_county'),
|
||||
[[
|
||||
'label' => 'Avg Sale Price by County',
|
||||
'data' => $data->pluck('avg_price')->map(fn($v) => (int) $v),
|
||||
'backgroundColor' => 'rgba(54, 162, 235, 0.5)',
|
||||
'borderColor' => 'rgba(54, 162, 235, 1)',
|
||||
]]
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
public function salePriceByZip(Request $request)
|
||||
{
|
||||
$range = $request->query('range', 'all');
|
||||
|
||||
$query = Sale::selectRaw('zip_postal_code, AVG(sale_price) as avg_price')
|
||||
->whereNotNull('sale_price');
|
||||
|
||||
if ($range !== 'all') {
|
||||
$query->where('sale_date', '>=', $this->getDateRange($range));
|
||||
}
|
||||
|
||||
$data = $query->groupBy('zip_postal_code')
|
||||
->orderByDesc('avg_price')
|
||||
->limit(20)
|
||||
->get();
|
||||
|
||||
return response()->json(
|
||||
ChartJsFormatter::barChart(
|
||||
$data->pluck('zip_postal_code'),
|
||||
[[
|
||||
'label' => 'Avg Sale Price by Zip Code',
|
||||
'data' => $data->pluck('avg_price')->map(fn($v) => (int) $v),
|
||||
'backgroundColor' => 'rgba(75, 192, 192, 0.5)',
|
||||
'borderColor' => 'rgba(75, 192, 192, 1)',
|
||||
]]
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
public function salePriceBySubtype(Request $request)
|
||||
{
|
||||
$range = $request->query('range', 'all');
|
||||
|
||||
$query = Sale::selectRaw('property_sub_type, AVG(sale_price) as avg_price')
|
||||
->whereNotNull('sale_price');
|
||||
|
||||
if ($range !== 'all') {
|
||||
$query->where('sale_date', '>=', $this->getDateRange($range));
|
||||
}
|
||||
|
||||
$data = $query->groupBy('property_sub_type')
|
||||
->orderByDesc('avg_price')
|
||||
->limit(20)
|
||||
->get();
|
||||
|
||||
return response()->json(
|
||||
ChartJsFormatter::barChart(
|
||||
$data->pluck('property_sub_type'),
|
||||
[[
|
||||
'label' => 'Avg Sale Price by Property Sub-Type',
|
||||
'data' => $data->pluck('avg_price')->map(fn($v) => (int) $v),
|
||||
'backgroundColor' => 'rgba(255, 206, 86, 0.5)',
|
||||
'borderColor' => 'rgba(255, 206, 86, 1)',
|
||||
]]
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
public function salePriceByCity(Request $request)
|
||||
{
|
||||
$range = $request->query('range', 'all');
|
||||
|
||||
$query = Sale::selectRaw('city, AVG(sale_price) as avg_price')
|
||||
->whereNotNull('sale_price');
|
||||
|
||||
if ($range !== 'all') {
|
||||
$query->where('sale_date', '>=', $this->getDateRange($range));
|
||||
}
|
||||
|
||||
$data = $query->groupBy('city')
|
||||
->orderByDesc('avg_price')
|
||||
->limit(20)
|
||||
->get();
|
||||
|
||||
return response()->json(
|
||||
ChartJsFormatter::barChart(
|
||||
$data->pluck('city'),
|
||||
[[
|
||||
'label' => 'Avg Sale Price by City',
|
||||
'data' => $data->pluck('avg_price')->map(fn($v) => (int) $v),
|
||||
'backgroundColor' => 'rgba(153, 102, 255, 0.5)',
|
||||
'borderColor' => 'rgba(153, 102, 255, 1)',
|
||||
]]
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
private function getDateRange(string $range)
|
||||
{
|
||||
return match ($range) {
|
||||
'6m' => Carbon::now()->subMonths(6),
|
||||
'12m' => Carbon::now()->subMonths(12),
|
||||
'2y' => Carbon::now()->subYears(2),
|
||||
'5y' => Carbon::now()->subYears(5),
|
||||
default => Carbon::minValue(),
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user