Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.85 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Http\Controllers;
  4.  
  5. use App\User;
  6. use Illuminate\Http\Request;
  7. use Illuminate\Support\Facades\Hash;
  8. use Illuminate\Support\Facades\Validator;
  9. use Illuminate\Support\Facades\Storage;
  10. use Illuminate\Support\Facades\File;
  11.  
  12. class UserController extends Controller
  13. {
  14.     public function getProfile(Request $request){
  15.         $validator=Validator::make($request->all(),[
  16.             'id'=>'required|integer',
  17.         ]);
  18.         if($validator->fails()){
  19.             return ApiResponseController::errorResponse($validator->errors()->first());
  20.         }
  21.         $id=$request->id;
  22.         $profile= User::find($id);
  23.         if($profile){
  24.             if($profile->location){
  25.                 $profile->location=json_decode($profile->location,true);
  26.             }
  27.             return ApiResponseController::successfulResponse($profile);
  28.         }
  29.         return ApiResponseController::errorResponse('User Does Not Exist');
  30.     }
  31.     public function updateProfile(Request $request){
  32.         $validator=Validator::make($request->all(),[
  33.             'id'=>'required|integer',
  34.             'name'=>'string',
  35.             'email'=>'email',
  36.             'new_password'=>'string|min:6',
  37.             'password'=>'required_with:new_password',
  38.             'phone'=>'string',
  39.             'address'=>'string|required_with:latitude,longitude',
  40.             'latitude'=>'string|required_with:longitude,address',
  41.             'longitude'=>'string|required_with:latitude,address',
  42.             'profile_picture'=>'image',
  43.             'cover_picture'=>'image',
  44. //            'profile_picture'=>'image|mimes:png',
  45. //            'cover_picture'=>'image|mimes:png',
  46.         ]);
  47.         if($validator->fails()){
  48.             return ApiResponseController::errorResponse($validator->errors()->first());
  49.         }
  50.         $id=$request->id;
  51.         $user=User::find($id);
  52.         if($user) {
  53.             $attributes = [];
  54.             if ($request->new_password) {
  55.                 if (Hash::check($request->password, $user->password)) {
  56.                     $attributes['password'] = bcrypt($request->new_password);
  57.                 } else {
  58.                     return ApiResponseController::errorResponse("Wrong Password");
  59.                 }
  60.             }
  61.             if ($request->name) {
  62.                 $attributes['name'] = $request->name;
  63.             }
  64.             if ($request->email) {
  65.                 $attributes['email'] = $request->email;
  66.             }
  67.             if ($request->phone) {
  68.                 $attributes['phone'] = $request->phone;
  69.             }
  70.             if ($request->address) {
  71.                 $location = [
  72.                     'address'=>$request->address,
  73.                     'latitude'=>$request->latitude,
  74.                     'longitude'=>$request->longitude,
  75.                 ];
  76.                 $attributes['location']=\GuzzleHttp\json_encode($location);
  77.             }
  78.             if ($request->profile_picture) {
  79.                 $profile_picture = $request->file('profile_picture');
  80.                 $name = $id . '_' . time() . '.' . $profile_picture->getClientOriginalExtension();
  81.                 Storage::disk('local')->put('user-profile-pictures/' . $name, File::get($profile_picture));
  82.                 $attributes['profile_picture_url'] = env('APP_URL') . '/api/v1/profile/profile-picture/' . $name;
  83.             }
  84.             if ($request->cover_picture) {
  85.                 $cover_picture = $request->file('cover_picture');
  86.                 $name = $id . '_' . time() . '.' . $cover_picture->getClientOriginalExtension();
  87.                 Storage::disk('local')->put('user-cover-pictures/' . $name, File::get($cover_picture));
  88.                 $attributes['cover_picture_url'] = env('APP_URL') . '/api/v1/profile/cover-picture/' . $name;
  89.             }
  90.             User::where('id', $id)->update($attributes);
  91.             $user = User::find($id);
  92.             if($user->location){
  93.                 $user->location=json_decode($user->location,true);
  94.             }
  95.             return ApiResponseController::successfulResponse($user);
  96.         }
  97.         return ApiResponseController::errorResponse("User does not exist");
  98.     }
  99.     public function getProfilePicture($name){
  100.         $path='user-profile-pictures/' . $name;
  101.         if(Storage::disk('local')->exists($path)) {
  102.             $file= Storage::disk('local')->get($path);
  103.             return response($file, 200)->header('Content-Type', 'image/png');
  104.         }
  105.         return ApiResponseController::errorResponse("Invalid URL");
  106.     }
  107.     public function getCoverPicture($name){
  108.         $path='user-cover-pictures/' . $name;
  109.         if(Storage::disk('local')->exists($path)) {
  110.             $file= Storage::disk('local')->get($path);
  111.             return response($file, 200)->header('Content-Type', 'image/png');
  112.         }
  113.         return ApiResponseController::errorResponse("Invalid URL");
  114.     }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement