-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathShelterDB.sol
194 lines (172 loc) · 5.8 KB
/
ShelterDB.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
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.18;
contract ShelterDB {
/**
* Shelters often struggle to accommodate the constant influx of new animals.
* This overcrowding leads to limited space for each animal,
* which can result in health issues and a decreased quality of life.
* This smart contract aims to support shelters by providing a decentralized database
* that can track the population of animals in shelters.
* This can help shelters better allocate resources and make informed decisions about
* admitting new animals.
*/
uint256 public totalPets;
uint256 public totalShelterId; // every time a new shelter is added, this number increases
enum Country {
USA,
India,
Brazil
}
event ShelterInfo(
uint256 shelterId,
Country indexed shelterCountry,
bool indexed atCapacity,
bool indexed isDeleted
);
struct Pet {
string name;
string petType; // cat, dog, bird, other
string size; // small, medium, large
string sex; // female, male
string age; // baby, young, adult, senior
uint256 petId;
}
struct Shelter {
string shelterName;
Country shelterCountry;
string shelterCity;
string shelterZipCode;
bool atCapacity;
address shelterOwner;
bool isDeleted;
}
mapping(uint256 => Pet[]) ShelterPets;
mapping(uint256 => mapping(uint256 => bool)) PetIdExists;
mapping(uint256 => Shelter) public ShelterListings;
error ShelterNotAvaliable();
error SheleterDeleted();
error NotShelterOwner();
error PetIdAlreadyTaken();
modifier onlyExistingShelter(uint256 _shelterId) {
if (_shelterId > totalShelterId || _shelterId == 0) {
revert ShelterNotAvaliable();
}
_;
}
modifier onlyNotDeletedShelter(uint256 _shelterId) {
if (ShelterListings[_shelterId].isDeleted == true) {
revert SheleterDeleted();
}
_;
}
modifier onlyShelterOwner(uint256 _shelterId) {
if (ShelterListings[_shelterId].shelterOwner != msg.sender) {
revert NotShelterOwner();
}
_;
}
modifier onlyAvailablePetId(uint256 _shelterId, uint256 _petId) {
if (PetIdExists[_shelterId][_petId]) {
revert PetIdAlreadyTaken();
}
_;
}
constructor() {}
function addShelter(
string memory _shelterName,
Country _shelterCountry,
string memory _shelterCity,
string memory _shelterZipCode
) public {
totalShelterId++;
// update Shelter struct
ShelterListings[totalShelterId] = Shelter({
shelterName: _shelterName,
shelterCountry: _shelterCountry,
shelterCity: _shelterCity,
shelterZipCode: _shelterZipCode,
atCapacity: false,
shelterOwner: msg.sender,
isDeleted: false
});
emit ShelterInfo(totalShelterId, _shelterCountry, false, false);
}
function updateShelter(
uint256 _shelterId,
string memory _shelterName,
Country _shelterCountry,
string memory _shelterCity,
string memory _shelterZipCode,
bool _newCapacity,
bool _isDeleted
)
public
onlyExistingShelter(_shelterId)
onlyNotDeletedShelter(_shelterId)
onlyShelterOwner(_shelterId)
{
// Use a reference to the existing Shelter struct stored in the ShelterListings[_shelterId] mapping entry.
// By using the storage keyword, the function updates the fields of the existing struct in place.
// This approach is often more efficient in terms of gas costs because it modifies the existing storage slot
// without replacing the entire struct.
Shelter storage shelter = ShelterListings[_shelterId];
shelter.shelterName = _shelterName;
shelter.shelterCountry = _shelterCountry;
shelter.shelterCity = _shelterCity;
shelter.shelterZipCode = _shelterZipCode;
shelter.atCapacity = _newCapacity;
shelter.shelterOwner = msg.sender;
shelter.isDeleted = _isDeleted;
emit ShelterInfo(_shelterId, _shelterCountry, _newCapacity, _isDeleted);
}
function getShelterListing(
uint256 _shelterId
)
public
view
onlyExistingShelter(_shelterId)
onlyNotDeletedShelter(_shelterId)
returns (Shelter memory)
{
return ShelterListings[_shelterId];
}
function addPet(
uint256 _shelterId,
string memory _name,
string memory _petType, // cat, dog, bird, other
string memory _size, // small, medium, large
string memory _sex, // female, male
string memory _age, // baby, young, adult, senior
uint256 _petId
)
public
onlyExistingShelter(_shelterId)
onlyNotDeletedShelter(_shelterId)
onlyShelterOwner(_shelterId)
onlyAvailablePetId(_shelterId, _petId)
{
totalPets++;
PetIdExists[_shelterId][_petId] = true;
ShelterPets[_shelterId].push(
Pet({
name: _name,
petType: _petType,
size: _size,
sex: _sex,
age: _age,
petId: _petId
})
);
}
function getShelterPets(
uint256 _shelterId
)
public
view
onlyExistingShelter(_shelterId)
onlyNotDeletedShelter(_shelterId)
returns (Pet[] memory)
{
return ShelterPets[_shelterId];
}
}