-
Notifications
You must be signed in to change notification settings - Fork 2
/
ListingFacet.sol
102 lines (84 loc) · 3.05 KB
/
ListingFacet.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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;
import "@contracts/interfaces/IListing.sol";
import "@contracts/libraries/DataTypes.sol";
import "@contracts/libraries/Modifiers.sol";
contract ListingFacet is Modifiers, IListingFacet {
using DeRentNFT for AppStorage;
function _getListingPropertyById(uint256 id) internal view returns (DataTypes.ListingProperty memory) {
string memory uri = s.tokenURI(id);
address owner = s.ownerOf(id);
DataTypes.Property memory property = s.properties[id];
DataTypes.Rental memory rental = s.rentals[id];
return DataTypes.ListingProperty({
id: id,
rentPrice: property.rentPrice,
owner: owner,
published: property.published,
uri: uri,
status: rental.status
});
}
function _filterPropertiesBy(function(DataTypes.ListingProperty memory) view returns(bool) filterFunc)
internal
view
returns (DataTypes.ListingProperty[] memory)
{
DataTypes.ListingProperty[] memory properties = getProperties();
uint256 totalCount = 0;
for (uint256 i = 0; i < properties.length; i++) {
if (filterFunc(properties[i])) {
totalCount++;
}
}
DataTypes.ListingProperty[] memory ret = new DataTypes.ListingProperty[](totalCount);
uint256 id = 0;
for (uint256 i = 0; i < properties.length; i++) {
if (filterFunc(properties[i])) {
ret[id] = properties[i];
id++;
}
}
return ret;
}
function _filterByPublished(DataTypes.ListingProperty memory property) internal pure returns (bool) {
return property.published;
}
function _filterByOwner(DataTypes.ListingProperty memory property) internal view returns (bool) {
return property.owner == msg.sender;
}
/**
* @dev see {IListingFacet-getSelfProperties}.
*/
function getSelfProperties() external view returns (DataTypes.ListingProperty[] memory) {
return _filterPropertiesBy(_filterByOwner);
}
/**
* @dev see {IListingFacet-getPublishedProperties}.
*/
function getPublishedProperties() external view returns (DataTypes.ListingProperty[] memory) {
return _filterPropertiesBy(_filterByPublished);
}
/**
* @dev see {IListingFacet-getProperties}.
*/
function getProperties() public view returns (DataTypes.ListingProperty[] memory) {
uint256 totalCount = s.getCount();
DataTypes.ListingProperty[] memory res = new DataTypes.ListingProperty[](totalCount);
for (uint256 id = 0; id < totalCount; id++) {
res[id] = _getListingPropertyById(id);
}
return res;
}
/**
* @dev see {IListing-getPropertyById}.
*/
function getPropertyById(uint256 property)
external
view
requirePropertyExists(property)
returns (DataTypes.ListingProperty memory)
{
return _getListingPropertyById(property);
}
}