-
Notifications
You must be signed in to change notification settings - Fork 0
/
realstate.sol
231 lines (196 loc) · 8.09 KB
/
realstate.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
// SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.9;
contract RealEstate {
//state variable
struct Property {
uint256 productID;
address owner;
uint256 price;
string propertyTitle;
string category;
string images;
string propertyAddress;
string description;
address[] reviewers;
string[] reviews;
}
//mapping
mapping(uint256 => Property) private properties;
uint256 public propertyIndex;
//events
event PropertyListed(uint256 indexed id, address indexed owner, uint256 price);
event PropertySold(uint256 indexed id, address indexed oldOwner, address indexed newOwner, uint256 price);
event PropertyResold(uint256 indexed id, address indexed oldOwner, address indexed newOwner, uint256 price);
//review
struct Review {
address reviewer;
uint256 productId;
uint256 rating;
string comment;
uint256 likes;
}
struct Product {
uint256 productId;
uint256 totalRating;
uint256 numReviews;
}
mapping(uint256 => Review[]) private reviews;
mapping(address => uint256[]) private userReviews;
mapping(uint256 => Product) private products;
uint256 public reviewsCounter;
event ReviewAdded(uint256 indexed productId, address indexed reviewer, uint256 rating, string comment);
event ReviewLiked(uint256 indexed productId, uint256 indexed reviewIndex, address indexed liker, uint256 likes);
//function in contract
function listProperty(
address owner,
uint256 price,
string memory _propertyTitle,
string memory _category,
string memory _images,
string memory _propertyAddress,
string memory _description
) external returns (uint256) {
require(price > 0, "price must be greater than 0.");
uint256 productId = propertyIndex++;
Property storage property = properties[productId];
property.owner = owner;
property.price = price;
property.propertyTitle = _propertyTitle;
property.category = _category;
property.images = _images;
property.propertyAddress = _propertyAddress;
property.description = _description;
emit PropertyListed(productId, owner, price);
return productId;
}
function updateProperty(
address owner,
uint256 productId,
string memory _propertyTitle,
string memory _category,
string memory _images,
string memory _propertyAddress,
string memory _description
) external returns (uint256) {
Property storage property = properties[productId];
require(property.owner == owner, "You are not the owner");
property.propertyTitle = _propertyTitle;
property.category = _category;
property.images = _images;
property.propertyAddress = _propertyAddress;
property.description = _description;
return productId;
}
function updatePrice(address owner, uint256 productId, uint256 price) external returns (string memory) {
Property storage property = properties[productId];
require(property.owner == owner, "You are not the owner");
property.price = price;
return "Your Property Price Is Updated";
}
function buyProperty(uint256 id, address buyer) external payable {
uint256 amount = msg.value;
require(amount >= properties[id].price, "Insufficient funds");
Property storage property = properties[id];
(bool sent, ) = payable(property.owner).call{value: amount}("");
if (sent) {
property.owner = buyer;
emit PropertySold(id, property.owner, buyer, amount);
}
}
function getAllProperties() public view returns (Property[] memory) {
uint256 itemCount = propertyIndex;
uint256 currentIndex = 0;
Property[] memory items = new Property[](itemCount);
for (uint256 i = 0; i < itemCount; i++) {
uint256 currentId = i;
Property storage currentItems = properties[currentId];
items[currentIndex] = currentItems;
currentIndex += 1;
}
return items;
}
function getProperty(uint256 id) external view returns (uint256, address, uint256, string memory, string memory, string memory, string memory, string memory) {
Property memory property = properties[id];
return (
property.productID,
property.owner,
property.price,
property.propertyTitle,
property.category,
property.images,
property.propertyAddress,
property.description
);
}
function getUserProperties(address user) external view returns (Property[] memory) {
uint256 totalItemCount = propertyIndex;
uint256 itemCount = 0;
uint256 currentIndex = 0;
for (uint256 i = 0; i < totalItemCount; i++) {
if (properties[i].owner == user) {
itemCount += 1;
}
}
Property[] memory items = new Property[](itemCount);
for (uint256 i = 0; i < totalItemCount; i++) {
if (properties[i].owner == user) {
uint256 currentId = i;
Property storage currentItem = properties[currentId];
items[currentIndex] = currentItem;
currentIndex += 1;
}
}
return items;
}
//review function
function addReview(uint256 productId, uint256 rating, string calldata comment, address user) external {
require(rating >= 1 && rating <= 5, "Rating must be between 1 and 5");
Property storage property = properties[productId];
property.reviewers.push(user);
property.reviews.push(comment);
//review section
reviews[productId].push(Review(user, productId, rating, comment, 0));
userReviews[user].push(productId);
products[productId].totalRating += rating;
products[productId].numReviews++;
emit ReviewAdded(productId, user, rating, comment);
reviewsCounter++;
}
function getProductReviews(uint256 productId) external view returns (Review[] memory) {
return reviews[productId];
}
function getUserReviews(address user) external view returns (Review[] memory) {
uint256 totalReviews = userReviews[user].length;
Review[] memory userProductReviews = new Review[](totalReviews);
for (uint256 i = 0; i < userReviews[user].length; i++) {
uint256 productId = userReviews[user][i];
Review[] memory productReviews = reviews[productId];
for (uint256 j = 0; j < productReviews.length; j++) {
if (productReviews[j].reviewer == user) {
userProductReviews[i] = productReviews[j];
}
}
}
return userProductReviews;
}
function likeReview(uint256 productId, uint256 reviewIndex, address user) external { // Changed 'likeREview' to 'likeReview'
Review storage review = reviews[productId][reviewIndex];
review.likes++;
emit ReviewLiked(productId, reviewIndex, user, review.likes);
}
function getHighestRatedProduct() external view returns (uint256) {
uint256 highestRating = 0;
uint256 highestRatedProductId = 0;
for (uint256 i = 0; i < propertyIndex; i++) { // Changed from 'reviewsCounter' to 'propertyIndex'
uint256 productId = i;
if (products[productId].numReviews > 0) {
uint256 avgRating = products[productId].totalRating / products[productId].numReviews;
if (avgRating > highestRating) {
highestRating = avgRating;
highestRatedProductId = productId;
}
}
}
return highestRatedProductId;
}
}