I'm trying to make a contract which will check if there is already some data in mapping. If so, it will then replace the old data with the new. If not - insert new record. Sounds easy, but im learning and spent hours triyng to find a solution. How can I do that?
function MyToken() public
{
}
struct DATA
{
uint Id;
uint Date;
}
mapping (uint => DATA) public data;
uint public total = 0;
function add(uint Id, uint Date) public returns (uint)
{
if(data[Id].Id == 0) // Checking if there is some records with this ID
{
data[total] = DATA(Id, Date);
total++;
return total;
}
else // If some data found - replace it with new
{
data[Id].Date = Date;
}
}
For some reason this code just insert new records. I need to have it changing old data.