Daniel M
Daniel M

Reputation: 3

Smart contract with text storage ethereum

I am trying to make a simple "text editor" in solidity on an Ethereum network smart contract. I want to make a data entry function, which collects the text in a STRING and a BYTES10 variable that collects the position of the text. You must store the text neatly so that you can access it later. And you should be able to enter new text without replacing the old one. Then an output function that returns the text and the coordinates, all those entered into the network since the beginning of the contract. And a function that allows you to delete data, in case any is entered wrong. I have this code so far:

    pragma solidity <0.9.0;
    contract texteditor {
        struct book {
            string block;
            bytes10 coordinates;
        }
        book [] public books;
        function save(string calldata _blocks, bytes10   _coordinates) public{
            books.push(book(_blocks, _coordinates));
        }
        function read()view public returns (string){
            return books[_block][_coordinates];
        }
        function remove(string _blocks, bytes10 _coordinates) private {
            delete book[_blocks][_coordinates];
        }}

The function to save the text I think is fine, but with the other two I am having problems compiling, I don't know if because of the compiler version or because the functions are not right. The information that I find in blogs and other help about this topic, apparently is quite outdated in terms of the compiler version and tends to give me problems. I imagine that I have to do a mapping, but I have not found the way yet. I appreciate any help you can give me to move me forward on this. Thank you very much for your time.

Upvotes: 0

Views: 838

Answers (1)

ashish0411
ashish0411

Reputation: 136

pragma solidity ^0.8.4;
// SPDX-License-Identifier: MIT

contract texteditor {
     uint256 public id=0;

struct book {
    string block;
    string coordinates;
}

mapping(uint256=>mapping(uint256 => book)) bookStore;
mapping(uint256=>uint256) bookBlockIndex;

function save(uint256 bookId,string calldata _block, string calldata _coordinates) public{
    book memory temp_book =  book(_block,_coordinates);
    bookStore[bookId][bookBlockIndex[bookId]]=temp_book;
    bookBlockIndex[bookId]++;
}

function read_book(uint256 bookid,uint256 bookBlockid)view public returns (string memory,string memory){
    return (bookStore[bookid][bookBlockid].block,bookStore[bookid][bookBlockid].coordinates);
}

function remove_book(uint bookid,uint256 bookBlockid) public {
    delete bookStore[bookid][bookBlockid];
}

}

This might help

Upvotes: 1

Related Questions