Skip to content

Commit

Permalink
Added class DataBaseService
Browse files Browse the repository at this point in the history
  • Loading branch information
Niepawowsky committed Dec 31, 2023
1 parent f3a7665 commit 2b817ba
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,4 @@ cython_debug/
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
.idea/
database/sample_books.db
107 changes: 107 additions & 0 deletions database/db_service.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import sqlite3
from collections import namedtuple
from datetime import datetime, date


class DbManager:
"""
Context manager created for creation or wraping database
"""

def __init__(self, connection) -> None:
self.connection = connection
print('init statement')
self.cursor = None

def __enter__(self):
self.cursor = self.connection.cursor()
print('Enter statement')
return self

def __exit__(self, exc_type, exc_val, exc_tb):
print('exit statement')
if isinstance(exc_val, Exception):
self.connection.rollback()
print('isinstance print')
else:
self.connection.commit()
print('commiting query')

# self.connection.close()
# print('Connection closed')


class DataBaseService():

# connection = sqlite3.connect('database/sample_books.db')

def __init__(self, connection):
self.connection = connection

def create_database(self, connection):
with DbManager(connection) as database:
print("CREATING TABLE IF NOT EXISTS")
database.cursor.execute('''CREATE TABLE IF NOT EXISTS borrowed_books(
id INTEGER PRIMARY KEY AUTOINCREMENT,
email TEXT,
name TEXT,
title TEXT NOT NULL,
author TEXT NOT NULL,
return_at DATE)'''
)

def get_borrowed_books_by_today(self):

"""
The get_borrowed_books_by_today function returns a list of
namedtuples containing the name, email, title and return_at
of all books that are overdue. The return_at is converted to a datetime object for easier comparison.
:param self: Represent the instance of the class
:return: A list of borrowers
"""
today = datetime.today()
borrower = namedtuple('Borrower', 'name email title return_at')
borrower_list = []
with DbManager(connection=self.connection) as database:
database.cursor.execute('''SELECT
name,
email,
title,
return_at
FROM borrowed_books
WHERE return_at < ?''', (today,))

for name, email, title, return_at in database.cursor.fetchall():
return_date = datetime.strptime(return_at, '%Y-%m-%d %H:%M:%S')
borrower_list.append(borrower(name, email, title, return_date))

return borrower_list

def get_borrowed_for_remider(self):
"""
The get_borrowed_for_remider function returns a list of
namedtuples containing the name, email, title and return_at
for each book that is currently borrowed.
The function will be used to send reminder emails to users who have overdue books.
:param self: Represent the instance of the class
:return: A list of named tuples
"""
today = datetime.today()
borrower = namedtuple('Borrower', 'name email title return_at')
borrower_list = []
with DbManager(connection=self.connection) as database:
database.cursor.execute('''SELECT
name,
email,
title,
return_at
FROM borrowed_books''')

for name, email, title, return_at in database.cursor.fetchall():
return_date = datetime.strptime(return_at, '%Y-%m-%d %H:%M:%S')
print(return_date)
borrower_list.append(borrower(name, email, title, return_date))

return borrower_list
13 changes: 13 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from collections import namedtuple
from datetime import datetime
from database.db_service import DbManager, DataBaseService
from database.db_service import DbManager
import sqlite3

connection = sqlite3.connect('database/sample_books.db') #być może trzeba będzie to zostawić

db= DataBaseService(connection)

# print(db.get_borrowed_books_by_today())
print(db.get_borrowed_for_remider())

0 comments on commit 2b817ba

Please sign in to comment.