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