Last active
June 21, 2021 19:58
-
-
Save buddies2705/f34952ec9836c6df2490cc7200d20007 to your computer and use it in GitHub Desktop.
Solidity Crud Smart Contract
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.23; | |
contract CrudApp { | |
struct country{ | |
string name; | |
string leader; | |
uint256 population; | |
} | |
country[] public countries; | |
uint256 public totalCountries; | |
constructor() public { | |
totalCountries = 0; | |
} | |
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 totalCountries){ | |
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(a) == keccak256(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