forked from justinmajetich/AirBnB_clone
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ca5d84c
commit dda8aac
Showing
6 changed files
with
115 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,12 @@ | ||
#!/usr/bin/python3 | ||
"""This module instantiates an object of class FileStorage""" | ||
from models.engine.file_storage import FileStorage | ||
"""This module instantiates the storage medium""" | ||
import os | ||
|
||
if os.environ.get('HBNB_TYPE_STORAGE') == 'db': | ||
from models.engine.db_storage import DBStorage | ||
storage = DBStorage() | ||
else: | ||
from models.engine.file_storage import FileStorage | ||
storage = FileStorage() | ||
|
||
storage = FileStorage() | ||
storage.reload() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,12 @@ | ||
#!/usr/bin/python3 | ||
""" City Module for HBNB project """ | ||
from models.base_model import BaseModel | ||
from models.base_model import BaseModel, Base | ||
from sqlalchemy import Column, String, ForeignKey | ||
from sqlalchemy.orm import relationship | ||
|
||
|
||
class City(BaseModel): | ||
class City(BaseModel, Base): | ||
""" The city class, contains state ID and name """ | ||
state_id = "" | ||
name = "" | ||
__tablename__ = 'cities' | ||
name = Column(String(128), nullable=False) | ||
state_id = Column(String(60), ForeignKey('states.id'), nullable=False) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
#!/usr/bin/env python3 | ||
"""Defines a new engine that uses SQLAlchemy""" | ||
import os | ||
from sqlalchemy import create_engine | ||
from sqlalchemy.orm import sessionmaker, scoped_session | ||
|
||
|
||
class DBStorage: | ||
"""DBStorage Engine running on SQLAlchemy""" | ||
|
||
__engine = None | ||
__session = None | ||
|
||
def __init__(self): | ||
"""class constructor for DBStorage""" | ||
|
||
credentials = ['HBNB_MYSQL_USER', 'HBNB_MYSQL_PWD', | ||
'HBNB_MYSQL_HOST', 'HBNB_MYSQL_DB'] | ||
env = [os.environ.get(c) for c in credentials] | ||
e = 'mysql+mysqldb://{}:{}@{}/{}'.format(*env[:]) | ||
self.__engine = create_engine(e, pool_pre_ping=True) | ||
Session = sessionmaker(bind=self.__engine) | ||
self.__session = Session() | ||
|
||
# drop all tables if the environment variable | ||
# HBNB_ENV is equal to test | ||
if os.environ.get('HBNB_ENV') == 'test': | ||
from models.base_model import Base | ||
Base.metadata.drop_all(self.__engine) | ||
|
||
|
||
def all(self, cls=None): | ||
"""Queries the current database session (self.__session) | ||
for all objects depending of the class name (argument cls)""" | ||
from models.user import User | ||
from models.state import State | ||
from models.city import City | ||
from models.amenity import Amenity | ||
from models.place import Place | ||
from models.review import Review | ||
|
||
class_list = [User, State, City, Amenity, Place, Review] | ||
objs = {} | ||
if cls is not None: | ||
for obj in self.__session.query(cls).all(): | ||
objs.update({obj.to_dict()['__class__'] + '.' + obj.id: obj}) | ||
else: | ||
for class_item in class_list: | ||
for obj in self.__session.query(class_item).all(): | ||
objs.update({obj.to_dict()['__class__'] + '.' + obj.id: obj}) | ||
return objs | ||
|
||
def new(self, obj): | ||
"""Add obj to the current database session""" | ||
self.__session.add(obj) | ||
|
||
def save(self): | ||
"""Commit all changes of the current database session""" | ||
self.__session.commit() | ||
|
||
def delete(self, obj=None): | ||
"""Delete from the current database session obj if not None""" | ||
if obj is not None: | ||
self.__session.delete(obj) | ||
|
||
def reload(self): | ||
"""Create all tables in the database | ||
""" | ||
from models.user import User | ||
from models.state import State | ||
from models.city import City | ||
from models.amenity import Amenity | ||
from models.place import Place | ||
from models.review import Review | ||
from models.base_model import Base | ||
|
||
Base.metadata.create_all(self.__engine) | ||
Session = sessionmaker(bind=self.__engine, expire_on_commit=False) | ||
self.__session = scoped_session(Session) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,17 @@ | ||
#!/usr/bin/python3 | ||
""" State Module for HBNB project """ | ||
from models.base_model import BaseModel | ||
from models.base_model import BaseModel, Base | ||
from sqlalchemy import Column, String | ||
from sqlalchemy.orm import relationship | ||
|
||
|
||
class State(BaseModel): | ||
class State(BaseModel, Base): | ||
""" State class """ | ||
name = "" | ||
__tablename__ = 'states' | ||
name = Column(String(128), nullable=False) | ||
cities = relationship('City', cascade='all, delete', backref='state') | ||
|
||
@property | ||
def cities(self): | ||
"""A getter for the attribute cities""" | ||
return [city for city in self.cities if city.state_id == self.id] |