People contract getPerson

@filip
This is from the old Eth 201 chapters. I added this functionality to the People’s contract which sends 1 eth as a reward to Seniors, based on an Enum state. But, I could not get this contract to get persons based on uint id. When the get functions returns, I added a uint id variable and you see it but I cannot succesfully get it to allow the user to select a uint id number…any help, would be appreciated

Example (I tried other ways but here is the basic idea):

function getPerson(uint id) public view returns(uint id, string memory name) {
       address creator = msg.sender;
       return (people[creator].id, people[creator].name);
   }
//import "./Ownable.sol";
// SPDX-License-Identifier: MIT
pragma solidity 0.7.5;
pragma abicoder v2;

contract HelloWorld {
    
    struct Person {
       uint id;
       string name;
       uint age;
       uint height;
       bool senior;
   }
   
   mapping(address => Person) private people;
   address[] private creators;
   

   uint public balance;
   address owner;
   address payable wallet;
   
   constructor(address payable _wallet) {
       owner = msg.sender;
       wallet = _wallet;
       currentStatus = Status.notSenior;
   }
   
   enum Status {notSenior, senior}
   Status currentStatus;
   
   
   modifier onlyOwner() {
        require(msg.sender == owner, "You must be an owner to withdrawAll");
        _;
   }
   
   modifier costs(uint amount) {
       require(msg.value >= amount, "Not enough, send more Ether");
       _;
   }
   
   
   event Reward(address indexed _senior, uint amount);
   event NoReward(address indexed _notSenior, uint amount);
   
   event personCreated(string name, bool senior);
   event personDeleted(string name, bool senior, address deletedBy);
   
   
   function createPerson(uint id, string memory name, uint age, uint height) public payable costs(1 ether) {
       require(age < 150, "Age needs to be below 150");
       require(msg.value >= 1 ether);
       balance += msg.value;
       
       Person memory newPerson;
       newPerson.id = id;
       newPerson.name = name;
       newPerson.age = age;
       newPerson.height = height;
       newPerson.senior = true;
       
       // person is < 65 ; not a senieor
       if(age >= 65 ) {
           
           if(newPerson.senior = true) {
               isSenior();
               wallet.transfer(msg.value);
               emit Reward(msg.sender, msg.value);
           }
           newPerson.senior = true;
       } else {
           newPerson.senior = false;
           notSenior();
           wallet.transfer(0 ether);
           emit NoReward(msg.sender, 0);
       } 
       
       insertPerson(newPerson);
       creators.push(msg.sender);
       
       assert(
           keccak256(
               abi.encodePacked(
                   people[msg.sender].id,
                   people[msg.sender].name,
                   people[msg.sender].age,
                   people[msg.sender].height,
                   people[msg.sender].senior
                   )
               )
               == keccak256(abi.encodePacked(
                   newPerson.id,
                   newPerson.name,
                   newPerson.age,
                   newPerson.height,
                   newPerson.senior
                   
                   )
                   
                 )
               
               );
               emit personCreated(newPerson.name, newPerson.senior);
               
       
   }
   
   function insertPerson(Person memory newPerson) private {
       address creator = msg.sender;
       people[creator] = newPerson;
       
   }
    
   
    function getPerson() public view returns(uint id, string memory name, uint age, uint height, bool senior) {
       address creator = msg.sender;
       return (people[creator].id, people[creator].name, people[creator].age, people[creator].height, people[creator].senior);
   }
   
   function deletePerson(address creator) public onlyOwner {
       string memory name = people[creator].name;
       bool senior = people[creator].senior;
       
       delete people[creator];
       assert(people[creator].age == 0);
       emit personDeleted(name, senior, owner);
   }
   function getCreator(uint index) public view returns(address) {
       return creators[index];
   }
   function withdrawAll() public onlyOwner returns(uint) {
       uint toTransfer = balance;
       balance = 0;
       msg.sender.transfer(toTransfer);
       return toTransfer;
   }

   
   function isSenior() private view returns(bool) {
       return currentStatus == Status.senior;
   }
   
   function notSenior() private view returns(bool) {
       return currentStatus == Status.notSenior;
   }
   
    
    
}

Hey @bjamRez

Your mapping key is an address mapping(address => Person) private people; therefore when you do return (people[creator].id) you will return the id property of your Person struct.
The id you are sending as parameter is not used.

Cheers,
Dani

1 Like

Thanks…the People contract was from a lesson in the previous Eth 201 exercise on error-handling, JS node, Truffle and Ganache. So I guess I would have to rebuild it from scratch if I wanted to add that feature; which is okay. Thanks again

1 Like