forked from mztacat/Base
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Structs
78 lines (70 loc) · 2.74 KB
/
Structs
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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;
/**
* @title GarageManager
* @dev Contract to manage a garage of cars for each user
*/
contract GarageManager {
// Mapping to store the garage of cars for each user
mapping(address => Car[]) private garages;
// Struct to represent a car
struct Car {
string make; // Make of the car
string model; // Model of the car
string color; // Color of the car
uint numberOfDoors; // Number of doors of the car
}
// Custom error for handling invalid car index
error BadCarIndex(uint256 index);
/**
* @dev Adds a new car to the caller's garage
* @param _make The make of the car
* @param _model The model of the car
* @param _color The color of the car
* @param _numberOfDoors The number of doors of the car
*/
function addCar(string memory _make, string memory _model, string memory _color, uint _numberOfDoors) external {
// Push a new car struct with the provided details to the caller's garage
garages[msg.sender].push(Car(_make, _model, _color, _numberOfDoors));
}
/**
* @dev Retrieves the caller's array of cars
* @return An array of `Car` structs
*/
function getMyCars() external view returns (Car[] memory) {
// Return the array of cars stored in the caller's garage
return garages[msg.sender];
}
/**
* @dev Retrieves a specific user's array of cars
* @param _user The address of the user
* @return An array of `Car` structs
*/
function getUserCars(address _user) external view returns (Car[] memory) {
// Return the array of cars stored in the garage of the specified user
return garages[_user];
}
/**
* @dev Updates a specific car in the caller's garage
* @param _index The index of the car in the garage array
* @param _make The new make of the car
* @param _model The new model of the car
* @param _color The new color of the car
* @param _numberOfDoors The new number of doors of the car
*/
function updateCar(uint256 _index, string memory _make, string memory _model, string memory _color, uint _numberOfDoors) external {
// Check if the provided index is valid
if (_index >= garages[msg.sender].length) {
revert BadCarIndex({index: _index}); // Revert with custom error if the index is invalid
}
// Update the specified car with the new details
garages[msg.sender][_index] = Car(_make, _model, _color, _numberOfDoors);
}
/**
* @dev Deletes all cars in the caller's garage
*/
function resetMyGarage() external {
// Delete all cars from the caller's garage
delete garages[msg.sender];
}
}