Skip to content

Commit

Permalink
add update profile
Browse files Browse the repository at this point in the history
  • Loading branch information
Eries Trisnadi committed Jan 20, 2018
1 parent 4de5727 commit 9b13094
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
48 changes: 48 additions & 0 deletions app/Http/Controllers/Api/V1/UserController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace App\Http\Controllers\Api\V1;

use Illuminate\Http\Request;
use Illuminate\Http\Response;
use App\Http\Controllers\Controller;
use App\User;
use App\Company;
use App\CompanyType;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Auth;

class UserController extends Controller
{
public function __construct(){
$this->content = array();
}

public function updateProfile(Request $request)
{
$auth = Auth::user();
$user = User::find($auth->id);

if($user->update([
'name' => $request->input('name') && !empty($request->input('name'))
? $request->input('name')
: $auth->name,
'email' => $request->input('email') && !empty($request->input('email'))
? $request->input('email')
: $auth->email,
'password' => $request->input('password') && !empty($request->input('password'))
? !Hash::check($request->input('old_password'), $user->password)
? $user->password
: bcrypt($request->input('password'))
: $user->password,
])) {
$this->content['data'] = User::with('company')->find($auth->id);
$this->content['status'] = 200;
return response()->json($this->content, $this->content['status'], [], JSON_NUMERIC_CHECK);
}

$this->content['error'] = "Server Error";
$this->content['status'] = 500;

return response()->json($this->content, $this->content['status'], [], JSON_NUMERIC_CHECK);
}
}
11 changes: 11 additions & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,17 @@
});


// Users API
Route::group(['prefix' => 'users', 'namespace' => '\Api\V1'], function()
{
Route::put('/profile', [
'as' => 'api.users.profile.update',
'middleware' => 'auth:api',
'uses' => 'UserController@updateProfile',
]);
});


// Customers API
Route::group(['prefix' => 'customers', 'namespace' => '\Api\V1'], function()
{
Expand Down

0 comments on commit 9b13094

Please sign in to comment.