From 193b3a7a8fe54946dfdaadee9402a62af019e3b4 Mon Sep 17 00:00:00 2001 From: mgkEdits Date: Tue, 19 Dec 2023 09:59:55 +0300 Subject: [PATCH] first commit --- lib/company.py | 116 ++++++++++++++++++++++++++++++++++++++++++++++++ lib/debug.py | 44 ++++++++++++++++-- lib/dev.py | 45 +++++++++++++++++++ lib/freebie.py | 118 +++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 319 insertions(+), 4 deletions(-) create mode 100644 lib/company.py create mode 100644 lib/dev.py create mode 100644 lib/freebie.py diff --git a/lib/company.py b/lib/company.py new file mode 100644 index 000000000..566341799 --- /dev/null +++ b/lib/company.py @@ -0,0 +1,116 @@ +import sqlite3 +from freebie import Freebie + +CONN = sqlite3.connect("./lib/freebies.db") +CURSOR = CONN.cursor() + +class Company(): + def __init__(self, name, founding_year, id=None): + self.set_name(name) + self.founding_year = founding_year + self.id = id + + #!Validate the properties + def get_name(self): + return self._name + + def set_name(self, new_name): + if type(new_name) == str and len(new_name) > 0: + self._name = new_name + else: + print("name is not a string") + raise Exception() + + name = property(get_name, set_name) + + #!Another way to make a property + @property + def founding_year(self): + return self._founding_year + + @founding_year.setter + def founding_year(self, new_year): + if type(new_year) == int and new_year > 0: + self._founding_year = new_year + else: + raise Exception("Incorrect Year") + + #!new from db + @classmethod + def new_from_db(cls, row): + company_row = cls(name=row[1], founding_year=row[2], id = row[0]) + return company_row + + + + # Create a table that companies get saved into: + @classmethod + def create_table(cls): + sql = """ + CREATE TABLE IF NOT EXISTS companies ( + id INTEGER PRIMARY KEY, + name TEXT, + founding_year INTEGER + ) + """ + CURSOR.execute(sql) + + # Create a drop table to refresh every time code is ran + @classmethod + def drop_table(cls): + sql = "DROP TABLE IF EXISTS companies" + CURSOR.execute(sql) + + # Create option to save information to Table once created: + def save(self): + sql = """ + INSERT INTO companies (name, founding_year) + VALUES (?, ?) + """ + CURSOR.execute(sql, (self.name, self.founding_year)) + CONN.commit() + self.id = CURSOR.lastrowid + + #! Create a freebies property for company + def get_freebies(self): + sql = """ + SELECT * FROM freebies + WHERE comp_id = ? + """ + found_freebies = CURSOR.execute(sql, (self.id,)).fetchall() + return found_freebies + + freebies = property(get_freebies) + + #! Create a devs property for company + def get_devs(self): + sql = """ + SELECT devs.id, devs.name + FROM devs + INNER JOIN freebies + ON devs.id = freebies.dev_id + WHERE freebies.comp_id = ? + """ + found_devs = CURSOR.execute(sql, (self.id,)).fetchall() + return found_devs + + devs = property(get_devs) + + + #*Aggregate Methods + def give_freebie(self,Dev,item_name,value): + item_name = Freebie.create(item_name,value,self.id,Dev.id) + return item_name + + @classmethod + def oldest_company(cls): + sql = """ + SELECT * + FROM companies + ORDER BY founding_year + ASC LIMIT 1 + """ + oldest_comp = CURSOR.execute(sql).fetchone() + return oldest_comp + + \ No newline at end of file diff --git a/lib/debug.py b/lib/debug.py index 4f922eb69..806a759a3 100644 --- a/lib/debug.py +++ b/lib/debug.py @@ -1,9 +1,45 @@ #!/usr/bin/env python3 -from sqlalchemy import create_engine +from freebie import Freebie +from company import Company +from dev import Dev -from models import Company, Dev +#! Drop all tables when first started to see if it works: +Company.drop_table() +Dev.drop_table() +Freebie.drop_table() + +# #! Create all (3) tables to exist in the database: +Company.create_table() +Dev.create_table() +Freebie.create_table() + + + +#! Create some dev instances +ollie = Dev("ollie") +ollie.save() +wally = Dev("Wally") +wally.save() +bill = Dev("Bill") +bill.save() + +#! Create some company instances + +docusign = Company("DocuSign", 1999) +docusign.save() +gitHub = Company("GitHub", 2002) +gitHub.save() +walmart = Company("Walmart", 1804) +walmart.save() + +#! Create some Freebie instances +koozie = Freebie("Koozie", 10, gitHub.id, bill.id) +koozie.save() +hat = Freebie("Hat", 20, gitHub.id, wally.id) +hat.save() +pen = Freebie("Pen", 1, docusign.id, bill.id) +pen.save() if __name__ == '__main__': - engine = create_engine('sqlite:///freebies.db') - import ipdb; ipdb.set_trace() + import ipdb; ipdb.set_trace() \ No newline at end of file diff --git a/lib/dev.py b/lib/dev.py new file mode 100644 index 000000000..7ad72eac4 --- /dev/null +++ b/lib/dev.py @@ -0,0 +1,45 @@ + + +from freebie import Freebie +from company import Company +from dev import Dev + +#! Drop all tables when first started to see if it works: +Company.drop_table() +Dev.drop_table() +Freebie.drop_table() + +# #! Create all (3) tables to exist in the database: +Company.create_table() +Dev.create_table() +Freebie.create_table() + + + +#! Create some dev instances +ollie = Dev("ollie") +ollie.save() +wally = Dev("Wally") +wally.save() +bill = Dev("Bill") +bill.save() + +#! Create some company instances + +docusign = Company("DocuSign", 1999) +docusign.save() +gitHub = Company("GitHub", 2002) +gitHub.save() +walmart = Company("Walmart", 1804) +walmart.save() + +#! Create some Freebie instances +koozie = Freebie("Koozie", 10, gitHub.id, bill.id) +koozie.save() +hat = Freebie("Hat", 20, gitHub.id, wally.id) +hat.save() +pen = Freebie("Pen", 1, docusign.id, bill.id) +pen.save() + +if __name__ == '__main__': + import ipdb; ipdb.set_trace() \ No newline at end of file diff --git a/lib/freebie.py b/lib/freebie.py new file mode 100644 index 000000000..c47faed44 --- /dev/null +++ b/lib/freebie.py @@ -0,0 +1,118 @@ +import sqlite3 + +CONN = sqlite3.connect("./lib/freebies.db") +CURSOR = CONN.cursor() + +#! Create Freebies Class +class Freebie(): + def __init__(self, item_name, value, comp_id=None, dev_id=None, id=None): + self.id = id + self.item_name = item_name + self.value = value + self.comp_id = comp_id + self.dev_id = dev_id + + @property + def item_name(self): + return self._item_name + @item_name.setter + def item_name(self,new_item_name): + if type(new_item_name)==str: + self._item_name = new_item_name + else: + raise Exception("Bad Item Name...") + + @property + def dev_id(self): + return self._dev_id + @dev_id.setter + def dev_id(self, dev_id): + all_devs = CURSOR.execute("SELECT id from devs").fetchall() + all_ids = [row[0] for row in all_devs] + if dev_id in all_ids: + self._dev_id = dev_id + else: + raise Exception("No Dev with id provided") + + + #! Create a table that freebies get saved into: + @classmethod + def create_table(cls): + sql = """ + CREATE TABLE IF NOT EXISTS freebies ( + id INTEGER PRIMARY KEY, + item_name TEXT, + value INTEGER, + comp_id INTEGER, + dev_id INTEGER + ) + """ + + CURSOR.execute(sql) + + #! Create a drop table to refresh every time code is ran + @classmethod + def drop_table(cls): + sql = "DROP TABLE IF EXISTS freebies" + CURSOR.execute(sql) + + #! Create option to save information to Table once created: + def save(self): + sql = """ + INSERT INTO freebies (item_name, value, comp_id, dev_id) + VALUES (?, ?, ?, ?) + """ + CURSOR.execute(sql, (self.item_name, self.value, self.comp_id, self.dev_id)) + CONN.commit() + self.id = CURSOR.lastrowid + #! Create function that makes instance and saves to database + @classmethod + def create(cls,item_name, value, comp_id, dev_id): + item_name = cls(item_name, value, comp_id, dev_id) + item_name.save() + return item_name + + + #! Create a dev property for frisby. + def get_dev(self): + sql = """ + SELECT * FROM devs + WHERE id = ? + LIMIT 1 + """ + found_dev = CURSOR.execute(sql, (self.dev_id,)).fetchone() + return found_dev + + dev = property(get_dev) + + #! Create a company property for frisby + def get_company(self): + sql = """ + SELECT * FROM companies + WHERE id = ? + LIMIT 1 + """ + found_company = CURSOR.execute(sql, (self.comp_id,)).fetchone() + from company import Company + return Company.new_from_db(found_company) + + company = property(get_company) + + + #! Create an update freebie + def update_dev_id(self,new_dev_id): + sql = """ + UPDATE freebies + SET dev_id = ? + WHERE id = ? + """ + CURSOR.execute(sql, (new_dev_id, self.id)) + CONN.commit() + + + def print_details(self): + print(f"{self.dev[1]} owns a {self.item_name} from {self.company[1]}") + + + + \ No newline at end of file