Skip to content

Commit

Permalink
WIP: Use trait to upload and delete files user (#1042)
Browse files Browse the repository at this point in the history
Co-authored-by: Abah Roland <[email protected]>
  • Loading branch information
arifpriadi and vickyrolanda authored Oct 23, 2024
1 parent b3b4316 commit 99736e2
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 45 deletions.
30 changes: 14 additions & 16 deletions app/Http/Controllers/User/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,21 @@

namespace App\Http\Controllers\User;

use App\Http\Controllers\Controller;
use App\Http\Requests\UserRequest;
use App\Http\Requests\UserUpdateRequest;
use App\Models\Pengurus;
use App\Models\User;
use App\Models\Pengurus;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Spatie\Permission\Models\Role;
use Yajra\DataTables\DataTables;
use App\Traits\HandlesFileUpload;
use App\Http\Requests\UserRequest;
use Spatie\Permission\Models\Role;
use App\Http\Controllers\Controller;
use App\Http\Requests\UserUpdateRequest;

class UserController extends Controller
{
use HandlesFileUpload;

/**
* Display a listing of the resource.
*
Expand Down Expand Up @@ -82,11 +85,9 @@ public function store(UserRequest $request)
try {
$status = ! empty($request->status) ? 1 : 1;
$request->merge(['status' => $status]);
$user = User::create($request->validated());
if ($request->hasFile('image')) {
$user->uploadImage($request->image);
}

$input = $request->validated();
$this->handleFileUpload($request, $input, 'image', 'user', false);
$user = User::create($input);
$roles = $request->input('role') ? $request->input('role') : [];
$user->assignRole($roles);

Expand Down Expand Up @@ -138,13 +139,10 @@ public function edit($id)
public function update(UserRequest $request, $id)
{
try {
$input = $request->validated();
$user = User::findOrFail($id);

$user->update($request->validated());
if ($request->hasFile('image')) {
$user->uploadImage($request->image);
}

$this->handleFileUpload($request, $input, 'image', 'user', false);
$user->update($input);
if (! empty($request->role)) {
$roles = $request->input('role') ? $request->input('role') : [];
$user->syncRoles($roles);
Expand Down
41 changes: 15 additions & 26 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,23 @@

namespace App\Models;

use Illuminate\Auth\Authenticatable as AuthenticableTrait;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Storage;
use Intervention\Image\Facades\Image;
use Spatie\Permission\Traits\HasRoles;
use App\Traits\HandlesResourceDeletion;
use Illuminate\Support\Facades\Storage;
use Tymon\JWTAuth\Contracts\JWTSubject;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Auth\Authenticatable as AuthenticableTrait;

class User extends Authenticatable implements JWTSubject
{
use AuthenticableTrait;
use HasRoles;
use Notifiable;
use HandlesResourceDeletion;

/**
* Default password.
Expand All @@ -71,6 +73,15 @@ class User extends Authenticatable implements JWTSubject
'pengurus_id',
];

/**
* Daftar field-file yang harus dihapus.
*
* @var array
*/
protected $resources = [
'image',
];

/**
* {@inheritDoc}
*/
Expand Down Expand Up @@ -105,28 +116,6 @@ public function getFotoAttribute()
return $this->attributes['image'] ? Storage::url('user/'.$this->attributes['image']) : null;
}

/**
* Uploads an image.
*
* @param <type> $image The image
* @return <type> ( description_of_the_return_value )
*/
public function uploadImage($image)
{
$extension = $image->getClientOriginalExtension();
$path = storage_path('app/public/user/');

if (! file_exists($path)) {
File::makeDirectory($path, 0777, true);
}

$name = $this->id.'.'.$extension;
$img = Image::make($image->getRealPath());
$img->save($path.$name);

return $this->update(['image' => $name]);
}

public function scopeSuspend($query, $email)
{
return $query->where('email', $email)->where('status', 0)->get();
Expand Down
7 changes: 4 additions & 3 deletions catatan_rilis.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Terima kasih pada @uddinmtm telah ikut berkontribusi.
13. [#1067](https://github.com/OpenSID/OpenDK/issues/1067) Penyesuaian use trait to upload and delete files regulasi.
14. [#1068](https://github.com/OpenSID/OpenDK/issues/1068) Penyesuaian use trait to upload and delete files artikel.
15. [#1069](https://github.com/OpenSID/OpenDK/issues/1069) Penyesuaian use trait to upload and delete files dokumen.
16. [#1070](https://github.com/OpenSID/OpenDK/issues/1070) Penyesuaian Use trait to upload and delete files medsos.
17. [#1071](https://github.com/OpenSID/OpenDK/issues/1071) Penyesuaian Use trait to upload and delete files sinergi program.
18. [#1072](https://github.com/OpenSID/OpenDK/issues/1072) Penyesuaian Use trait to upload and delete files pengurus.
16. [#1070](https://github.com/OpenSID/OpenDK/issues/1070) Penyesuaian use trait to upload and delete files medsos.
17. [#1071](https://github.com/OpenSID/OpenDK/issues/1071) Penyesuaian use trait to upload and delete files sinergi program.
18. [#1072](https://github.com/OpenSID/OpenDK/issues/1072) Penyesuaian use trait to upload and delete files pengurus.
19. [#1073](https://github.com/OpenSID/OpenDK/issues/1073) Peneysuaian use trait to upload and delete files use.

0 comments on commit 99736e2

Please sign in to comment.