-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstudentRecord.sol
66 lines (51 loc) · 1.57 KB
/
studentRecord.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
contract Record {
address owner;
struct studentData {
uint256 id;
string name;
string class;
}
// Initializes msg.sender as owner
constructor() {
owner = msg.sender;
}
// requires owner to execute function
modifier onlyOwner() {
require(msg.sender == owner, "Must be contract owner");
_;
}
// Creates an array of all students
studentData[] allStudent;
mapping(address => studentData) public students;
// Initializes student struct
function addStudent(address _address, uint256 _id, string memory _name, string memory _class) public onlyOwner {
studentData storage s = students[_address];
s.id = _id;
s.name = _name;
s.class = _class;
// Pushes student to student array above
allStudent.push(s);
}
// Get data of student address inputed
function getStudent(address _address) public view returns (studentData memory) {
return students[_address];
}
// Gets all students in array
function getAllStudents() external view returns (studentData[] memory) {
return allStudent;
}
// Updates student
function updateStudent(address _address, uint256 _id, string memory _name, string memory _class) public onlyOwner {
studentData storage s = students[_address];
s.id = _id;
s.name = _name;
s.class = _class;
allStudent.push(s);
}
// Deletes student
function clearStudentData() public {
delete allStudent;
}
}