Modificato in una REST API con Flask

This commit is contained in:
Fabio Scotto di Santolo
2021-06-20 15:08:59 +02:00
parent 2e1b532e5c
commit cf9ab44d5d
7 changed files with 275 additions and 33 deletions

View File

@@ -23,6 +23,100 @@ def __create_department(row):
))
def fetch_departments() -> List[Department]:
"""
Ricerca tutti i dipartimenti attivi
:return: elenco dei dipartimenti
"""
with connect(db_url) as connection:
with connection.cursor(cursor_factory=dbopts.DictCursor) as cursor:
cursor.execute(
"""
select
d.department_id,
d.department_name,
d.location_id,
l.postal_code,
l.street_address,
l.city,
l.state_province,
l.country_id
from
hr.departments d
left join hr.locations l on
d.location_id = l.location_id
"""
)
departments = [__create_department(row) for row in cursor.fetchall()]
return departments
def fetch_american_departments() -> List[Department]:
"""
Ricerca tutti i dipartimenti presenti negli USA.
:return: elenco dei dipartimenti USA
"""
with connect(db_url) as connection:
with connection.cursor(cursor_factory=dbopts.DictCursor) as cursor:
cursor.execute(
"""
select
d.department_id,
d.department_name,
d.location_id,
l.postal_code,
l.street_address,
l.city,
l.state_province,
l.country_id
from
hr.departments d
left join hr.locations l on
d.location_id = l.location_id
where
l.country_id = 'US'
"""
)
departments = [__create_department(row) for row in cursor.fetchall()]
return departments
def fetch_canadian_departments() -> List[Department]:
"""
Ricerca tutti i dipartimenti presenti in Canada.
:return: elenco dei dipartimenti canadesi
"""
with connect(db_url) as connection:
with connection.cursor(cursor_factory=dbopts.DictCursor) as cursor:
cursor.execute(
"""
select
d.department_id,
d.department_name,
d.location_id,
l.postal_code,
l.street_address,
l.city,
l.state_province,
l.country_id
from
hr.departments d
left join hr.locations l on
d.location_id = l.location_id
where
l.country_id = 'CA'
"""
)
departments = [__create_department(row) for row in cursor.fetchall()]
return departments
def fetch_european_departments() -> List[Department]:
"""
Ricerca tutti i dipartimenti dei paesi europei dove sono presenti
@@ -78,13 +172,24 @@ def fetch_department_employees(department_id: int) -> List[Employee]:
e.job_id,
e.salary,
e.manager_id,
e.department_id
e.department_id,
d.department_name,
l.location_id,
l.city,
l.postal_code,
l.state_province,
l.street_address,
l.country_id
from
hr.employees e
left join hr.departments d on
e.department_id = d.department_id
left join hr.locations l on
d.location_id = l.location_id
where
e.department_id = {department_id}
e.department_id = %s
""",
department_id
(department_id,)
)
# Prendo dal DB tutti i dipendenti di un dipartimento
@@ -99,7 +204,62 @@ def fetch_department_employees(department_id: int) -> List[Employee]:
job_id=row["job_id"],
salary=row["salary"],
manager_id=row["manager_id"],
department=None
department=__create_department(row)
))
return employees
def fetch_employees() -> List[Employee]:
"""
Ricerca tutti i dipendenti dell'azienda
:return: elenco dei dipendenti
"""
employees = []
with connect(db_url) as connection:
with connection.cursor(cursor_factory=dbopts.DictCursor) as cursor:
cursor.execute(
f"""
select
e.employee_id,
e.first_name,
e.last_name,
e.email,
e.phone_number,
e.hire_date,
e.job_id,
e.salary,
e.manager_id,
e.department_id,
d.department_name,
l.location_id,
l.city,
l.postal_code,
l.state_province,
l.street_address,
l.country_id
from
hr.employees e
left join hr.departments d on
e.department_id = d.department_id
left join hr.locations l on
d.location_id = l.location_id
"""
)
# Prendo dal DB tutti i dipendenti di un dipartimento
for row in cursor.fetchall():
employees.append(Employee(
employee_id=row["employee_id"],
first_name=row["first_name"],
last_name=row["last_name"],
email=row["email"],
phone_number=row["phone_number"],
hire_date=row["hire_date"],
job_id=row["job_id"],
salary=row["salary"],
manager_id=row["manager_id"],
department=__create_department(row)
))
return employees