|
| 1 | +import datetime |
| 2 | +from dateutil import parser |
| 3 | + |
1 | 4 | from infrastructure.switchlang import switch |
2 | 5 | import program_hosts as hosts |
| 6 | +import servies.data_service as svc |
3 | 7 | from program_hosts import success_msg, error_msg |
4 | 8 | import infrastructure.state as state |
5 | 9 |
|
@@ -54,36 +58,114 @@ def show_commands(): |
54 | 58 |
|
55 | 59 | def add_a_snake(): |
56 | 60 | print(' ****************** Add a snake **************** ') |
57 | | - # TODO: Require an account |
58 | | - # TODO: Get snake info from user |
59 | | - # TODO: Create the snake in the DB. |
| 61 | + if not state.active_account: |
| 62 | + error_msg("You must log in first to add a snake") |
| 63 | + return |
| 64 | + |
| 65 | + name = input("What is your snake's name? ") |
| 66 | + if not name: |
| 67 | + error_msg('cancelled') |
| 68 | + return |
| 69 | + |
| 70 | + length = float(input('How long is your snake (in meters)? ')) |
| 71 | + species = input("Species? ") |
| 72 | + is_venomous = input("Is your snake venomous [y]es, [n]o? ").lower().startswith('y') |
60 | 73 |
|
61 | | - print(" -------- NOT IMPLEMENTED -------- ") |
| 74 | + snake = svc.add_snake(state.active_account, name, length, species, is_venomous) |
| 75 | + state.reload_account() |
| 76 | + success_msg('Created {} with id {}'.format(snake.name, snake.id)) |
62 | 77 |
|
63 | 78 |
|
64 | 79 | def view_your_snakes(): |
65 | 80 | print(' ****************** Your snakes **************** ') |
| 81 | + if not state.active_account: |
| 82 | + error_msg("You must log in first to view your snakes") |
| 83 | + return |
66 | 84 |
|
67 | | - # TODO: Require an account |
68 | | - # TODO: Get snakes from DB, show details list |
69 | | - |
70 | | - print(" -------- NOT IMPLEMENTED -------- ") |
| 85 | + snakes = svc.get_snakes_for_user(state.active_account.id) |
| 86 | + print("You have {} snakes.".format(len(snakes))) |
| 87 | + for s in snakes: |
| 88 | + print(" * {} is a {} that is {}m long and is {}venomous.".format( |
| 89 | + s.name, |
| 90 | + s.species, |
| 91 | + s.length, |
| 92 | + '' if s.is_venomous else 'not ' |
| 93 | + )) |
71 | 94 |
|
72 | 95 |
|
73 | 96 | def book_a_cage(): |
74 | 97 | print(' ****************** Book a cage **************** ') |
75 | | - # TODO: Require an account |
76 | | - # TODO: Verify they have a snake |
77 | | - # TODO: Get dates and select snake |
78 | | - # TODO: Find cages available across date range |
79 | | - # TODO: Let user select cage to book. |
| 98 | + if not state.active_account: |
| 99 | + error_msg("You must log in first to book a cage") |
| 100 | + return |
| 101 | + |
| 102 | + snakes = svc.get_snakes_for_user(state.active_account.id) |
| 103 | + if not snakes: |
| 104 | + error_msg('You must first [a]dd a snake before you can book a cage.') |
| 105 | + return |
| 106 | + |
| 107 | + print("Let's start by finding available cages.") |
| 108 | + start_text = input("Check-in date [yyyy-mm-dd]: ") |
| 109 | + if not start_text: |
| 110 | + error_msg('cancelled') |
| 111 | + return |
| 112 | + |
| 113 | + checkin = parser.parse( |
| 114 | + start_text |
| 115 | + ) |
| 116 | + checkout = parser.parse( |
| 117 | + input("Check-out date [yyyy-mm-dd]: ") |
| 118 | + ) |
| 119 | + if checkin >= checkout: |
| 120 | + error_msg('Check in must be before check out') |
| 121 | + return |
| 122 | + |
| 123 | + print() |
| 124 | + for idx, s in enumerate(snakes): |
| 125 | + print('{}. {} (length: {}, venomous: {})'.format( |
| 126 | + idx + 1, |
| 127 | + s.name, |
| 128 | + s.length, |
| 129 | + 'yes' if s.is_venomous else 'no' |
| 130 | + )) |
80 | 131 |
|
81 | | - print(" -------- NOT IMPLEMENTED -------- ") |
| 132 | + snake = snakes[int(input('Which snake do you want to book (number)')) - 1] |
| 133 | + |
| 134 | + cages = svc.get_available_cages(checkin, checkout, snake) |
| 135 | + |
| 136 | + print("There are {} cages available in that time.".format(len(cages))) |
| 137 | + for idx, c in enumerate(cages): |
| 138 | + print(" {}. {} with {}m carpeted: {}, has toys: {}.".format( |
| 139 | + idx + 1, |
| 140 | + c.name, |
| 141 | + c.square_meters, |
| 142 | + 'yes' if c.is_carpeted else 'no', |
| 143 | + 'yes' if c.has_toys else 'no')) |
| 144 | + |
| 145 | + if not cages: |
| 146 | + error_msg("Sorry, no cages are available for that date.") |
| 147 | + return |
| 148 | + |
| 149 | + cage = cages[int(input('Which cage do you want to book (number)')) - 1] |
| 150 | + svc.book_cage(state.active_account, snake, cage, checkin, checkout) |
| 151 | + |
| 152 | + success_msg('Successfully booked {} for {} at ${}/night.'.format(cage.name, snake.name, cage.price)) |
82 | 153 |
|
83 | 154 |
|
84 | 155 | def view_bookings(): |
85 | 156 | print(' ****************** Your bookings **************** ') |
86 | | - # TODO: Require an account |
87 | | - # TODO: List booking info along with snake info |
88 | | - |
89 | | - print(" -------- NOT IMPLEMENTED -------- ") |
| 157 | + if not state.active_account: |
| 158 | + error_msg("You must log in first to register a cage") |
| 159 | + return |
| 160 | + |
| 161 | + snakes = {s.id: s for s in svc.get_snakes_for_user(state.active_account.id)} |
| 162 | + bookings = svc.get_bookings_for_user(state.active_account.email) |
| 163 | + |
| 164 | + print("You have {} bookings.".format(len(bookings))) |
| 165 | + for b in bookings: |
| 166 | + print(' * Snake: {} is booked at {} from {} for {} days.'.format( |
| 167 | + snakes.get(b.guest_snake_id).name, |
| 168 | + b.cage.name, |
| 169 | + datetime.date(b.check_in_date.year, b.check_in_date.month, b.check_in_date.day), |
| 170 | + (b.check_out_date - b.check_in_date).days |
| 171 | + )) |
0 commit comments