Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
alihardan committed Jun 10, 2024
1 parent 76aa101 commit ec3db14
Show file tree
Hide file tree
Showing 7 changed files with 222 additions and 64 deletions.
113 changes: 50 additions & 63 deletions app/Http/Controllers/GameController.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,32 +20,40 @@ class GameController extends Controller
*/
public function index(Request $request)
{
return Game::latest()->get(['id', 'slug']);
return Game::latest()->get(['id', 'slug', 'title_fa', 'title_en', 'games'])->transform(function ($game) {
return [
'id' => $game->id,
'slug' => $game->slug,
'title_fa' => $game->title_fa ?? $game->games[0]['title_fa'][0] ?? '',
'title_en' => $game->title_en ?? $game->games[0]['title_en'][0] ?? '',
];
});
}

/**
* Store a newly created resource in storage.
*/
public function store(Request $request)
{
$input = $request->except(['producers', 'publishers', 'contributes', 'photos']);
return DB::transaction(function () use ($request) {
$input = $request->except(['producers', 'publishers', 'contributes', 'photos']);

$game = Game::create($input);
$game->producers()->attach($request->producers, ['relation' => 'producer']);
$game->publishers()->attach($request->publishers, ['relation' => 'publisher']);
$game = Game::create($input);
$game->producers()->attach($request->producers, ['relation' => 'producer']);
$game->publishers()->attach($request->publishers, ['relation' => 'publisher']);
$game->contributes()->createMany($request->contributes);

$game->contributes()->createMany($request->contributes);


foreach ($request->photos as $photo) {
if (isset($photo["dataURL"]))
$game->addMediaFromBase64($photo["dataURL"])
->usingFileName(str_replace('/tmp/', '', tempnam(sys_get_temp_dir(), 'media-library')) . '.jpg')
->toMediaCollection();
}
foreach ($request->photos as $photo) {
if (isset($photo["dataURL"]))
$game->addMediaFromBase64($photo["dataURL"])
->usingFileName(str_replace('/tmp/', '', tempnam(sys_get_temp_dir(), 'media-library')) . '.jpg')
->toMediaCollection();
}


return $game;
return $game;
});
}

