-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLibrary.sol
68 lines (54 loc) · 2.39 KB
/
Library.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
pragma solidity ^0.8.0;
import "./Ownable.sol";
contract BookLibrary is Ownable {
string[] public availableBooks;
address[] public borrowers;
mapping(address => bool) uniqueBorrowers;
//0 means book has never been added to the library, 1 means that all books are currently borrowed
mapping(string => uint) private availableCopies;
mapping(string => bool) private borrowerToBook;
modifier isAvailable(string memory _title) {
require(availableCopies[_title] > 1, "No copy currently available!");
_;
}
modifier alreadyBorrowed(address _borrower, string memory _title) {
require(borrowerToBook[_generateKey(borrower, _title)], "Book not borrowed!");
_;
}
modifier notBorrowed(address _borrower, string memory _title) {
require(!borrowerToBook[_generateKey(_borrower, _title)], "Book already borrowed");
_;
}
function getAvailableCopies(string memory _title) external view returns (uint) {
return availableCopies[_title] == 0 ? 0 : availableCopies[_title]-1;
}
function getBorrowerToBook(string memory _title) external view returns (bool) {
return borrowerToBook[_generateKey(msg.sender, _title)];
}
function _generateKey(address _address, string memory _title) private pure returns (string memory) {
return string(abi.encodePacked(_address, _title));
}
function addBooks(string memory _title, uint _copies) external onlyOwner {
// require(_copies > 0 && _title != "");
if (availableCopies[_title] < 1) {
availableBooks.push(_title);
availableCopies[_title]++;
}
availableCopies[_title] += _copies;
}
function borrowBook(string memory _title) external isAvailable(_title) notBorrowed(msg.sender, _title) {
availableCopies[_title]--;
borrowerToBook[_generateKey(msg.sender, _title)] = true;
addUniqueBorrower(msg.sender);
}
function returnBook(string memory _title) external alreadyBorrowed(msg.sender, _title) {
availableCopies[_title]++;
borrowerToBook[_generateKey(msg.sender, _title)] = false;
}
function addUniqueBorrower(address _address) private {
if (uniqueBorrowers[_address] == false) {
borrowers.push(_address);
uniqueBorrowers[_address] = true;
}
}
}