-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLibrary.sol
More file actions
98 lines (76 loc) · 3.6 KB
/
Library.sol
File metadata and controls
98 lines (76 loc) · 3.6 KB
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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
import "./Ownable.sol";
contract Library is Ownable{
event NewBook(uint id, string name, uint copies);
event CopiesAdded(uint id, string name, uint copies);
event BookBorrowed(uint id, uint remainingCopies);
event BookReturned(uint id, uint remainingCopies);
struct Book{
string name;
uint256 copies;
}
//Mapping that helps to manage how many copies of each book are out.
mapping(uint => uint) public bookIdToCopiesOut;
// Mapping that saves a boolean if a book id is now borrowed by an address
mapping(address => mapping(uint => bool)) public addressBookIdCopiesBorrowed;
// This keeps the order in which the books have been borrowed
mapping(uint256 => address[]) public borrowers;
// This is where the book info is stored. THE INDEX OF THE BOOK IN THE ARRAY IS TAKEN AS THE BOOK ID.
Book[] public bookArray;
// Owner Functions
// Adds a new book to the library
function addNewBook(string memory _name, uint256 _copies) public onlyOwner{
Book memory _book = Book(_name, _copies);
bookArray.push(_book);
//bookIdToCopiesOut[bookArray.length-1]=0;
emit NewBook(bookArray.length-1, _name, _copies);
}
// Adds more copies to a book in library by id
function addCopies(uint256 _id, uint256 _copies) public onlyOwner{
bookArray[_id].copies = bookArray[_id].copies + _copies;
emit CopiesAdded(_id, bookArray[_id].name, bookArray[_id].copies);
}
// View functions
//Returns the name of a book by id
function getName(uint256 _id) public view returns(string memory){
require(bookArray.length-1 >= _id, "No book with this index");
return bookArray[_id].name;
}
// Returns boolean if book is available for borrowing
function isAvailable(uint256 _id) public view returns(bool){
require(bookArray.length-1 >= _id, "No book with this index");
return bookArray[_id].copies - bookIdToCopiesOut[_id] > 0;
}
// returns the amount of available copies to borrow
function availableUnits(uint256 _id) public view returns(uint256){
require(bookArray.length-1 >= _id, "No book with this index");
return bookArray[_id].copies - bookIdToCopiesOut[_id];
}
// returns array of book borrowers in chronological order
function viewBookBorrowers(uint256 _id) public view returns(address [] memory){
require(bookArray.length-1 >= _id, "No book with this index");
return borrowers[_id];
}
// Book handleing functions
// Lets user borrow a book
function borrowBook(uint256 _id) public{
require(bookArray.length-1 >= _id, "No book with this index");
require(isAvailable(_id), "All copies are out");
require(!addressBookIdCopiesBorrowed[msg.sender][_id], "You already have one of those");
//borrow book
borrowers[_id].push(msg.sender);
bookIdToCopiesOut[_id] = bookIdToCopiesOut[_id] + 1;
addressBookIdCopiesBorrowed[msg.sender][_id] = true;
emit BookBorrowed(_id, bookArray[_id].copies - bookIdToCopiesOut[_id]);
}
//Lets user return a book
function returnBook(uint256 _id) public{
require(bookArray.length-1 >= _id, "No book with this index");
require(addressBookIdCopiesBorrowed[msg.sender][_id], "You dont have this book");
//return book
bookIdToCopiesOut[_id] = bookIdToCopiesOut[_id] - 1;
addressBookIdCopiesBorrowed[msg.sender][_id] = false;
emit BookBorrowed(_id, bookArray[_id].copies - bookIdToCopiesOut[_id]);
}
}