diff --git a/product_reviews_contract.sol b/product_reviews_contract.sol new file mode 100644 index 0000000..52893d3 --- /dev/null +++ b/product_reviews_contract.sol @@ -0,0 +1,42 @@ +// SPDX-License-Identifier: GPL-3.0-or-later +pragma solidity ^0.8.0; + +contract productReviews { + struct Review { + uint reviewId; + uint productId; + string description; + uint rating; + address reviewer; + } + + uint public reviewId = 1; + Review[] public reviews; + + function addReview(uint _productId, string memory _description, uint _rating) external { + if(_isRatingOk(_rating)) { + reviews.push(Review(reviewId, _productId, _description, _rating, msg.sender)); + } + reviewId++; + revert('Rating invalid'); + } + + function _isRatingOk(uint _rating) internal pure returns (bool) { // azt allitja, hogy folosleges a returns deklaralasa, ha nincs elnevezve, viszont ha kitorlom, panaszkodik a return parameterek szama miatt... + if(_rating < 6 && _rating > 0) { + return true; + } + } + + function isOwner(uint _reviewId) public view returns (bool) { + if(msg.sender == reviews[_reviewId].reviewer) { + return true; + } + } + + function _deleteReview(uint _reviewId) private { + if(isOwner(_reviewId) == true) { + delete reviews[_reviewId]; + } + revert('User is not review owner'); + } +} \ No newline at end of file