Skip to content

Commit

Permalink
Update photo exports (#66)
Browse files Browse the repository at this point in the history
* Update photos export JSON format

* Add photos export CSV format

* Add headings for CSV export
  • Loading branch information
GeniJaho authored Aug 19, 2024
1 parent 39e86c2 commit 30eac63
Show file tree
Hide file tree
Showing 10 changed files with 645 additions and 18 deletions.
3 changes: 2 additions & 1 deletion app/Actions/Photos/ExportPhotosAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ public function run(User $user): Generator
->filter($user->settings->photo_filters)
->with(['photoItems' => fn (Builder $q) => $q
->with('item:id,name')
->with('tags:id,name'),
->with('tags:id,tag_type_id,name')
->with('tags.type:id,name'),
])
->lazyById();

Expand Down
6 changes: 5 additions & 1 deletion app/DTO/PhotoExport.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Models\Photo;
use App\Models\PhotoItem;
use App\Models\Tag;
use Illuminate\Support\Collection;
use Spatie\LaravelData\Data;

Expand Down Expand Up @@ -39,7 +40,10 @@ public static function fromModel(Photo $photo): static
'recycled' => $photoItem->recycled,
'deposit' => $photoItem->deposit,
'quantity' => $photoItem->quantity,
'tags' => $photoItem->tags->pluck('name')->toArray(),
'tags' => $photoItem->tags->map(fn (Tag $tag): array => [
'type' => $tag->type->name,
'name' => $tag->name,
])->all(),
]),
);
}
Expand Down
42 changes: 42 additions & 0 deletions app/Exports/PhotosCsvExport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace App\Exports;

use App\DTO\PhotoExport;
use Generator;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\FromGenerator;
use Maatwebsite\Excel\Concerns\WithHeadings;

readonly class PhotosCsvExport implements FromGenerator, WithHeadings
{
use Exportable;

/**
* @param Generator<PhotoExport> $photos
*/
public function __construct(private Generator $photos)
{
}

public function generator(): Generator
{
return $this->photos;
}

/**
* @return string[]
*/
public function headings(): array
{
return [
'ID',
'Original File Name',
'Latitude',
'Longitude',
'Taken At Local',
'Created At',
'Items',
];
}
}
10 changes: 9 additions & 1 deletion app/Http/Controllers/Photos/ExportPhotosController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,27 @@
namespace App\Http\Controllers\Photos;

use App\Actions\Photos\ExportPhotosAction;
use App\Exports\PhotosCsvExport;
use App\Http\Controllers\Controller;
use App\Models\User;
use Illuminate\Http\Response;
use Maatwebsite\Excel\Excel;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\StreamedResponse;

class ExportPhotosController extends Controller
{
public function __invoke(ExportPhotosAction $action): StreamedResponse
public function __invoke(ExportPhotosAction $action): StreamedResponse|BinaryFileResponse|Response
{
/** @var User $user */
$user = auth()->user();

$photos = $action->run($user);

if (request()->input('format') === 'csv') {
return (new PhotosCsvExport($photos))->download('photos.csv', Excel::CSV);
}

return response()->streamJson(['photos' => $photos], 200, [
'Content-Type' => 'application/json',
'Content-Disposition' => 'attachment; filename="photos.json"',
Expand Down
3 changes: 3 additions & 0 deletions app/Models/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;

/**
* @property TagType $type
*/
class Tag extends Model
{
use HasFactory;
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"laravel/socialite": "^5.11",
"laravel/tinker": "^2.8",
"league/flysystem-aws-s3-v3": "^3.0",
"maatwebsite/excel": "^3.1",
"spatie/laravel-data": "^4.0",
"tightenco/ziggy": "^1.0"
},
Expand Down
Loading

0 comments on commit 30eac63

Please sign in to comment.