Skip to content

Instantly share code, notes, and snippets.

@mobigaurav
Created August 16, 2021 03:47
Show Gist options
  • Save mobigaurav/e28fee17a5faf15370407ca31869f39a to your computer and use it in GitHub Desktop.
Save mobigaurav/e28fee17a5faf15370407ca31869f39a to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.8.7+commit.e28d00a7.js&optimize=false&runs=200&gist=
pragma solidity ^0.8.4;
contract bookLibrary {
address public contractOwner;
constructor() public {
contractOwner = msg.sender;
}
modifier onlyOwner() {
require(msg.sender == contractOwner);
_;
}
struct Book {
uint256 id;
string name;
string author;
uint256 price;
bool exists;
}
mapping(uint256 => Book) public books;
function addBook(
uint256 _bookId,
string memory _bookName,
string memory _bookAuthor,
uint256 _price
) public onlyOwner {
require(_price > 0, "Book price can not be zero");
books[_bookId].name = _bookName;
books[_bookId].author = _bookAuthor;
books[_bookId].price = _price;
books[_bookId].exists = true;
}
function queryBookById(uint256 _bookId)
public view returns (
string memory name,
string memory author,
uint256 price
)
{
require(books[_bookId].exists, "Book must be available to query");
return (
books[_bookId].name,
books[_bookId].author,
books[_bookId].price
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment