Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\Http\Controllers;
- use App\User;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Hash;
- use Illuminate\Support\Facades\Validator;
- use Illuminate\Support\Facades\Storage;
- use Illuminate\Support\Facades\File;
- class UserController extends Controller
- {
- public function getProfile(Request $request){
- $validator=Validator::make($request->all(),[
- 'id'=>'required|integer',
- ]);
- if($validator->fails()){
- return ApiResponseController::errorResponse($validator->errors()->first());
- }
- $id=$request->id;
- $profile= User::find($id);
- if($profile){
- if($profile->location){
- $profile->location=json_decode($profile->location,true);
- }
- return ApiResponseController::successfulResponse($profile);
- }
- return ApiResponseController::errorResponse('User Does Not Exist');
- }
- public function updateProfile(Request $request){
- $validator=Validator::make($request->all(),[
- 'id'=>'required|integer',
- 'name'=>'string',
- 'email'=>'email',
- 'new_password'=>'string|min:6',
- 'password'=>'required_with:new_password',
- 'phone'=>'string',
- 'address'=>'string|required_with:latitude,longitude',
- 'latitude'=>'string|required_with:longitude,address',
- 'longitude'=>'string|required_with:latitude,address',
- 'profile_picture'=>'image',
- 'cover_picture'=>'image',
- // 'profile_picture'=>'image|mimes:png',
- // 'cover_picture'=>'image|mimes:png',
- ]);
- if($validator->fails()){
- return ApiResponseController::errorResponse($validator->errors()->first());
- }
- $id=$request->id;
- $user=User::find($id);
- if($user) {
- $attributes = [];
- if ($request->new_password) {
- if (Hash::check($request->password, $user->password)) {
- $attributes['password'] = bcrypt($request->new_password);
- } else {
- return ApiResponseController::errorResponse("Wrong Password");
- }
- }
- if ($request->name) {
- $attributes['name'] = $request->name;
- }
- if ($request->email) {
- $attributes['email'] = $request->email;
- }
- if ($request->phone) {
- $attributes['phone'] = $request->phone;
- }
- if ($request->address) {
- $location = [
- 'address'=>$request->address,
- 'latitude'=>$request->latitude,
- 'longitude'=>$request->longitude,
- ];
- $attributes['location']=\GuzzleHttp\json_encode($location);
- }
- if ($request->profile_picture) {
- $profile_picture = $request->file('profile_picture');
- $name = $id . '_' . time() . '.' . $profile_picture->getClientOriginalExtension();
- Storage::disk('local')->put('user-profile-pictures/' . $name, File::get($profile_picture));
- $attributes['profile_picture_url'] = env('APP_URL') . '/api/v1/profile/profile-picture/' . $name;
- }
- if ($request->cover_picture) {
- $cover_picture = $request->file('cover_picture');
- $name = $id . '_' . time() . '.' . $cover_picture->getClientOriginalExtension();
- Storage::disk('local')->put('user-cover-pictures/' . $name, File::get($cover_picture));
- $attributes['cover_picture_url'] = env('APP_URL') . '/api/v1/profile/cover-picture/' . $name;
- }
- User::where('id', $id)->update($attributes);
- $user = User::find($id);
- if($user->location){
- $user->location=json_decode($user->location,true);
- }
- return ApiResponseController::successfulResponse($user);
- }
- return ApiResponseController::errorResponse("User does not exist");
- }
- public function getProfilePicture($name){
- $path='user-profile-pictures/' . $name;
- if(Storage::disk('local')->exists($path)) {
- $file= Storage::disk('local')->get($path);
- return response($file, 200)->header('Content-Type', 'image/png');
- }
- return ApiResponseController::errorResponse("Invalid URL");
- }
- public function getCoverPicture($name){
- $path='user-cover-pictures/' . $name;
- if(Storage::disk('local')->exists($path)) {
- $file= Storage::disk('local')->get($path);
- return response($file, 200)->header('Content-Type', 'image/png');
- }
- return ApiResponseController::errorResponse("Invalid URL");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement