Aeternity basics

Hello i devs i have two records one is person and other is address how can i retrive address of specific person by using person ID

contract ContactContract=

      record person ={
          name :string,
          id :int
         }

  record addresses={
          index :int,
         person_id:int,
        address:string
     }

Hello, do you have a list of records, or what is your data-structure to retrieve from?
Ideally you’d have a mapping of person id to addresses.

am new to this language. Can you help show me how to map

the structure is like that

contract ContactContact =

    record person ={
                  name : string,
                  id: int
               }

     record   p_address ={
             index:int,
             person_id :int,
             p_address: string //this can be mulitple mean person can have multiple addresss
        
          }

    record state ={
            persons :map(int,person),
            p_addresses :map(int,p_address)
           int personLength  :int,
         

       }
entrypoint init() = {
    persons = {},
    p_addresses ={},
    personLength = 0 ,

}

//what i want is to get person addreses by using…

entrypoint getAdresses(person_id:int)={

//retrive all addresses releted to person id
}

does one person only have one address? then you could do

@compiler >= 4

contract Example =
  record person = {
    name : string,
    id : int }

  record addresses = {
    index : int,
    address : string }

  record state = {
    persons : list(person),
    address_for_person : map(int, addresses) }
    
  entrypoint init() =
    { persons = [], address_for_person = {}}

  stateful entrypoint add_test_data() =
    let person = { name = "person", id = 1 }
    let addresses = { index = 1, address = "address" }
    put(state{persons = person :: state.persons, address_for_person = state.address_for_person{ [person.id] = addresses }})
    
  entrypoint get_test_data(person_id : int) =
    state.address_for_person[person_id]

this example stays quite close to your datastructure, but you could simplify more:

@compiler >= 4

contract Example =
  record addresses = { address : string }
  
  record person = {
    name : string,
    address : addresses }

  record state = { persons : map(int, person) }
    
  entrypoint init() =
    { persons = {} }

  stateful entrypoint add_test_data() =
    let person = { name = "person", address = { address = "address" } }
    put(state{persons = state.persons{[Map.size(state.persons)] = person}})
    
  entrypoint get_test_data(person_id : int) =
    state.persons[person_id]
1 Like

one person has multiple addresses (i.e LOCATIONS not contract address), what i mean take example one person can have multiple phone number

1 Like

this way you could represent a many-to-many relationship with somewhat performant lookups

@compiler >= 4

contract Example =
  type persons = map(int, string)
  type addresses = map(int, string)
  
  record state =
    { persons : persons
    , addresses : addresses
    , persons_to_addresses : map(int, list(int))
    , addresses_to_persons : map(int, list(int)) }
    
  entrypoint init() = { persons = {}, addresses = {}, persons_to_addresses = {}, addresses_to_persons = {} }
    
  stateful entrypoint add_address_to_person(person_id : int, address_id : int) =
    require(Map.member(person_id, state.persons), "PERSON_MISSING")
    require(Map.member(person_id, state.persons), "ADDRESS_MISSING")
    put(state{ persons_to_addresses = state.persons_to_addresses{ [person_id] @ addresses = address_id :: addresses } })
    put(state{ addresses_to_persons = state.addresses_to_persons{ [address_id] @ persons = person_id :: persons } })
3 Likes

I think it would be great to share such code / Sophia examples via the fire editor so people can try out call and put functions.

Yah! it will be good

1 Like