Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
alihardan committed Apr 6, 2024
1 parent f9f7c9e commit b2ef144
Show file tree
Hide file tree
Showing 8 changed files with 422 additions and 160 deletions.
21 changes: 1 addition & 20 deletions app/Http/Controllers/CompanyController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,12 @@

class CompanyController extends Controller
{

public function websiteIndex(Request $request)
{
$companies = Company::latest()->get(['id', 'slug', 'title_fa', 'title_en']);
return $companies;

}

/**
* Display a listing of the resource.
*/
public function index(Request $request)
{
$companies = QueryBuilder::for(Company::class)
->allowedSorts(['id', 'title_fa'])
->allowedFilters(['id', 'title_fa'])
->allowedFields(['id', 'title_fa']);

if ($request->input('page')) {
$companies = $companies->jsonPaginate();
} else {
$companies = $companies->get();
}

return $companies;
return Company::latest()->get(['id', 'slug', 'title_fa', 'title_en']);
}

/**
Expand Down
88 changes: 33 additions & 55 deletions app/Http/Controllers/GameController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,61 +14,21 @@
class GameController extends Controller
{

private function removePhotos($id, $photos)
{
foreach ($photos as $photo_id) {
Storage::disk('public')->delete('gamePhotos/' . $id . '/' . $photo_id . '.jpg');
}
}

private function uploadPhotos($id, $photos)
{
foreach ($photos as $photo) {
if (isset($photo['dataURL'])) {
$file = base64_decode(preg_replace(
'#^data:image/\w+;base64,#i',
'',
$photo['dataURL']
));
Storage::disk('public')->put('gamePhotos/' . $id . '/' . $photo['id'] . '.jpg', $file);
}
}
}

/**
* Display a listing of the resource.
*/
public function index(Request $request)
{
$games = QueryBuilder::for(Game::class)
->allowedSorts(['id', 'created_at'])
->allowedFilters(['games'])
->allowedFields(['id', 'games', 'slug',]);

if ($request->input('page')) {
$games = $games->jsonPaginate();
} else {
$games = $games->get();
}

return $games;
return Game::latest()->get(['id', 'slug']);
}

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

if (isset($input['photos'])) {
foreach (array_keys($input['photos']) as $index) {
if (isset($input['photos'][$index]['dataURL'])) {
unset($input['photos'][$index]['dataURL']);
$input['photos'][$index]['uploaded_at'] = Carbon::now();
}
}
}
$input = $request->except(['producers', 'publishers', 'contributes', 'photos']);

$game = Game::create($input);
$game->producers()->attach($request->producers, ['relation' => 'producer']);
Expand All @@ -77,7 +37,13 @@ public function store(Request $request)
$game->contributes()->createMany($request->contributes);


$this->uploadPhotos($game->id, $request->photos);
foreach ($request->photos as $photo) {
if (isset($photo["dataURL"]))
$game->addMediaFromBase64($photo["dataURL"])
->toMediaCollection();
}


return $game;
}

Expand All @@ -94,25 +60,39 @@ public function show($id)
$game->publishers = $game->publishers->map->id->values();
$game->unsetRelation('publishers');

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

return [...$game->toArray(), 'photos' => $photos];
}

/**
* Update the specified resource in storage.
*/
public function update(Request $request, Game $game)
{
if ($request->photos)
$this->removePhotos($game->id, array_diff(array_column($game->photos, 'id'), array_column($request->photos, 'id')));

$input = $request->except(['producers', 'publishers', 'contributes']);
$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');

if (isset($input['photos'])) {
foreach (array_keys($input['photos']) as $index) {
if (isset($input['photos'][$index]['dataURL'])) {
unset($input['photos'][$index]['dataURL']);
$input['photos'][$index]['uploaded_at'] = Carbon::now();
}
// Remove other items from media library
foreach ($game->getMedia() as $media) {
if (!in_array($media->id, $toKeepIds))
$media->delete();
}

// Upload photos
foreach ($request->photos as $photo) {
if (isset($photo["dataURL"]))
$game->addMediaFromBase64($photo["dataURL"])->toMediaCollection();
}
}

Expand All @@ -138,8 +118,6 @@ public function update(Request $request, Game $game)
$game->contributes()->whereIn('id', $contributesToDelete)->delete();

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

$this->uploadPhotos($game->id, $request->photos);
}

/**
Expand Down
89 changes: 35 additions & 54 deletions app/Http/Controllers/ItemController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,35 +16,16 @@ class ItemController extends Controller
*/
public function index(Request $request)
{
$items = QueryBuilder::for(Item::class)
->allowedSorts(['id', 'created_at'])
->allowedFilters(['items'])
->allowedFields(['id', 'items', 'slug',]);

if ($request->input('page')) {
$items = $items->jsonPaginate();
} else {
$items = $items->get();
}

return $items;
return Item::latest()->get(['id', 'slug']);
}

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

if (isset($input['photos'])) {
foreach (array_keys($input['photos']) as $index) {
if (isset($input['photos'][$index]['dataURL'])) {
unset($input['photos'][$index]['dataURL']);
$input['photos'][$index]['uploaded_at'] = Carbon::now();
}
}
}
$input = $request->except(['producers', 'publishers', 'contributes', 'photos']);


$item = Item::create($input);
$item->producers()->attach($request->producers, ['relation' => 'producer']);
Expand All @@ -53,7 +34,12 @@ public function store(Request $request)
$item->contributes()->createMany($request->contributes);


$this->uploadPhotos($item->id, $request->photos);
foreach ($request->photos as $photo) {
if (isset($photo["dataURL"]))
$item->addMediaFromBase64($photo["dataURL"])
->toMediaCollection();
}

return $item;
}

