// This Smart contract was auto generated by EthairBaloons 
// https://github.com/petrosDemetrakopoulos/ethairballoons
// SPDX-License-Identifier: {{contract.license}}
pragma solidity >=0.6.2;
pragma experimental ABIEncoderV2;

contract {{contract.name}} {
	{{contract.idDataType}} public dataId;
	uint public numberOfRecords = 0;
	{{contract.idDataType}}[] public recordsList;

	event dataAdded(string dat);

	struct {{contract.structName}} {
		string payload;
		uint listPointer;
	}

	mapping({{contract.idDataType}} => {{contract.structName}}) public facts;

	function isRecord({{contract.idDataType}} recordAddress) internal view returns (bool isRec) {
		if(recordsList.length == 0) return false;
		return (recordsList[facts[recordAddress].listPointer] == recordAddress);
	}

	function getRecordCount() external view returns (uint recCount){
		return recordsList.length;
	}

	function addRecord(string memory payload, {{contract.idDataType}} ID) external returns (bool success) {
		if(isRecord(ID)) revert('record with this id already exists');
		facts[ID].payload = payload;
		recordsList.push(ID);
		facts[ID].listPointer = recordsList.length - 1;
		numberOfRecords++;
		return (true);
	}

	function getRecord({{contract.idDataType}} id) external view returns (string memory payload){
		if(!isRecord(id)) revert('record with this id does not exist');
		return (facts[id].payload);
	}

	function updateRecord({{contract.idDataType}} id, string memory payload) external returns (bool success){
		if(!isRecord(id)) revert('record with this id does not exist');
		facts[id].payload = payload;
		return (true);
	}

	function getAllRecords() external view returns (string[] memory payloads) {
		string[] memory payloadss = new string[](numberOfRecords);
		for (uint i = 0; i < numberOfRecords; i++) {
			{{contract.structName}} storage fact = facts[recordsList[i]];
			payloadss[i] = fact.payload;
		}
		return (payloadss);
	}

	function deleteRecord({{contract.idDataType}} id) external returns (bool success) {
		if(!isRecord(id)) revert('record with this id does not exist');
		uint rowToDelete = facts[id].listPointer;
		{{contract.idDataType}} keyToMove = recordsList[recordsList.length-1];
		recordsList[rowToDelete] = keyToMove;
		facts[keyToMove].listPointer = rowToDelete;
		recordsList.pop();
		numberOfRecords--;
		return (true);
	}
}