From f89638af792cd23f105a5b2909e39caba4e20562 Mon Sep 17 00:00:00 2001 From: larafa Date: Sun, 25 Mar 2018 23:17:25 -0400 Subject: [PATCH 1/6] User's follower and followings list --- .../Http/Controllers/ProfileController.php | 24 ++++++++ backend/app/Models/User.php | 4 +- ...02_22_023241_create_transactions_table.php | 36 ++++++++++++ backend/routes/api.php | 5 +- frontend/src/App.js | 4 ++ frontend/src/actions/users.js | 52 +++++++++++++++++ frontend/src/components/FollowersList.js | 57 +++++++++++++++++++ frontend/src/components/FollowingsList.js | 57 +++++++++++++++++++ frontend/src/pages/Account.js | 38 ++++++++++++- frontend/src/reducers/reducer_user.js | 36 +++++++++++- 10 files changed, 305 insertions(+), 8 deletions(-) create mode 100644 backend/database/migrations/2018_02_22_023241_create_transactions_table.php create mode 100644 frontend/src/components/FollowersList.js create mode 100644 frontend/src/components/FollowingsList.js diff --git a/backend/app/Http/Controllers/ProfileController.php b/backend/app/Http/Controllers/ProfileController.php index 7cc155d..6596957 100755 --- a/backend/app/Http/Controllers/ProfileController.php +++ b/backend/app/Http/Controllers/ProfileController.php @@ -98,4 +98,28 @@ public function follow($user_id) return response()->build(self::RESPONSE_MESSAGE_ALREADY_FOLLOWING); } } + + /** + * authorized user follows user_id. + * Get all followers for the user + * + * @return const RESPONSE_MESSAGE_SUCCESS or RESPONSE_MESSAGE_ALREADY_FOLLOWING + */ + public function getFollowers() + { + $user_id = auth()->user()->id; + return response()->build(self::RESPONSE_MESSAGE_SUCCESS, User::with("followers")->find($user_id)->get()); + } + + /** + * authorized user follows user_id. + * Get all followings of the user + * + * @return const RESPONSE_MESSAGE_SUCCESS or RESPONSE_MESSAGE_ALREADY_FOLLOWING + */ + public function getFollowings() + { + $user_id = auth()->user()->id; + return response()->build(self::RESPONSE_MESSAGE_SUCCESS, Follow::with("followings")->where('follower_id', $user_id)->get()); + } } diff --git a/backend/app/Models/User.php b/backend/app/Models/User.php index 9e535d1..fbf64b6 100755 --- a/backend/app/Models/User.php +++ b/backend/app/Models/User.php @@ -40,10 +40,10 @@ public function getToken() public function followers() { - return $this->belongsToMany('App\Models\User', 'follows', 'user_id', 'follower_id')->withTimestamps(); + return $this->hasMany('App\Models\Follow','id', 'follower_id'); } - public function following() + public function followings() { return $this->belongsToMany('App\Models\User', 'follows', 'follower_id', 'user_id')->withTimestamps(); } diff --git a/backend/database/migrations/2018_02_22_023241_create_transactions_table.php b/backend/database/migrations/2018_02_22_023241_create_transactions_table.php new file mode 100644 index 0000000..59e18e6 --- /dev/null +++ b/backend/database/migrations/2018_02_22_023241_create_transactions_table.php @@ -0,0 +1,36 @@ +increments('id'); + $table->integer('card_id')->unsigned(); + $table->foreign('card_id')->references('id')->on('cards'); + $table->integer('user_id')->unsigned(); + $table->foreign('user_id')->references('id')->on('users'); + $table->bigInteger('price')->unsigned(); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('transactions'); + } +} diff --git a/backend/routes/api.php b/backend/routes/api.php index 63b767b..7f04d00 100755 --- a/backend/routes/api.php +++ b/backend/routes/api.php @@ -21,6 +21,9 @@ Route::put('/follow/{id}', 'ProfileController@follow'); Route::get('/users/{id}', 'ProfileController@getUserDetail'); +Route::get('me/followers', 'ProfileController@getFollowers'); +Route::get('me/followings', 'ProfileController@getFollowings'); +Route::get('/test', function () { return Route::list(); }); Route::get('/cards', 'MarketplaceController@getAllCards'); Route::get('/cards/{id}', 'MarketplaceController@getCardDetail'); @@ -28,5 +31,5 @@ Route::get('/listings', 'MarketplaceController@getAllListings'); Route::get('/', function () { - return Response::build(\App\Http\Controllers\Controller::RESPONSE_MESSAGE_SUCCESS, 'hi'); + return Response::build(\App\Http\Controllers\Controller::RESPONSE_MESSAGE_SUCCESS, 'hi'); }); diff --git a/frontend/src/App.js b/frontend/src/App.js index 55f6945..a79710d 100644 --- a/frontend/src/App.js +++ b/frontend/src/App.js @@ -13,6 +13,8 @@ import AllCards from './pages/AllCards'; import LoginPage from './pages/Login'; import MarketplacePage from './pages/Marketplace'; import AccountPage from './pages/Account'; +import FollowersPage from './components/FollowersList'; +import FollowingsPage from './components/FollowingsList'; import Nav from './components/Nav'; import Web3Initialization from './components/Web3Initialization'; import { connect } from 'react-redux'; @@ -79,6 +81,8 @@ const App = () => ( {/*Routes that only logged in Users can access*/} + + {/*