|
1 | | -use crate::interfaces::contract::RentACarContractTrait; |
2 | | -use soroban_sdk::{contract, contractimpl, symbol_short, Address, Env, Symbol}; |
| 1 | +use crate::{ |
| 2 | + interfaces::contract::RentACarContractTrait, |
| 3 | + storage::{ |
| 4 | + admin::{read_admin, write_admin}, |
| 5 | + car::{read_car, remove_car, write_car}, |
| 6 | + rental::write_rental, |
| 7 | + structs::car::Car, |
| 8 | + structs::rental::Rental, |
| 9 | + token::write_token, |
| 10 | + types::car_status::CarStatus, |
| 11 | + }, |
| 12 | +}; |
| 13 | + |
| 14 | +use soroban_sdk::{contract, contractimpl, Address, Env}; |
3 | 15 |
|
4 | 16 | #[contract] |
5 | 17 | pub struct RentACarContract; |
6 | 18 |
|
7 | | -pub const ADMIN_KEY: &Symbol = &symbol_short!("ADMIN"); |
8 | | -pub const TOKEN_KEY: &Symbol = &symbol_short!("TOKEN"); |
9 | | - |
10 | 19 | #[contractimpl] |
11 | 20 | impl RentACarContractTrait for RentACarContract { |
12 | 21 | fn __constructor(env: &Env, admin: Address, token: Address) { |
13 | | - env.storage().instance().set(ADMIN_KEY, &admin); |
14 | | - env.storage().instance().set(TOKEN_KEY, &token); |
| 22 | + write_admin(env, &admin); |
| 23 | + write_token(env, &token); |
15 | 24 | } |
16 | 25 |
|
17 | 26 | fn get_admin(env: &Env) -> Address { |
18 | | - env.storage() |
19 | | - .instance() |
20 | | - .get::<Symbol, Address>(ADMIN_KEY) |
21 | | - .unwrap() |
| 27 | + read_admin(env) |
| 28 | + } |
| 29 | + |
| 30 | + fn add_car(env: &Env, owner: Address, price_per_day: i128) { |
| 31 | + let car = Car { |
| 32 | + price_per_day, |
| 33 | + car_status: CarStatus::Available, |
| 34 | + }; |
| 35 | + |
| 36 | + write_car(env, &owner, &car); |
| 37 | + } |
| 38 | + |
| 39 | + fn get_car_status(env: &Env, owner: Address) -> CarStatus { |
| 40 | + let car = read_car(env, &owner); |
| 41 | + |
| 42 | + car.car_status |
| 43 | + } |
| 44 | + |
| 45 | + fn rental(env: &Env, renter: Address, owner: Address, total_days_to_rent: u32, amount: i128) { |
| 46 | + let mut car = read_car(env, &owner); |
| 47 | + |
| 48 | + car.car_status = CarStatus::Rented; |
| 49 | + |
| 50 | + let rental = Rental { |
| 51 | + total_days_to_rent, |
| 52 | + amount, |
| 53 | + }; |
| 54 | + |
| 55 | + write_car(env, &owner, &car); |
| 56 | + write_rental(env, &renter, &owner, &rental); |
| 57 | + } |
| 58 | + |
| 59 | + fn remove_car(env: &Env, owner: Address) { |
| 60 | + remove_car(env, &owner); |
22 | 61 | } |
23 | 62 | } |
0 commit comments