Created
January 8, 2019 20:22
-
-
Save buddies2705/11c6e2b64840e6a37039fc8a530dcb16 to your computer and use it in GitHub Desktop.
Upgradable Crud using zeppelin OS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.4.24; | |
import "zos-lib/contracts/Initializable.sol"; | |
contract CrudApp is Initializable { | |
struct country{ | |
string name; | |
string leader; | |
uint256 population; | |
} | |
country[] public countries; | |
uint256 public totalCountries; | |
uint256 public limit; | |
function initialize(uint256 countryLimit) initializer public { | |
totalCountries = 0; | |
limit = countryLimit; | |
} | |
event CountryEvent(string countryName , string leader, uint256 population); | |
event LeaderUpdated(string countryName , string leader); | |
event CountryDelete(string countryName); | |
function insert( string countryName , string leader , uint256 population) public returns (uint256){ | |
require(totalCountries < limit); | |
country memory newCountry = country(countryName , leader, population); | |
countries.push(newCountry); | |
totalCountries++; | |
//emit event | |
emit CountryEvent (countryName, leader, population); | |
return totalCountries; | |
} | |
function updateLeader(string countryName, string newLeader) public returns (bool success){ | |
//This has a problem we need loop | |
for(uint256 i =0; i< totalCountries; i++){ | |
if(compareStrings(countries[i].name ,countryName)){ | |
countries[i].leader = newLeader; | |
emit LeaderUpdated(countryName, newLeader); | |
return true; | |
} | |
} | |
return false; | |
} | |
function deleteCountry(string countryName) public returns(bool success){ | |
require(totalCountries > 0); | |
for(uint256 i =0; i< totalCountries; i++){ | |
if(compareStrings(countries[i].name , countryName)){ | |
countries[i] = countries[totalCountries-1]; // pushing last into current arrray index which we gonna delete | |
delete countries[totalCountries-1]; // now deleteing last index | |
totalCountries--; //total count decrease | |
countries.length--; // array length decrease | |
//emit event | |
emit CountryDelete(countryName); | |
return true; | |
} | |
} | |
return false; | |
} | |
function getCountry(string countryName) public view returns(string name , string leader , uint256 population){ | |
for(uint256 i =0; i< totalCountries; i++){ | |
if(compareStrings(countries[i].name, countryName)){ | |
//emit event | |
return (countries[i].name , countries[i].leader , countries[i].population); | |
} | |
} | |
revert('country not found'); | |
} | |
function compareStrings (string a, string b) internal pure returns (bool){ | |
return keccak256(abi.encodePacked(a)) == keccak256(abi.encodePacked(b)); | |
} | |
function getTotalCountries() public view returns (uint256 length){ | |
return countries.length; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment