/AirBnB_clone

Static website: https://danicelistobon.github.io/AirBnB_clone_static/

Primary LanguageHTML

🚩 0x00. AirBnB clone - The console

Background Context

Welcome to the AirBnB clone project! (The Holberton B&B)

First step: Write a command interpreter to manage your AirBnB objects. This is the first step towards building your first full web application: the AirBnB clone. This first step is very important because you will use what you build during this project with all other following projects: HTML/CSS templating, database storage, API, front-end integration

Each task is linked and will help you to:

  • Put in place a parent class (called BaseModel) to take care of the initialization, serialization and deserialization of your future instances
  • Create a simple flow of serialization/deserialization: Instance <-> Dictionary <-> JSON string <-> file
  • Create all classes used for AirBnB (User, State, City, Place) that inherit from BaseModel
  • Create the first abstracted storage engine of the project: File storage
  • Create all unittests to validate all our classes and storage engine

Whats a command interpreter?

Do you remember the Shell? Its exactly the same but limited to a specific use-case. In our case, we want to be able to manage the objects of our project:

  • Create a new object (ex: a new User or a new Place)
  • Retrieve an object from a file, a database etc
  • Do operations on objects (count, compute stats, etc)
  • Update attributes of an object
  • Destroy an object

Commands

These are some of the commands implemented in our console (HBNBCommand):

Command Description
all Prints all string representation of all instances based or not on the class name
create Creates a new instance of class name, saves it (to the JSON file) and prints the id
destroy Deletes an instance based on the class name and id (save the change into the JSON file)
help List available commands with "help" or detailed help with "help cmd"
quit Quit command to exit the program
show Prints the string representation of an instance based on the class name and id
update Updates an instance based on the class name and id by adding or updating attribute (save the change into the JSON file)
EOF EOF command to exit the program

Files

File Description
console.py Contains the entry point of the command interpreter
amenity.py Amenity class that inherit from BaseModel
base_model.py BaseModel class that defines all common attributes/methods for other classes
city.py City class that inherit from BaseModel
place.py Place class that inherit from BaseModel
review.py Review class that inherit from BaseModel
state.py State class that inherit from BaseModel
user.py User class that inherit from BaseModel
file_storage.py FileStorage class that serializes instances to a JSON file and deserializes JSON file to instances
test_amenity.py Unit Tests for the Amenity class
test_base_model.py Unit Tests for the BaseModel class
test_city.py Unit Tests for the City class
test_place.py Unit Tests for the Place class
test_review.py Unit Tests for the Review class
test_state.py Unit Tests for the State class
test_user.py Unit Tests for the User class
test_file_storage.py Unit Tests for the FileStorage class
AUTHORS Lists all individuals having contributed content to the repository
README.md README file with project description, installation, use and examples

Installation

Clone this repository in your terminal:

$ git clone https://github.com/danicelistobon/AirBnB_clone.git
$ cd AirBnB_clone

Execution

This code should work like this in interactive mode:

$ ./console.py
(hbnb) help

Documented commands (type help <topic>):
========================================
EOF  help  quit

(hbnb) 
(hbnb) 
(hbnb) quit
$

But also in non-interactive mode:

$ echo "help" | ./console.py
(hbnb)

Documented commands (type help <topic>):
========================================
EOF  help  quit
(hbnb) 
$
$ cat test_help
help
$
$ cat test_help | ./console.py
(hbnb)

Documented commands (type help <topic>):
========================================
EOF  help  quit
(hbnb) 
$

Examples

In the AirBnB_clone directory create the file named "test_base_model.py" with the following code:

#!/usr/bin/python3
from models.base_model import BaseModel

my_model = BaseModel()
my_model.name = "Holberton"
my_model.my_number = 89
print(my_model)
my_model.save()
print(my_model)
my_model_json = my_model.to_dict()
print(my_model_json)
print("JSON of my_model:")
for key in my_model_json.keys():
    print("\t{}: ({}) - {}".format(key, type(my_model_json[key]), my_model_json[key]))

Grant execution permissions to the file as follows:

$ chmod u+x test_base_model.py

Then execute the file as follows:

$ ./test_base_model.py

The output should be as follows:

