Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat - assignment on update, delete and events in student registry system #4

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions contracts/Ownable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,32 @@
pragma solidity >=0.7.0 <0.9.0;


contract Ownable {
contract Ownable{

address private owner;
address payable internal owner;

event ChangeOwner(address indexed oldOwner, address indexed newOwner);
event ChangeOwner(address indexed oldOwner, address indexed newOwner);

constructor(){
owner = msg.sender;
constructor() payable {
owner = payable(msg.sender);
}


modifier onlyOwner {
modifier onlyOwner(){
require(owner == msg.sender, "Caller not owner");
_;
}



function getOwner() public view returns (address){
function getOwner() public view returns (address) {
return owner;
}


function changeOwner(address _newOwner) internal onlyOwner {
require(_newOwner != address(0), "Owner can not be address zero");
function changeOwner(address payable _newOwner) internal onlyOwner {
require(_newOwner != address(0), "Owner cannot be address zero");

emit ChangeOwner(owner, _newOwner);
owner = _newOwner;
}
}
}
15 changes: 8 additions & 7 deletions contracts/Student.sol
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// SPDX-License-Identifier: MIT
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
struct Student {
address studentAddr;
string name;
uint256 studentId;
uint8 age;
}
struct Student {
address studentAddr;
uint256 studentID;
string name;
uint8 age;
bool hasPaid;
}
128 changes: 72 additions & 56 deletions contracts/StudentRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,94 +3,110 @@ pragma solidity ^0.8.24;
import "./Ownable.sol";
import "./Student.sol";


contract StudentRegistry is Ownable {
//custom erros

// custom error
error NameIsEmpty();
error UnderAge(uint8 age, uint8 expectedAge);
error UnderAge(uint8 age, uint8 expectedAge);

//custom data type


//dynamic array of students
Student[] private students;
// Dynamic array of student
Student[] private studentsThatPaid;

mapping(address => Student) public studentsMapping;
Student[] private authorizedStudents;

mapping(address => Student) public studentMapping;

modifier isNotAddressZero() {
mapping(address => uint256) public receipt;

modifier isNotAddressZero () {
require(msg.sender != address(0), "Invalid Address");
_;
}

function addStudent(
address _studentAddr,
string memory _name,
// Events
event StudentAdded(address indexed studentAddress, uint256 studentId, string name);
event StudentDeleted(address indexed studentAddress, uint256 studentId, string name);

function registerStudent(
address _studentAddr,
string memory _name,
uint8 _age
) public isNotAddressZero {
if (bytes(_name).length == 0) {
) public payable {
uint256 registrationFee = msg.value;

require(receipt[_studentAddr] == 0, "Already Registered");
require(registrationFee == 1 ether, "Incorrect Registration Fee");

if(bytes(_name).length == 0){
revert NameIsEmpty();
}

if (_age < 18) {
revert UnderAge({age: _age, expectedAge: 18});
revert UnderAge({ age: _age, expectedAge: 18 });
}

uint256 _studentId = students.length + 1;
Student memory student = Student({
studentAddr: _studentAddr,
name: _name,
age: _age,
studentId: _studentId
studentID: 0,
hasPaid: true
});

studentMapping[_studentAddr] = student;

students.push(student);
// add student to studentsMapping
studentsMapping[_studentAddr] = student;
}
(bool success, ) = address(this).call{value: registrationFee}("");
require(success, "Failed to send payment");

function getStudent(uint8 _studentId)
public
view
isNotAddressZero
returns (Student memory)
{
return students[_studentId - 1];
receipt[_studentAddr] = registrationFee;

studentsThatPaid.push(student);
}

function getStudentFromMapping(address _studentAddr)
public
view
isNotAddressZero
returns (Student memory)
{
return studentsMapping[_studentAddr];
function studentPaymentStatus (address _studentAddr) public view returns (bool) {
return studentMapping[_studentAddr].hasPaid;
}

function deleteStudent(address _studentAddr)
public
onlyOwner
isNotAddressZero
{
require(
studentsMapping[_studentAddr].studentAddr != address(0),
"Student does not exist"
);
function studentsWhoPaid() public view onlyOwner returns (Student[] memory) {
return studentsThatPaid;
}

// delete studentsMapping[_studentAddr];
function getAuthorizedStudents() public view onlyOwner returns (Student[] memory) {
return authorizedStudents;
}

Student memory student = Student({
studentAddr: address(0),
name: "",
age: 0,
studentId: 0
function authorizeStudentRegistration(address _studentAddr) private onlyOwner isNotAddressZero {
require(studentMapping[_studentAddr].studentID == 0, "Student has Already been authorized");
require(studentMapping[_studentAddr].hasPaid == true, "Fees have not been paid");

uint256 _studentID = authorizedStudents.length + 1;
Student memory _student = Student({
studentAddr: studentMapping[_studentAddr].studentAddr,
name: studentMapping[_studentAddr].name,
age: studentMapping[_studentAddr].age,
hasPaid: studentMapping[_studentAddr].hasPaid,
studentID: _studentID
});

studentsMapping[_studentAddr] = student;
}
authorizedStudents.push(_student);
//add student to studentMapping
studentMapping[_studentAddr] = _student;

//emit
emit StudentAdded(_studentAddr, _studentID, _student.name);
}


function withdraw() public {
uint256 amount = address(this).balance;
require(amount > 0, "No Funds");
require(msg.sender == owner, "Only the owner can withdraw");
(bool success, ) = owner.call{value: amount}("");
require(success, "Failed to withdraw ETH");
}

function modifyOwner(address _newOwner) public {
function modifyOwner(address payable _newOwner) public {
changeOwner(_newOwner);
}
}

receive() external payable {}
}