Skip to content
This repository was archived by the owner on Nov 26, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion backend/app/Http/Controllers/MarketplaceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']]);
}

/**
Expand Down Expand Up @@ -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) {
Copy link
Member

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:

  • The card exists
  • The card is available for auction (not already on auction, not in battle, etc)
  • The card is owned by the user trying to sell the card (thus the $listing->user_id should not be set to the $user->id

$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
Copy link
Member

Choose a reason for hiding this comment

The 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);
Expand All @@ -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);
}
}
4 changes: 3 additions & 1 deletion backend/routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@

Route::get('/battles', 'BattlegroundController@getAllBattles');

Route::put('/sell', 'MarketplaceController@sellCards');
Copy link
Member

Choose a reason for hiding this comment

The 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)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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');
Expand Down
1 change: 1 addition & 0 deletions frontend/src/actions/cards.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
18 changes: 18 additions & 0 deletions frontend/src/actions/listings.js
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';
Expand Down Expand Up @@ -26,3 +27,20 @@ function receiveAllListings(listings) {
listings
};
}

export function sellCards(cardIds) {
console.log('cardss');
console.log(cardIds);
Copy link
Contributor

Choose a reason for hiding this comment

The 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');
});
};
}
2 changes: 1 addition & 1 deletion frontend/src/components/CardValue.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class CardValue extends Component {
render() {
return (
<span>
Recomended Price:{' '}
Card Id {this.props.cardId} Price:{' '}
{!this.props.card.card_value_loading ? (
<EtherPrice price={this.props.card.card_value} />
) : null}
Expand Down
55 changes: 55 additions & 0 deletions frontend/src/components/SellCards.js
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);
48 changes: 25 additions & 23 deletions frontend/src/pages/CardDetail.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,29 +54,31 @@ class CardDetail extends Component {
<div>
{doesCurrentUserOwnCard ? (
<div>
You own this card!
<br />
Hide card from public lists and your profile?
<input
name="isHidden"
type="checkbox"
checked={cardDetail.hidden}
onChange={e => {
this.props.editCardDetail(
this.state.cardId,
'hidden',
e.target.checked
);
}}
/>
<br />
<Button
onClick={() => {
this.props.saveCardDetail(this.state.cardId);
}}
>
Save Card Preferences
</Button>
<div>
You own this card!
<br />
Hide card from public lists and your profile?
<input
name="isHidden"
type="checkbox"
checked={cardDetail.hidden}
onChange={e => {
this.props.editCardDetail(
this.state.cardId,
'hidden',
e.target.checked
);
}}
/>
<br />
<Button
onClick={() => {
this.props.saveCardDetail(this.state.cardId);
}}
>
Save Card Preferences
</Button>
</div>
</div>
) : (
<div>
Expand Down
12 changes: 10 additions & 2 deletions frontend/src/pages/UserDetail.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -71,8 +72,15 @@ class UserDetail extends Component {
</div>
) : (
<div>
<h4>make a battle group!</h4>
<BattleGroupCreator cardIds={card.selectedCardIDs} />
<div>
<h4>make a battle group!</h4>
<BattleGroupCreator cardIds={card.selectedCardIDs} />
</div>
<br />
<div>
<h4>Sell your cards in the marketPlace!</h4>
<SellCards cardIds={card.selectedCardIDs} />
</div>
</div>
)}
<hr />
Expand Down