Skip to content

Instantly share code, notes, and snippets.

@elinardo10
Last active March 29, 2019 04:20
Show Gist options
  • Save elinardo10/8a673f5a86d51f447ab8deb633d46b85 to your computer and use it in GitHub Desktop.
Save elinardo10/8a673f5a86d51f447ab8deb633d46b85 to your computer and use it in GitHub Desktop.
<?php
namespace App\Http\Controllers\Api;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\User;
use App\Models\Specialty;
use Illuminate\Support\Facades\Hash;
use App\Http\Requests\UserformRequest;
use Intervention\Image\ImageManagerStatic as Image;
use Illuminate\Support\Facades\Storage;
use File;
class UserController extends Controller
{
private $path = 'app/public/img/users/';
public function __construct()
{
$this->middleware('auth:api');
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
//$this->authorize('isAdmin');
$users = User::orderBy('name','ASC')->get();
return $users->toArray();
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function updateUser(UserformRequest $request, $id)
{
$user = User::findOrFail($id);
$this->authorize('isAdmin', $user);
$this->validate($request,[
'name' => 'required|string|max:191',
'email' => 'required|string|email|max:191|unique:users,email,'.$user->id,
'password' => 'sometimes|min:6'
]);
$user->update($request->except('photo'));
return ['message' => 'Updated the user info'];
}
public function profile(){
return auth('api')->user();
//return auth('api')->user()->load('specialties'); //posso usar assim tbm e comento o with do User
}
public function updateProfile(UserformRequest $request)
{
$user = auth('api')->user();
$data = $request->all();
if(!empty($request->password)){
$request->merge(['password' => Hash::make($request['password'])]);
}
//$currentPhoto = $user->photo;
if(isset($data['photo'])){
$image = $request->photo;
$imageName = time().'_avatar_'.$user->id;
if (!file_exists('app/public/img/users/'))
{
Storage::makeDirectory('public/img/users/', 0777 , true);
}
Image::make($request->photo)->save( storage_path( 'app/public/img/users/' . $imageName, base64_decode($image)));
$request->merge(['photo' => $imageName]);
//exclução da imagem alterada até q em fim.. ufaaa!
if($user->photo){
$userPhoto = str_replace(asset('/'),'',$user->photo);
if(file_exists($userPhoto)){
@unlink($userPhoto);
}
}
}
$user->birth = ($request->birth) ? date('Y-m-d', strtotime($request->birth)) : null;
$user->update($request->all());
//$user = User::findOrFail($id);
foreach ($user->specialties()->get() as $key => $v) {
$specialty = Specialty::where('name', $v['name'])->first();
$user->specialties()->detach($specialty);
}
$user->specialties()->attach($request->specialties);
return $user;
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
$users = User::find($id);
$this->authorize('isAdmin', $users);
if ($users->photo != null){
$userPhoto = str_replace(asset('/'),'',$users->photo);
if(file_exists($userPhoto)){
unlink($userPhoto);
}
}
$users->delete();
return response()->json($users, 204);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment