-
Notifications
You must be signed in to change notification settings - Fork 5
selling cards functionality #116
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The card should not be removed from the user until the Listing has found a buyer, at which point the card will be removed from the old user and added to the new user instantaneously. |
||
| $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); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,13 +27,15 @@ | |
|
|
||
| Route::get('/battles', 'BattlegroundController@getAllBattles'); | ||
|
|
||
| Route::put('/sell', 'MarketplaceController@sellCards'); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why does sell take an array of cardIDs and not just a single cardID parameter to sell (like all other function calls)?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. because in the frontend the user selects one or more cards to sell so the cards are passed to the backend together. I think battles does it the same way in the contract controller |
||
|
|
||
| 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'); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should remove console.log statements |
||
| 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'); | ||
| }); | ||
| }; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 ( | ||
| <div> | ||
| <div class="row"> | ||
| {cardIds.map(cardId => { | ||
| return ( | ||
| <div class="col-md-2"> | ||
| <CardValue cardId={cardId} /> | ||
| </div> | ||
| ); | ||
| })} | ||
| </div> | ||
| <br /> | ||
| <Button | ||
| disabled={!isValidSize} | ||
| onClick={() => this.props.sellCards(cardIds)} | ||
| > | ||
| Sell cards | ||
| </Button> | ||
| </div> | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| 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); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do not assume that the user passes a valid set of card IDs.
We need to verify that:
$listing->user_id should not be set to the $user->id