-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathClusterExportController.php
More file actions
68 lines (53 loc) · 2.45 KB
/
Copy pathClusterExportController.php
File metadata and controls
68 lines (53 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php
namespace App\Http\Controllers;
use App\Services\ClusterService;
use App\Services\ExportServices\ClusterExportService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Symfony\Component\HttpFoundation\StreamedResponse;
class ClusterExportController extends Controller {
public function __construct(
private ClusterService $clusterService,
private ClusterExportService $clusterExportService,
) {}
public function download(Request $request): StreamedResponse|JsonResponse {
$format = $request->get('format', 'excel');
if ($format === 'csv') {
return $this->downloadCsv($request);
}
if ($format === 'json') {
return $this->downloadJson($request);
}
return $this->downloadExcel($request);
}
public function downloadExcel(Request $request): StreamedResponse {
$clusters = $this->clusterService->getAllForExport();
$this->clusterExportService->createSpreadSheetFromTemplate($this->clusterExportService->getTemplatePath());
$this->clusterExportService->setClusterData($clusters);
$this->clusterExportService->setExportingData();
$this->clusterExportService->writeClusterData();
$pathToSpreadSheet = $this->clusterExportService->saveSpreadSheet();
return Storage::download($pathToSpreadSheet, 'cluster_export_'.now()->format('Ymd_His').'.xlsx');
}
public function downloadCsv(Request $request): StreamedResponse {
$clusters = $this->clusterService->getAllForExport();
$this->clusterExportService->setClusterData($clusters);
$this->clusterExportService->setExportingData();
$headers = ['Cluster Name', 'Manager', 'Mini Grids Count', 'Villages Count', 'Created At', 'Updated At'];
$csvPath = $this->clusterExportService->saveCsv($headers);
return Storage::download($csvPath, 'cluster_export_'.now()->format('Ymd_His').'.csv');
}
public function downloadJson(Request $request): JsonResponse {
$clusters = $this->clusterService->getAllForExport();
$this->clusterExportService->setClusterData($clusters);
$jsonData = $this->clusterExportService->exportDataToArray();
return response()->json([
'data' => $jsonData,
'meta' => [
'total' => count($jsonData),
'exported_at' => now()->toISOString(),
],
]);
}
}