-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbookstore.sol
60 lines (48 loc) · 1.97 KB
/
bookstore.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
// SPDX-License-Identifier: GPL-3.0;
pragma solidity ^0.8.21;
contract BookStore {
address public owner;
uint256 public bookcount;
event BookAdded(uint256 indexed book_id, string title, string author ,uint256 copies);
event BookUddated(uint256 indexed book_idU, string titleU, string authorU ,uint256 copiesU);
struct Book {
string title;
string author;
uint256 copies;
}
//mapping book id to Book, whenever an id is input, it will bring the list of book.
//books will be the access point for inputting book
mapping(uint256 => Book) public books;
modifier onlyOwner() {
require(msg.sender == owner, "Only owner can add to books");
_;
}
constructor (){
bookcount = 0;
owner = msg.sender;
}
//we will save the title, author and copies to memory, since we are storing them for future use.
function addBook (string memory _title, string memory _author, uint256 _copies) public onlyOwner {
bookcount ++;
books[bookcount] = Book ({
title: _title,
author: _author,
copies: _copies
});
emit BookAdded(bookcount, _title, _author, _copies);
}
function updateBook (uint256 book_id, string memory _title, string memory _author, uint256 _copies) public onlyOwner {
book_id = book_id;
books[book_id] = Book ({
title: _title,
author: _author,
copies: _copies
});
emit BookUddated(book_id, _title, _author, _copies);
}
function getBook(uint256 book_id) public view returns (string memory, string memory, uint256) {
require(book_id >0 && book_id < bookcount, "Book not exist");
require(keccak256(abi.encodePacked(books[book_id].title)) != keccak256(abi.encodePacked("")), "Book not exist");
return(books[book_id].title, books[book_id].author, books[book_id].copies);
}
}