mucanyu
3/15/2019 - 10:27 AM

Oyla.sol

pragma solidity ^0.5.0;

contract Oyla {
    struct Election {
        string name;
        uint candidateCount;
        uint voterCount;
        address organizer;
    
        // uint `key` represents electionID
        mapping (uint => Candidate) candidates;
    }
    
    struct Candidate {
        uint id;
        string name;
        uint voteCount;
    }
    
    mapping (uint => Election) createdElections;
    
    modifier isOrganizer(uint _elecID) {
        require(msg.sender == createdElections[_elecID].organizer);
        _;
    }
    
    function createElection(uint _elecID, string memory _name, uint _candidateCount, uint _voterCount, address _organizer) public {
        createdElections[_elecID] = Election(_name, _candidateCount, _voterCount, _organizer);
    }
    
    function addCandidate(uint _elecID, uint _candidateID, string memory _name) isOrganizer(_elecID) public {
        Candidate memory cand = Candidate(_candidateID, _name, 0);
        Election storage election = createdElections[_elecID];
        election.candidates[_candidateID] = cand;
    }
    
    function vote(uint _elecID, uint _candidateID) public {
        createdElections[_elecID].candidates[_candidateID].voteCount++;
    }
    
    function getCandidateVoteCount(uint _elecID, uint _candidateID) view public returns (uint) {
        return createdElections[_elecID].candidates[_candidateID].voteCount;
    }
}