Expand All @@ -70,22 +56,40 @@ public function show($id)
$item->publishers = $item->publishers->map->id->values();
$item->unsetRelation('publishers');

return $item;

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

return [...$item->toArray(), 'photos' => $photos];
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, Item $item)
{

$input = $request->except(['producers', 'publishers', 'contributes']);
$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');

if (isset($input['photos'])) {
foreach (array_keys($input['photos']) as $index) {
if (isset($input['photos'][$index]['dataURL'])) {
unset($input['photos'][$index]['dataURL']);
$input['photos'][$index]['uploaded_at'] = Carbon::now();
}
// Remove other items from media library
foreach ($item->getMedia() as $media) {
if (!in_array($media->id, $toKeepIds))
$media->delete();
}

// Upload photos
foreach ($request->photos as $photo) {
if (isset($photo["dataURL"]))
$item->addMediaFromBase64($photo["dataURL"])->toMediaCollection();
}
}

Expand All @@ -111,8 +115,6 @@ public function update(Request $request, Item $item)
$item->contributes()->whereIn('id', $contributesToDelete)->delete();

$item->contributes()->createMany($contributesToAdd);

$this->uploadPhotos($item->id, $request->photos);
}

/**
Expand All @@ -122,25 +124,4 @@ public function destroy(Item $item)
{
$item->delete();
}

private function removePhotos($id, $photos)
{
foreach ($photos as $photo_id) {
Storage::disk('public')->delete('item-photos/' . $id . '/' . $photo_id . '.jpg');
}
}

private function uploadPhotos($id, $photos)
{
foreach ($photos as $photo) {
if (isset($photo['dataURL'])) {
$file = base64_decode(preg_replace(
'#^data:image/\w+;base64,#i',
'',
$photo['dataURL']
));
Storage::disk('public')->put('item-photos/' . $id . '/' . $photo['id'] . '.jpg', $file);
}
}
}
}
59 changes: 59 additions & 0 deletions app/Http/Controllers/UserController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace App\Http\Controllers;

use App\Models\User;
use Illuminate\Http\Request;
use Spatie\QueryBuilder\QueryBuilder;
use Illuminate\Validation\Rule;
use App\Actions\Fortify\PasswordValidationRules;
use App\Models\QuizSubmission;
use Illuminate\Support\Facades\Hash;
use Spatie\QueryBuilder\AllowedFilter;
use Spatie\QueryBuilder\Filters\Filter;
use Illuminate\Database\Eloquent\Builder;



class UserController extends Controller
{


/**
* Display a listing of the resource.
*/
public function index(Request $request)
{
return User::get(['id', 'email', 'name']);
}


/**
* Store a newly created resource in storage.
*/
public function store(Request $request)
{
}

/**
* Display the specified resource.
*/
public function show(User $user)
{
}

/**
* Update the specified resource in storage.
*/
public function update(Request $request, User $user)
{
}

/**
* Remove the specified resource from storage.
*/
public function destroy(User $user)
{
//
}
}
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"laravel/framework": "^11.0",
"laravel/sanctum": "^4.0",
"laravel/tinker": "^2.9",
"nutgram/laravel": "^1.4",
"spatie/laravel-medialibrary": "^11.4"
},
"require-dev": {
Expand Down
Loading

0 comments on commit b2ef144

Please sign in to comment.