/**
Expand All @@ -55,70 +63,49 @@ public function show($id)
{
$game = Game::with(['producers:id', 'publishers:id', 'contributes'])->findOrFail($id);

$game->producers = $game->producers->map->id->values();
$game->unsetRelation('producers');

$game->publishers = $game->publishers->map->id->values();
$game->unsetRelation('publishers');

$photos = $game->getMedia()
->map(function ($photo) {
return ["id" => $photo->id, "url" => $photo->getUrl()];
});

return [...$game->toArray(), 'photos' => $photos];
return [
...$game->toArray(),
'producers' => $game->producers->pluck('id'),
'publishers' => $game->publishers->pluck('id'),
'photos' => $game->getMedia()
->map(function ($photo) {
return ["id" => $photo->id, "url" => $photo->getUrl()];
}),
];
}

/**
* Update the specified resource in storage.
*/
public function update(Request $request, Game $game)
{
return DB::transaction(function () use ($game, $request) {
if ($request->filled('photos')) {
$photos = collect($request->photos);

$input = $request->except(['producers', 'publishers', 'contributes', 'photos']);

if ($request->filled('photos')) {
// Photos to keep in media library
$toKeep = collect(array_filter($request->photos, function ($photo) {
return !isset($photo["dataURL"]);
}));
$toKeepIds = array_column($toKeep->toArray(), 'id');
// Photos to keep in the media library
$toKeepIds = $photos->whereNotNull('id')->pluck('id');

// Remove other items from media library
foreach ($game->getMedia() as $media) {
if (!in_array($media->id, $toKeepIds))
$media->delete();
}
// Remove other items from the media library
foreach ($game->getMedia() as $media) {
if ($toKeepIds->doesntContain($media->id))
$media->delete();
}

// Upload photos
foreach ($request->photos as $photo) {
if (isset($photo["dataURL"]))
// Upload photos
$photos->whereNotNull('dataURL')->each(function ($photo) use ($game) {
$game->addMediaFromBase64($photo["dataURL"])->usingFileName(str_replace('/tmp/', '', tempnam(sys_get_temp_dir(), 'media-library')) . '.jpg')->toMediaCollection();
});
}
}

$game->update($input);
$game->producers()->syncWithPivotValues($request->producers, ['relation' => 'producer']);
$game->publishers()->syncWithPivotValues($request->publishers, ['relation' => 'publisher']);

$currentContributes = $game->contributes->toArray();
$newContributes = $request->contributes;

$contributesToAdd = array_values(array_filter($newContributes, function ($value) {
return $value["new"] ?? false;
}));
$contributesToEdit = array_values(array_filter($newContributes, function ($value) {
return !($value["new"] ?? false);
}));
$contributesToDelete = array_values(array_diff(array_column($currentContributes, 'id'), array_column($contributesToEdit, 'id')));

foreach ($contributesToEdit as $value) {
$game->contributes->firstWhere('id', $value['id'])->update($value);
}

$game->contributes()->whereIn('id', $contributesToDelete)->delete();
$game->update($request->except(['producers', 'publishers', 'contributes', 'photos']));
$game->producers()->syncWithPivotValues($request->producers, ['relation' => 'producer']);
$game->publishers()->syncWithPivotValues($request->publishers, ['relation' => 'publisher']);

$game->contributes()->createMany($contributesToAdd);
$game->contributes()->delete();
$game->contributes()->createMany($request->contributes);
});
}

/**
Expand Down
8 changes: 8 additions & 0 deletions app/Models/Game.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,28 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Spatie\Activitylog\LogOptions;
use Spatie\Activitylog\Traits\LogsActivity;
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;

class Game extends Model implements HasMedia
{
use HasFactory;
use InteractsWithMedia;
use LogsActivity;

protected $guarded = [
'id', 'created_at', 'updated_at'
];
protected $appends = ['featured_image'];
protected $hidden = ['media'];

public function getActivitylogOptions(): LogOptions
{
return LogOptions::defaults();
}

protected function casts(): array
{
return [
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"laravel/sanctum": "^4.0",
"laravel/tinker": "^2.9",
"nutgram/laravel": "^1.4",
"spatie/laravel-activitylog": "^4.8",
"spatie/laravel-medialibrary": "^11.4"
},
"require-dev": {
Expand Down
93 changes: 92 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateActivityLogTable extends Migration
{
public function up()
{
Schema::connection(config('activitylog.database_connection'))->create(config('activitylog.table_name'), function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('log_name')->nullable();
$table->text('description');
$table->nullableMorphs('subject', 'subject');
$table->nullableMorphs('causer', 'causer');
$table->json('properties')->nullable();
$table->timestamps();
$table->index('log_name');
});
}

public function down()
{
Schema::connection(config('activitylog.database_connection'))->dropIfExists(config('activitylog.table_name'));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class AddEventColumnToActivityLogTable extends Migration
{
public function up()
{
Schema::connection(config('activitylog.database_connection'))->table(config('activitylog.table_name'), function (Blueprint $table) {
$table->string('event')->nullable()->after('subject_type');
});
}

public function down()
{
Schema::connection(config('activitylog.database_connection'))->table(config('activitylog.table_name'), function (Blueprint $table) {
$table->dropColumn('event');
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class AddBatchUuidColumnToActivityLogTable extends Migration
{
public function up()
{
Schema::connection(config('activitylog.database_connection'))->table(config('activitylog.table_name'), function (Blueprint $table) {
$table->uuid('batch_uuid')->nullable()->after('properties');
});
}

public function down()
{
Schema::connection(config('activitylog.database_connection'))->table(config('activitylog.table_name'), function (Blueprint $table) {
$table->dropColumn('batch_uuid');
});
}
}

0 comments on commit ec3db14

Please sign in to comment.