4REALOSS.sol 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /**
  2. *Submitted for verification at Etherscan.io on 2024-07-15
  3. */
  4. // SPDX-License-Identifier: MIT
  5. pragma solidity ^0.6.8;
  6. pragma experimental ABIEncoderV2;
  7. contract REALOSS {
  8. // Structure of project
  9. struct Project {
  10. int repoid;
  11. string projectname;
  12. string ipfsCID;
  13. string description;
  14. }
  15. mapping(int => Project) private projects;
  16. int private nextRepoId = 1;
  17. // Function to add project details
  18. function addProject(string memory projectname, string memory ipfsCID, string memory description) public {
  19. Project memory newProject = Project(nextRepoId, projectname, ipfsCID, description);
  20. projects[nextRepoId] = newProject;
  21. nextRepoId++;
  22. }
  23. // Function to get details of a project
  24. function getProject(int repoid) public view returns (string memory, string memory, string memory) {
  25. Project memory project = projects[repoid];
  26. require(project.repoid != 0, "Project not found"); // revert if project is not found
  27. return (project.projectname, project.ipfsCID, project.description);
  28. }
  29. // Function to get all project IDs and names
  30. function getProjects() public view returns (int[] memory, string[] memory) {
  31. int[] memory ids = new int[](uint(nextRepoId) - 1);
  32. string[] memory names = new string[](uint(nextRepoId) - 1);
  33. for (uint i = 0; i < uint(nextRepoId) - 1; i++) {
  34. ids[i] = int(i + 1);
  35. names[i] = projects[int(i + 1)].projectname;
  36. }
  37. return (ids, names);
  38. }
  39. // Function to search projects by name
  40. function searchProjectByName(string memory projectname) public view returns (int[] memory, string[] memory, string[] memory) {
  41. uint count = 0;
  42. // First count matching projects
  43. for (uint i = 1; i < uint(nextRepoId); i++) {
  44. if (keccak256(bytes(projects[int(i)].projectname)) == keccak256(bytes(projectname))) {
  45. count++;
  46. }
  47. }
  48. int[] memory ids = new int[](count);
  49. string[] memory ipfsCIDs = new string[](count);
  50. string[] memory descriptions = new string[](count);
  51. uint index = 0;
  52. for (uint i = 1; i < uint(nextRepoId); i++) {
  53. if (keccak256(bytes(projects[int(i)].projectname)) == keccak256(bytes(projectname))) {
  54. ids[index] = projects[int(i)].repoid;
  55. ipfsCIDs[index] = projects[int(i)].ipfsCID;
  56. descriptions[index] = projects[int(i)].description;
  57. index++;
  58. }
  59. }
  60. return (ids, ipfsCIDs, descriptions);
  61. }
  62. }