Skip to content

Instantly share code, notes, and snippets.

@prithwis
Last active December 4, 2021 04:01
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prithwis/ad3f6e7720614f69af03c36035f965d0 to your computer and use it in GitHub Desktop.
Save prithwis/ad3f6e7720614f69af03c36035f965d0 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.4.25+commit.59dbf8f1.js&optimize=false&runs=200&gist=
pragma solidity ^0.4.6;
// https://medium.com/robhitchens/solidity-crud-part-1-824ffa69509a
// https://bitbucket.org/rhitchens2/soliditycrud/src/master/contracts/SolidityCRUD-part1.sol
//
// the python client for this contract is available at
// https://colab.research.google.com/drive/1JQAze2ZxRF1_AsGbdyQWr82XsFWPP3gk?usp=sharing
//
contract pmCrudCon {
struct dataRecord {
string empName;
uint empSalary;
uint empIdIndex;
}
mapping(uint => dataRecord) private dataBase9;
uint[] private db9Index;
event LogNewEmp (string empName,uint empSalary, uint empIdIndex, uint empID);
event LogUpdateEmpSal(string empName, uint empSalary);
event LogDeleteEmp(string empName, uint empIdIndex);
function isEmp(uint empIDck)
public
constant
returns(bool isIndeed)
{
if(db9Index.length == 0) return false;
return (db9Index[dataBase9[empIDck].empIdIndex] == empIDck);
}
function insertEmp(
string empName,
uint empSalary,
uint empID)
public
{
if(isEmp(empID)) revert('duplicate');
dataBase9[empID].empName = empName;
dataBase9[empID].empSalary = empSalary;
dataBase9[empID].empIdIndex = db9Index.push(empID)-1;
emit LogNewEmp(
dataBase9[empID].empName,
dataBase9[empID].empSalary,
dataBase9[empID].empIdIndex,
empID);
}
function getEmpData(uint empIDck)
public
constant
returns(string empName, uint empSalary)
{
if(!isEmp(empIDck)) return('not found',0);
return(
dataBase9[empIDck].empName,
dataBase9[empIDck].empSalary);
}
//
function updateEmpSalary(uint empID, uint newSalary)
public
returns(bool success)
{
if(!isEmp(empID)) revert('not found');
dataBase9[empID].empSalary = newSalary;
emit LogUpdateEmpSal(
dataBase9[empID].empName,
dataBase9[empID].empSalary);
return true;
}
//
function getEmpCount()
public
constant
returns(uint count, string retString)
{
return (db9Index.length,'Hello Prithwis - v1.0 ');
}
function deleteEmp(uint empID)
public
{
if(!isEmp(empID)) revert('not found');
string storage empNameToDelete = dataBase9[empID].empName;
uint rowToDelete = dataBase9[empID].empIdIndex;
uint keyToMove = db9Index[db9Index.length-1];
db9Index[rowToDelete] = keyToMove;
dataBase9[keyToMove].empIdIndex = rowToDelete;
db9Index.length--;
emit LogDeleteEmp(
empNameToDelete,
rowToDelete);
}
// function getUserAtIndex(uint index)
// public
// constant
// returns(address userAddress)
// {
// return userIndex[index];
// }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment