UserProfile.sol
1 // SPDX-License-Identifier: MIT 2 pragma solidity ^0.8.0; 3 4 import "./Post.sol"; 5 6 contract UserProfile { 7 address private owner; 8 string private name; 9 Post[] private posts; 10 11 struct Profile { 12 address owner; 13 string name; 14 Post[] posts; 15 } 16 17 constructor(string memory _name) { 18 owner = msg.sender; 19 name = _name; 20 } 21 22 modifier onlyOwner() { 23 require(msg.sender == owner, "Caller is not the owner"); 24 _; 25 } 26 27 function createPost(string memory _content) external onlyOwner { 28 posts.push(new Post(address(this), _content)); 29 } 30 31 function setName(string memory _newName) external onlyOwner { 32 name = _newName; 33 } 34 35 function getAllPosts() external view returns (Post[] memory) { 36 return posts; 37 } 38 39 function getName() external view returns (string memory) { 40 return name; 41 } 42 43 function getOwner() external view returns (address) { 44 return owner; 45 } 46 47 function getProfile() external view returns (Profile memory) { 48 return Profile({ 49 owner: owner, 50 name: name, 51 posts: posts 52 }); 53 } 54 }