From df91dcc22333bf0ea037418e270dd4b65a1747af Mon Sep 17 00:00:00 2001 From: larafa Date: Thu, 26 Apr 2018 01:33:58 -0400 Subject: [PATCH 1/2] selling cards functionality --- .../Controllers/MarketplaceController.php | 31 ++++++++++- backend/routes/api.php | 4 +- frontend/src/actions/cards.js | 1 + frontend/src/actions/listings.js | 18 ++++++ frontend/src/components/CardValue.js | 2 +- frontend/src/components/SellCards.js | 55 +++++++++++++++++++ frontend/src/pages/CardDetail.js | 48 ++++++++-------- frontend/src/pages/UserDetail.js | 12 +++- 8 files changed, 143 insertions(+), 28 deletions(-) create mode 100644 frontend/src/components/SellCards.js diff --git a/backend/app/Http/Controllers/MarketplaceController.php b/backend/app/Http/Controllers/MarketplaceController.php index 4b246a4..c24ce8c 100755 --- a/backend/app/Http/Controllers/MarketplaceController.php +++ b/backend/app/Http/Controllers/MarketplaceController.php @@ -16,7 +16,7 @@ class MarketplaceController extends Controller { public function __construct() { - $this->middleware('jwt.auth', ['except' => ['getAllCards', 'getCardDetail', 'getAllListings', 'estimateValue']]); + $this->middleware('jwt.auth', ['except' => ['getAllCards', 'getCardDetail', 'getAllListings', 'estimateValue', 'getValue']]); } /** @@ -82,6 +82,28 @@ public function putTransaction($card_id) return $this->getCardDetail($card_id); } + public function sellCards() + { + //add selling car to listing + $user = auth()->user(); + $data = json_decode(Request::getContent(), true); + foreach ($data as $cardId) { + $card = Card::find($cardId);; + + $listing = new Listing(); + $listing->card_id = $cardId; + $listing->user_id = $user->id; + $listing->price = $this->estimateValue($cardId); + $listing->save(); + + //remove selling card from user's cards + $card->user_id = NULL; + $card->save(); + } + + return response()->build(self::RESPONSE_MESSAGE_SUCCESS, $data); + } + public function getCardTransactions($card_id) { return response()->build(self::RESPONSE_MESSAGE_SUCCESS, Card::find($card_id)->transactions); @@ -104,6 +126,13 @@ public function estimateValue($card_id) $value = EthereumConverter::convertETHPriceToFloat(rand(250000000, 1250000000) * pow(10, 4)); } + return $value; + } + + public function getValue($card_id) + { + $value = $this->estimateValue($card_id); + return response()->build(self::RESPONSE_MESSAGE_SUCCESS, $value); } } diff --git a/backend/routes/api.php b/backend/routes/api.php index 7345868..0ef90d6 100755 --- a/backend/routes/api.php +++ b/backend/routes/api.php @@ -27,13 +27,15 @@ Route::get('/battles', 'BattlegroundController@getAllBattles'); +Route::put('/sell', 'MarketplaceController@sellCards'); + Route::get('/cards', 'MarketplaceController@getAllCards'); Route::get('/cards/{id}', 'MarketplaceController@getCardDetail'); Route::put('/cards/{id}', 'MarketplaceController@updateCard'); Route::get('/listings', 'MarketplaceController@getAllListings'); Route::put('/cards/{id}/transaction', 'MarketplaceController@putTransaction'); Route::get('/cards/{id}/transactions', 'MarketplaceController@getCardTransactions'); -Route::get('/card/{id}/value', 'MarketplaceController@estimateValue'); +Route::get('/card/{id}/value', 'MarketplaceController@getValue'); Route::get('/stats/counts', 'StatsController@getCounts'); Route::get('/stats/transactionReport', 'StatsController@getTransactionReport'); diff --git a/frontend/src/actions/cards.js b/frontend/src/actions/cards.js index 8bec69e..8a43158 100644 --- a/frontend/src/actions/cards.js +++ b/frontend/src/actions/cards.js @@ -121,6 +121,7 @@ export function toggleCardSelection(cardId) { cardId }; } + export const REQUEST_CARD_TRANSACTIONS = 'REQUEST_CARD_TRANSACTIONS'; export const RECEIVE_CARD_TRANSACTIONS = 'RECEIVE_CARD_TRANSACTIONS'; diff --git a/frontend/src/actions/listings.js b/frontend/src/actions/listings.js index db5538c..9a42e7c 100644 --- a/frontend/src/actions/listings.js +++ b/frontend/src/actions/listings.js @@ -1,4 +1,5 @@ import apiFetch from './index'; +import { toast } from 'react-toastify'; export const REQUEST_ALL_LISTINGS = 'REQUEST_ALL_LISTINGS'; export const RECEIVE_ALL_LISTINGS = 'RECEIVE_ALL_LISTINGS'; @@ -26,3 +27,20 @@ function receiveAllListings(listings) { listings }; } + +export function sellCards(cardIds) { + console.log('cardss'); + console.log(cardIds); + return dispatch => { + return apiFetch('sell', { + method: 'PUT', + body: JSON.stringify(cardIds) + }) + .then(response => response.json()) + .then(json => { + if (json.success) { + toast.success('Cards successfully added to listings!'); + } else toast.error('Failure adding cards to listing'); + }); + }; +} diff --git a/frontend/src/components/CardValue.js b/frontend/src/components/CardValue.js index fac09ea..c9b9009 100644 --- a/frontend/src/components/CardValue.js +++ b/frontend/src/components/CardValue.js @@ -12,7 +12,7 @@ class CardValue extends Component { render() { return ( - Recomended Price:{' '} + Card Id {this.props.cardId} Price:{' '} {!this.props.card.card_value_loading ? ( ) : null} diff --git a/frontend/src/components/SellCards.js b/frontend/src/components/SellCards.js new file mode 100644 index 0000000..83de5d6 --- /dev/null +++ b/frontend/src/components/SellCards.js @@ -0,0 +1,55 @@ +import React from 'react'; +import { connect } from 'react-redux'; +import { bindActionCreators } from 'redux'; +import { sellCards } from '../actions/listings'; +import PropTypes from 'prop-types'; +import { Button } from 'reactstrap'; +import CardValue from './CardValue'; +class SellCards extends React.Component { + render() { + let { cardIds } = this.props; + let isValidSize = cardIds.length >= 1; + return ( +
+
+ {cardIds.map(cardId => { + return ( +
+ +
+ ); + })} +
+
+ +
+ ); + } +} + +SellCards.propTypes = { + carddIds: PropTypes.array.isRequired +}; + +function mapStateToProps(state) { + let { card, user } = state; + return { + card, + user + }; +} + +const mapDispatchToProps = dispatch => { + return bindActionCreators( + { + sellCards + }, + dispatch + ); +}; +export default connect(mapStateToProps, mapDispatchToProps)(SellCards); diff --git a/frontend/src/pages/CardDetail.js b/frontend/src/pages/CardDetail.js index 3a2ea0b..57b7cc0 100644 --- a/frontend/src/pages/CardDetail.js +++ b/frontend/src/pages/CardDetail.js @@ -54,29 +54,31 @@ class CardDetail extends Component {
{doesCurrentUserOwnCard ? (
- You own this card! -
- Hide card from public lists and your profile? - { - this.props.editCardDetail( - this.state.cardId, - 'hidden', - e.target.checked - ); - }} - /> -
- +
+ You own this card! +
+ Hide card from public lists and your profile? + { + this.props.editCardDetail( + this.state.cardId, + 'hidden', + e.target.checked + ); + }} + /> +
+ +
) : (
diff --git a/frontend/src/pages/UserDetail.js b/frontend/src/pages/UserDetail.js index 6f3193b..e5d108f 100644 --- a/frontend/src/pages/UserDetail.js +++ b/frontend/src/pages/UserDetail.js @@ -14,6 +14,7 @@ import CardFilterSort, { FILTER_SORT_PRESET_FULL } from '../components/CardFilterSort'; import BattleGroupCreator from '../components/BattleGroupCreator'; +import SellCards from '../components/SellCards'; import UserDetailFollowing from '../components/UserDetailFollowing'; import CopyToClipboardButton from '../components/CopyToClipboardButton'; class UserDetail extends Component { @@ -71,8 +72,15 @@ class UserDetail extends Component {
) : (
-

make a battle group!

- +
+

make a battle group!

+ +
+
+
+

Sell your cards in the marketPlace!

+ +
)}
From ebb3a81ccb7d7a864392bf62e2e73adbfbde4e7f Mon Sep 17 00:00:00 2001 From: larafa Date: Thu, 26 Apr 2018 14:30:09 -0400 Subject: [PATCH 2/2] fixed styling errors --- backend/app/Http/Controllers/MarketplaceController.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/backend/app/Http/Controllers/MarketplaceController.php b/backend/app/Http/Controllers/MarketplaceController.php index c24ce8c..bec7b18 100755 --- a/backend/app/Http/Controllers/MarketplaceController.php +++ b/backend/app/Http/Controllers/MarketplaceController.php @@ -88,7 +88,7 @@ public function sellCards() $user = auth()->user(); $data = json_decode(Request::getContent(), true); foreach ($data as $cardId) { - $card = Card::find($cardId);; + $card = Card::find($cardId); $listing = new Listing(); $listing->card_id = $cardId; @@ -97,7 +97,7 @@ public function sellCards() $listing->save(); //remove selling card from user's cards - $card->user_id = NULL; + $card->user_id = null; $card->save(); }