Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lizz branch #16

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

from helpers import (
exit_program,
helper_1
helper_1,
list_teams
)


Expand All @@ -14,6 +15,8 @@ def main():
exit_program()
elif choice == "1":
helper_1()
elif choice == "2":
list_teams()
else:
print("Invalid choice")

Expand All @@ -22,6 +25,7 @@ def menu():
print("Please select an option:")
print("0. Exit the program")
print("1. Some useful function")
print("2. List all the teams")


if __name__ == "__main__":
Expand Down
2 changes: 2 additions & 0 deletions lib/debug.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

from models.__init__ import CONN, CURSOR
import ipdb
from models.team import Team



ipdb.set_trace()
9 changes: 9 additions & 0 deletions lib/helpers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# lib/helpers.py
from models.team import Team

def helper_1():
print("Performing useful function#1.")
Expand All @@ -7,3 +8,11 @@ def helper_1():
def exit_program():
print("Goodbye!")
exit()

def list_teams():
teams = Team.get_all()
if len(teams) < 1:
print("\n No teams in database.")
print("ID", "\t", '{0: <25}'.format("NAME"), "DIVISION")
for team in teams:
print(team[0], "\t", '{0: <25}'.format(team[1]), team[2])
2 changes: 1 addition & 1 deletion lib/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import sqlite3

CONN = sqlite3.connect('company.db')
CONN = sqlite3.connect('nba.db')
CURSOR = CONN.cursor()
163 changes: 163 additions & 0 deletions lib/models/team.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
# lib/models/team.py
from models.__init__ import CURSOR, CONN


class Team:

def __init__(self, name, division, id=None):
self.id = id
self.name = name
self.division = division

def __repr__(self):
return f"<Team {self.id}:{self.name}, {self.division}>"

@property
def name(self):
return self._name

@name.setter
def name(self, name):
if isinstance(name, str) and len(name):
self._name = name
else:
raise ValueError(
"Name must be a non-empty string"
)

@property
def division(self):
return self._division

@division.setter
def division(self, division):
if isinstance(division, str) and len(division):
self._division = division
else:
raise ValueError(
"Division must be a non-empty string"
)

# Create a class method to create a table which will create a blank table
# Create rows and append the seed data to the rows
# Users should be able to type in 2 to the CLI and return all teams

@classmethod
def create_table(cls):
sql = """
CREATE TABLE IF NOT EXISTS teams (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT,
division TEXT
) """
CURSOR.execute(sql)
CONN.commit()

@classmethod
def drop_table(cls):
""" Drop the table that persists Team instances """
sql = """
DROP TABLE IF EXISTS teams;
"""
CURSOR.execute(sql)
CONN.commit()

def save(self):
""" Insert a new row with the name and division values of the current Team instance."""
sql = """
INSERT INTO teams (name, division)
VALUES (?, ?)
"""

CURSOR.execute(sql, (self.name, self.division))
CONN.commit()

# @classmethod
# def create(cls, name, division):
# """ Initialize a new Team instance and save the object to the database """
# team = cls(name, division)
# team.save()
# return team

# def update(self):
# """Update the table row corresponding to the current Team instance."""
# sql = """
# UPDATE teams
# SET name = ?, location = ?
# WHERE id = ?
# """
# CURSOR.execute(sql, (self.name, self.division, self.id))
# CONN.commit()

# def delete(self):
# """Delete the table row corresponding to the current Team instance,
# delete the dictionary entry, and reassign id attribute"""

# sql = """
# DELETE FROM teams
# WHERE id = ?
# """

# CURSOR.execute(sql, (self.id,))
# CONN.commit()

# # Delete the dictionary entry using id as the key
# del type(self).all[self.id]

# # Set the id to None
# self.id = None

# @classmethod
# def instance_from_db(cls, row):
# """Return a Team object having the attribute values from the table row."""
# one_team = Team(row[1], row[2])
# one_team.id = row[0]
# return one_team

@classmethod
def get_all(cls):
"""Return a list containing a Team object per row in the table"""
sql = """
SELECT *
FROM teams
"""
rows = CURSOR.execute(sql).fetchall()
return rows

# @classmethod
# def find_by_id(cls, id):
# """Return a Team object corresponding to the table row matching the specified primary key"""
# sql = """
# SELECT *
# FROM teams
# WHERE id = ?
# """

# row = CURSOR.execute(sql, (id,)).fetchone()
# return cls.instance_from_db(row) if row else None

# @classmethod
# def find_by_name(cls, name):
# """Return a Team object corresponding to first table row matching specified name"""
# sql = """
# SELECT *
# FROM teams
# WHERE name is ?
# """

# row = CURSOR.execute(sql, (name,)).fetchone()
# return cls.instance_from_db(row) if row else None

# def employees(self):
# """Return list of employees associated with current team"""
# from models.employee import Employee
# sql = """
# SELECT * FROM employees
# WHERE team_id = ?
# """
# CURSOR.execute(sql, (self.id,),)

# rows = CURSOR.fetchall()
# return [
# Employee.instance_from_db(row) for row in rows
# ]
41 changes: 41 additions & 0 deletions lib/seed.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from models.__init__ import CONN, CURSOR
from models.team import Team

def seed_database():
Team.drop_table()
Team.create_table()


atlanta_hawks = Team("Atlanta Hawks", "Southeast").save()
boston_celtics = Team("Boston Celtics", "Atlantic").save()
brooklyn_nets = Team("Brooklyn Nets", "Atlantic").save()
charlotte_hornets = Team("Charlotte Hornets", "Southeast").save()
chicago_bulls = Team("Chicago Bulls", "Central").save()
cleveland_cavaliers = Team("Cleveland Cavaliers", "Central").save()
dallas_mavericks = Team("Dallas Mavericks", "Southwest").save()
denver_nuggets = Team("Denver Nuggets", "Northwest").save()
detroit_pistons = Team("Detroit Pistons", "Central").save()
golden_state_warriors = Team("Golden State Warriors", "Pacific").save()
houston_rockets = Team("Houston Rockets", "Southwest").save()
indiana_pacers = Team("Indiana Pacers", "Central").save()
la_clippers = Team("LA Clippers", "Pacific").save()
los_angeles_lakers = Team("Los Angeles Lakers", "Pacific").save()
memphis_grizzlies = Team("Memphis Grizzlies", "Southwest").save()
miami_heat = Team("Miami Heat", "Southeast").save()
milwaukee_bucks = Team("Milwaukee Bucks", "Central").save()
minnesota_timberwolves = Team("Minnesota Timberwolves", "Northwest").save()
new_orleans_pelicans = Team("New Orleans Pelicans", "Southwest").save()
new_york_knicks = Team("New York Knicks", "Atlantic").save()
oklahoma_city_thunder = Team("Oklahoma City Thunder", "Northwest").save()
orlando_magic = Team("Orlando Magic", "Southeast").save()
philadelphia_76ers = Team("Philadelphia 76ers", "Atlantic").save()
phoenix_suns = Team("Phoenix Suns", "Pacific").save()
portland_trail_blazers = Team("Portland Trail Blazers", "Northwest").save()
sacramento_kings = Team("Sacramento Kings", "Pacific").save()
san_antonio_spurs = Team("San Antonio Spurs", "Southwest").save()
toronto_raptors = Team("Toronto Raptors", "Atlantic").save()
utah_jazz = Team("Utah Jazz", "Northwest").save()
washington_wizards = Team("Washington Wizards", "Southeast").save()

seed_database()
print("Seeded database")