georgegathu / AirBnB_clone_v2

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Description 🏠

Airbnb clone is a complete web application, integrating database storage, a back-end API, and front-end interfacing in a clone of AirBnB.

The project currently only implements the back-end console.

Classes πŸ†‘

AirBnB utilizes the following classes:

BaseModel FileStorage User State City Amenity Place Review
PUBLIC INSTANCE ATTRIBUTES id
created_at
updated_at
Inherits from BaseModel Inherits from BaseModel Inherits from BaseModel Inherits from BaseModel Inherits from BaseModel Inherits from BaseModel
PUBLIC INSTANCE METHODS save
to_dict
all
new
save
reload
"" "" "" "" "" ""
PUBLIC CLASS ATTRIBUTES email
password
first_name
last_name
name state_id
name
name city_id
user_id
name
description
number_rooms
number_bathrooms
max_guest
price_by_night
latitude
longitude
amenity_ids
place_id
user_id
text
PRIVATE CLASS ATTRIBUTES file_path
objects

Storage πŸ›„

The above classes are handled by the abstracted storage engine defined in the FileStorage class.

Every time the backend is initialized, AirBnB instantiates an instance of FileStorage called storage. The storage object is loaded/re-loaded from any class instances stored in the JSON file file.json. As class instances are created, updated, or deleted, the storage object is used to register corresponding changes in the file.json.

Console πŸ’»

The console is a command line interpreter that permits management of the backend of AirBnB. It can be used to handle and manipulate all classes utilized by the application (achieved by calls on the storage object defined above).

Using the Console

The AirBnB console can be run both interactively and non-interactively. To run the console in non-interactive mode, pipe any command(s) into an execution of the file console.py at the command line.

$ echo "help" | ./console.py
(hbnb) 
Documented commands (type help <topic>):
========================================
EOF  all  count  create  destroy  help  quit  show  update

(hbnb) 
$

Alternatively, to use the AirBnB console in interactive mode, run the file console.py by itself:

$ ./console.py

While running in interactive mode, the console displays a prompt for input:

$ ./console.py
(hbnb) 

To quit the console, enter the command quit, or input an EOF signal (ctrl-D).

$ ./console.py
(hbnb) quit
$
$ ./console.py
(hbnb) EOF
$

Console Commands

The AirBnB console supports the following commands:

  • create
    • Usage: create <class>

Creates a new instance of a given class. The class' ID is printed and the instance is saved to the file file.json.

  • show
    • Usage: show <class> <id> or <class>.show(<id>)

Prints the string representation of a class instance based on a given id.

$ ./console.py
(hbnb) create User
(hbnb)
(hbnb) show User uid		
(hbnb) 
(hbnb) User.show(uid)
(hbnb) 
  • destroy
    • Usage: destroy <class> <id> or <class>.destroy(<id>)

Deletes a class instance based on a given id. The storage file file.json is updated accordingly.

$ ./console.py
(hbnb) create State
(hbnb) create Place
(hbnb)
(hbnb) destroy State uid
(hbnb) Place.destroy(uid)
(hbnb) quit
$ cat file.json ; echo ""
{}
  • all
    • Usage: all or all <class> or <class>.all()

Prints the string representations of all instances of a given class. If no class name is provided, the command prints all instances of every class.

$ ./console.py
(hbnb) create BaseModel
(hbnb) create BaseModel
(hbnb) create User
(hbnb) create User
(hbnb)
(hbnb) all BaseModel
(hbnb)
(hbnb) User.all()
(hbnb) 
(hbnb) all
  • count
    • Usage: count <class> or <class>.count()

Retrieves the number of instances of a given class.

$ ./console.py
(hbnb) create Place
(hbnb) create Place
(hbnb) create City
(hbnb) 
(hbnb) count Place
2
(hbnb) city.count()
1
(hbnb) 
  • update
    • Usage: update <class> <id> <attribute name> "<attribute value>" or <class>.update(<id>, <attribute name>, <attribute value>) or <class>.update( <id>, <attribute dictionary>).

Updates a class instance based on a given id with a given key/value attribute pair or dictionary of attribute pairs. If update is called with a single key/value attribute pair, only "simple" attributes can be updated (ie. not id, created_at, and updated_at). However, any attribute can be updated by providing a dictionary.

$ ./console.py
(hbnb) create User
(hbnb)
(hbnb) update User id first_name "name"
(hbnb) show User uid
(hbnb)
(hbnb) User.update(uid), address, "address")
(hbnb) User.show(uid)
(hbnb)
(hbnb) User.update(uid, {'email': 'email', 'last_name': 'last_name'})
(hbnb) 

Testing πŸ“

Unittests for the Airbnb_clone project are defined in the tests folder. To run the entire test suite simultaneously, execute the following command:

$ python3 unittest -m discover tests

Alternatively, you can specify a single test file to run at a time:

$ python3 unittest -m tests/test_console.py

Authors βœ’οΈi

About


Languages

Language:Python 97.3%Language:HTML 1.3%Language:CSS 0.8%Language:PowerShell 0.4%Language:Shell 0.2%Language:Puppet 0.0%