[BaseModel] (b6a6e15c-c67d-4312-9a75-9d084935e579) {'my_number': 89, 'name': 'Holberton', 'updated_at': datetime.datetime(2017, 9, 28, 21, 5, 54, 119434), 'id': 'b6a6e15c-c67d-4312-9a75-9d084935e579', 'created_at': datetime.datetime(2017, 9, 28, 21, 5, 54, 119427)}
[BaseModel] (b6a6e15c-c67d-4312-9a75-9d084935e579) {'my_number': 89, 'name': 'Holberton', 'updated_at': datetime.datetime(2017, 9, 28, 21, 5, 54, 119572), 'id': 'b6a6e15c-c67d-4312-9a75-9d084935e579', 'created_at': datetime.datetime(2017, 9, 28, 21, 5, 54, 119427)}
{'my_number': 89, 'name': 'Holberton', '__class__': 'BaseModel', 'updated_at': '2017-09-28T21:05:54.119572', 'id': 'b6a6e15c-c67d-4312-9a75-9d084935e579', 'created_at': '2017-09-28T21:05:54.119427'}
JSON of my_model:
    my_number: (<class 'int'>) - 89
    name: (<class 'str'>) - Holberton
    __class__: (<class 'str'>) - BaseModel
    updated_at: (<class 'str'>) - 2017-09-28T21:05:54.119572
    id: (<class 'str'>) - b6a6e15c-c67d-4312-9a75-9d084935e579
    created_at: (<class 'str'>) - 2017-09-28T21:05:54.119427

Another example using the "console.py" file:

$ ./console.py
(hbnb) all MyModel
** class doesn't exist **
(hbnb) show BaseModel
** instance id missing **
(hbnb) show BaseModel Holberton
** no instance found **
(hbnb) create BaseModel
49faff9a-6318-451f-87b6-910505c55907
(hbnb) all BaseModel
["[BaseModel] (49faff9a-6318-451f-87b6-910505c55907) {'created_at': datetime.datetime(2017, 10, 2, 3, 10, 25, 903293), 'id': '49faff9a-6318-451f-87b6-910505c55907', 'updated_at': datetime.datetime(2017, 10, 2, 3, 10, 25, 903300)}"]
(hbnb) show BaseModel 49faff9a-6318-451f-87b6-910505c55907
[BaseModel] (49faff9a-6318-451f-87b6-910505c55907) {'created_at': datetime.datetime(2017, 10, 2, 3, 10, 25, 903293), 'id': '49faff9a-6318-451f-87b6-910505c55907', 'updated_at': datetime.datetime(2017, 10, 2, 3, 10, 25, 903300)}
(hbnb) destroy
** class name missing **
(hbnb) update BaseModel 49faff9a-6318-451f-87b6-910505c55907 first_name "Betty"
(hbnb) show BaseModel 49faff9a-6318-451f-87b6-910505c55907
[BaseModel] (49faff9a-6318-451f-87b6-910505c55907) {'first_name': 'Betty', 'id': '49faff9a-6318-451f-87b6-910505c55907', 'created_at': datetime.datetime(2017, 10, 2, 3, 10, 25, 903293), 'updated_at': datetime.datetime(2017, 10, 2, 3, 11, 3, 49401)}
(hbnb) create BaseModel
2dd6ef5c-467c-4f82-9521-a772ea7d84e9
(hbnb) all BaseModel
["[BaseModel] (2dd6ef5c-467c-4f82-9521-a772ea7d84e9) {'id': '2dd6ef5c-467c-4f82-9521-a772ea7d84e9', 'created_at': datetime.datetime(2017, 10, 2, 3, 11, 23, 639717), 'updated_at': datetime.datetime(2017, 10, 2, 3, 11, 23, 639724)}", "[BaseModel] (49faff9a-6318-451f-87b6-910505c55907) {'first_name': 'Betty', 'id': '49faff9a-6318-451f-87b6-910505c55907', 'created_at': datetime.datetime(2017, 10, 2, 3, 10, 25, 903293), 'updated_at': datetime.datetime(2017, 10, 2, 3, 11, 3, 49401)}"]
(hbnb) destroy BaseModel 49faff9a-6318-451f-87b6-910505c55907
(hbnb) show BaseModel 49faff9a-6318-451f-87b6-910505c55907
** no instance found **
(hbnb) 

Authors

Maribel Serna Arbelaez | Daniel Celis Tobon