-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathEHR.sol
More file actions
97 lines (79 loc) · 3.34 KB
/
EHR.sol
File metadata and controls
97 lines (79 loc) · 3.34 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract EHR {
struct Patient {
uint id;
string name;
uint age;
string[] diseases;
address createdBy;
}
struct Doctor {
uint id;
string name;
string qualification;
string workPlace;
address createdBy;
}
struct Medicine {
uint id;
string name;
string expiryDate;
string dose;
uint price;
}
mapping(uint => Patient) patients;
mapping(uint => Doctor) doctors;
mapping(uint => Medicine) medicines;
mapping(uint => uint[]) patientMedicines;
uint public patientCount;
uint public doctorCount;
uint public medicineCount;
modifier onlyDoctor(uint _doctorId) {
require(doctors[_doctorId].createdBy == msg.sender, "Only the registered doctor can perform this action");
_;
}
modifier onlyPatient(uint _patientId) {
require(patients[_patientId].createdBy == msg.sender, "Only the registered patient can perform this action");
_;
}
function registerPatient(string memory _name, uint _age) public {
patientCount++;
patients[patientCount] = Patient(patientCount, _name, _age, new string[](0), msg.sender);
}
function registerDoctor(string memory _name, string memory _qualification, string memory _workPlace) public {
doctorCount++;
doctors[doctorCount] = Doctor(doctorCount, _name, _qualification, _workPlace, msg.sender);
}
function addDisease(uint _patientId, string memory _disease) public onlyPatient(_patientId) {
patients[_patientId].diseases.push(_disease);
}
function addMedicine(string memory _name, string memory _expiryDate, string memory _dose, uint _price) public {
medicineCount++;
medicines[medicineCount] = Medicine(medicineCount, _name, _expiryDate, _dose, _price);
}
function prescribeMedicine(uint _patientId, uint _medicineId) public onlyDoctor(_patientId) {
patientMedicines[_patientId].push(_medicineId);
}
function updatePatientAge(uint _patientId, uint _newAge) public onlyPatient(_patientId) {
patients[_patientId].age = _newAge;
}
function viewPatient(uint _patientId) public view returns (uint, string memory, uint, string[] memory) {
require(patients[_patientId].createdBy == msg.sender, "You are not authorized to view this patient's data");
Patient memory p = patients[_patientId];
return (p.id, p.name, p.age, p.diseases);
}
function viewDoctor(uint _doctorId) public view returns (uint, string memory, string memory, string memory) {
require(doctors[_doctorId].createdBy == msg.sender, "You are not authorized to view this doctor's data");
Doctor memory d = doctors[_doctorId];
return (d.id, d.name, d.qualification, d.workPlace);
}
function viewMedicine(uint _medicineId) public view returns (uint, string memory, string memory, string memory, uint) {
Medicine memory m = medicines[_medicineId];
return (m.id, m.name, m.expiryDate, m.dose, m.price);
}
function viewPatientMedicines(uint _patientId) public view returns (uint[]
memory) {
require(patients[_patientId].createdBy == msg.sender, "You are not authorized to view this patient's medicines");
return patientMedicines[_patientId];}
}