From 018c79205b25e13fe130066651a44293c064669c Mon Sep 17 00:00:00 2001 From: martin machiebe Date: Sun, 28 Jul 2024 23:04:52 +0100 Subject: [PATCH 1/2] assignment on solidity --- contracts/StudentRegistry.sol | 83 ++++++++++++++++++++--------------- 1 file changed, 48 insertions(+), 35 deletions(-) diff --git a/contracts/StudentRegistry.sol b/contracts/StudentRegistry.sol index 4f5bb554..a2946c32 100644 --- a/contracts/StudentRegistry.sol +++ b/contracts/StudentRegistry.sol @@ -2,12 +2,12 @@ pragma solidity ^0.8.24; contract StudentRegistry { - //custom data type + struct Student { address studentAddr; string name; - uint256 studentId; uint8 age; + uint studentId; } address public owner; @@ -16,31 +16,29 @@ contract StudentRegistry { owner = msg.sender; } - //dynamic array of students Student[] private students; - mapping(address => Student) public studentsMapping; + mapping(address => Student) public studentMap; - modifier onlyOwner () { - require( owner == msg.sender, "You fraud!!!"); + modifier onlyOwner() { + require(owner == msg.sender, "you're not authorized"); _; } - modifier isNotAddressZero () { + modifier isNotAddressZero() { require(msg.sender != address(0), "Invalid Address"); _; } - function addStudent( - address _studentAddr, - string memory _name, - uint8 _age - ) public onlyOwner isNotAddressZero { - - require( bytes(_name).length > 0, "Name cannot be blank"); - require( _age >= 18, "This student is under age"); + event StudentAdded(address indexed studentAddr, string name, uint8 age, uint studentId); + event StudentDeleted(address indexed studentAddr); - uint256 _studentId = students.length + 1; + /// @dev function to add student to the students array and mapping + /// @notice adds student + function addStudent(address _studentAddr, string memory _name, uint8 _age) public onlyOwner isNotAddressZero { + require(bytes(_name).length > 0, "input cannot be empty"); + require(_age >= 18, "You are not up to age"); + uint _studentId = students.length + 1; Student memory student = Student({ studentAddr: _studentAddr, name: _name, @@ -49,32 +47,36 @@ contract StudentRegistry { }); students.push(student); - // add student to studentsMapping - studentsMapping[_studentAddr] = student; + studentMap[_studentAddr] = student; + + emit StudentAdded(_studentAddr, _name, _age, _studentId); } - function getStudent(uint8 _studentId) public isNotAddressZero view returns (Student memory) { + /// @dev function to get student using studentId + /// @notice gets student ID + function getStudent(uint256 _studentId) public view onlyOwner isNotAddressZero returns (Student memory) { return students[_studentId - 1]; } - - - function getStudentFromMapping(address _studentAddr) - public - isNotAddressZero - view - returns (Student memory) - { - return studentsMapping[_studentAddr]; + /// @dev function to get student using studentAddr + /// @notice gets student Address + function getStudentAddr(address _ownerAddr) public view onlyOwner isNotAddressZero returns (Student memory) { + return studentMap[_ownerAddr]; } + function updateStudent(address _studentAddr, string memory _name, uint8 _age, uint _studentId) public onlyOwner isNotAddressZero { + Student storage studentToUpdate = studentMap[_studentAddr]; + studentToUpdate.name = _name; + studentToUpdate.age = _age; + studentToUpdate.studentId = _studentId; + } + /// @dev function to delete using address + /// @notice resets it back to the initial state + function deleteStudent(address _studentAddr) public onlyOwner isNotAddressZero { + require(studentMap[_studentAddr].studentAddr != address(0), "student not available"); - function deleteStudent(address _studentAddr) public onlyOwner isNotAddressZero{ - - require(studentsMapping[_studentAddr].studentAddr != address(0), "Student does not exist"); - - // delete studentsMapping[_studentAddr]; + // delete studentMap[_studentAddr]; Student memory student = Student({ studentAddr: address(0), @@ -83,7 +85,18 @@ contract StudentRegistry { studentId: 0 }); - studentsMapping[_studentAddr] = student; + studentMap[_studentAddr] = student; + + emit StudentDeleted(_studentAddr); + } + + /// @dev function to delete using Uint + /// @notice resets it back to the initial state + function deleteStudentUint(uint _student) public onlyOwner isNotAddressZero { + require(_student > 0, "student not available or does not exist"); + + delete students[_student - 1]; + emit StudentDeleted(students[_student - 1].studentAddr); } -} +} \ No newline at end of file From e7cd9679b516b5b655a8086eb5db58ceae0cccb7 Mon Sep 17 00:00:00 2001 From: martin machiebe Date: Sun, 28 Jul 2024 23:11:26 +0100 Subject: [PATCH 2/2] updated the code --- contracts/StudentRegistry.sol | 151 +++++++++++++++++++++++++++++++--- 1 file changed, 140 insertions(+), 11 deletions(-) diff --git a/contracts/StudentRegistry.sol b/contracts/StudentRegistry.sol index a2946c32..c6220ce2 100644 --- a/contracts/StudentRegistry.sol +++ b/contracts/StudentRegistry.sol @@ -1,13 +1,105 @@ +// SPDX-License-Identifier: MIT +// pragma solidity ^0.8.24; + +// contract StudentRegistry { +// //custom data type +// struct Student { +// address studentAddr; +// string name; +// uint256 studentId; +// uint8 age; +// } + +// address public owner; + +// constructor() { +// owner = msg.sender; +// } + +// //dynamic array of students +// Student[] private students; + +// mapping(address => Student) public studentsMapping; + +// modifier onlyOwner () { +// require( owner == msg.sender, "You fraud!!!"); +// _; +// } + +// modifier isNotAddressZero () { +// require(msg.sender != address(0), "Invalid Address"); +// _; +// } + +// function addStudent( +// address _studentAddr, +// string memory _name, +// uint8 _age +// ) public onlyOwner isNotAddressZero { + +// require( bytes(_name).length > 0, "Name cannot be blank"); +// require( _age >= 18, "This student is under age"); + +// uint256 _studentId = students.length + 1; +// Student memory student = Student({ +// studentAddr: _studentAddr, +// name: _name, +// age: _age, +// studentId: _studentId +// }); + +// students.push(student); +// // add student to studentsMapping +// studentsMapping[_studentAddr] = student; +// } + +// function getStudent(uint8 _studentId) public isNotAddressZero view returns (Student memory) { +// return students[_studentId - 1]; +// } + + + +// function getStudentFromMapping(address _studentAddr) +// public +// isNotAddressZero +// view +// returns (Student memory) +// { +// return studentsMapping[_studentAddr]; +// } + + + +// function deleteStudent(address _studentAddr) public onlyOwner isNotAddressZero{ + +// require(studentsMapping[_studentAddr].studentAddr != address(0), "Student does not exist"); + +// // delete studentsMapping[_studentAddr]; + +// Student memory student = Student({ +// studentAddr: address(0), +// name: "", +// age: 0, +// studentId: 0 +// }); + +// studentsMapping[_studentAddr] = student; + +// } +// } +// ` + + + // SPDX-License-Identifier: MIT pragma solidity ^0.8.24; contract StudentRegistry { - struct Student { address studentAddr; string name; uint8 age; - uint studentId; + uint256 studentId; } address public owner; @@ -30,15 +122,24 @@ contract StudentRegistry { _; } - event StudentAdded(address indexed studentAddr, string name, uint8 age, uint studentId); + event StudentAdded( + address indexed studentAddr, + string name, + uint8 age, + uint256 studentId + ); event StudentDeleted(address indexed studentAddr); /// @dev function to add student to the students array and mapping /// @notice adds student - function addStudent(address _studentAddr, string memory _name, uint8 _age) public onlyOwner isNotAddressZero { + function addStudent( + address _studentAddr, + string memory _name, + uint8 _age + ) public onlyOwner isNotAddressZero { require(bytes(_name).length > 0, "input cannot be empty"); require(_age >= 18, "You are not up to age"); - uint _studentId = students.length + 1; + uint256 _studentId = students.length + 1; Student memory student = Student({ studentAddr: _studentAddr, name: _name, @@ -54,17 +155,34 @@ contract StudentRegistry { /// @dev function to get student using studentId /// @notice gets student ID - function getStudent(uint256 _studentId) public view onlyOwner isNotAddressZero returns (Student memory) { + function getStudent(uint256 _studentId) + public + view + onlyOwner + isNotAddressZero + returns (Student memory) + { return students[_studentId - 1]; } /// @dev function to get student using studentAddr /// @notice gets student Address - function getStudentAddr(address _ownerAddr) public view onlyOwner isNotAddressZero returns (Student memory) { + function getStudentAddr(address _ownerAddr) + public + view + onlyOwner + isNotAddressZero + returns (Student memory) + { return studentMap[_ownerAddr]; } - function updateStudent(address _studentAddr, string memory _name, uint8 _age, uint _studentId) public onlyOwner isNotAddressZero { + function updateStudent( + address _studentAddr, + string memory _name, + uint8 _age, + uint256 _studentId + ) public onlyOwner isNotAddressZero { Student storage studentToUpdate = studentMap[_studentAddr]; studentToUpdate.name = _name; studentToUpdate.age = _age; @@ -73,8 +191,15 @@ contract StudentRegistry { /// @dev function to delete using address /// @notice resets it back to the initial state - function deleteStudent(address _studentAddr) public onlyOwner isNotAddressZero { - require(studentMap[_studentAddr].studentAddr != address(0), "student not available"); + function deleteStudent(address _studentAddr) + public + onlyOwner + isNotAddressZero + { + require( + studentMap[_studentAddr].studentAddr != address(0), + "student not available" + ); // delete studentMap[_studentAddr]; @@ -92,7 +217,11 @@ contract StudentRegistry { /// @dev function to delete using Uint /// @notice resets it back to the initial state - function deleteStudentUint(uint _student) public onlyOwner isNotAddressZero { + function deleteStudentUint(uint256 _student) + public + onlyOwner + isNotAddressZero + { require(_student > 0, "student not available or does not exist"); delete students[_student - 1];