Skip to content

Commit

Permalink
Update api
Browse files Browse the repository at this point in the history
  • Loading branch information
hdakhli committed Nov 19, 2023
1 parent 96fa6e9 commit 28f7810
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 28 deletions.
8 changes: 2 additions & 6 deletions 6_architecture/api/quiz_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,8 @@ def say_hello(username: str) -> dict:


@app.post("/send-answer")
def answer(username: str, question_id: int, answer_id: int) -> dict:
return {
"username": username,
"question_id": question_id,
"answer_id": answer_id
}
def answer() -> dict:
raise NotImplementedError()


if __name__ == "__main__":
Expand Down
26 changes: 19 additions & 7 deletions 6_architecture/api/quiz_dao.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import logging
import sqlite3
from sqlite3 import OperationalError


class QuizDAO:
Expand All @@ -8,12 +10,15 @@ def __init__(self):
question_id INT NOT NULL,
user_name text NOT NULL,
answer_id INT NOT NULL);'''
with self.create_connection() as conn:
cur = conn.cursor()
cur.execute(sql)
conn.commit()
try:
with self._create_connection() as conn:
cur = conn.cursor()
cur.execute(sql)
conn.commit()
except OperationalError as error:
logging.error("Database error", error.args)

def create_connection(self):
def _create_connection(self):
conn = None
try:
conn = sqlite3.connect(self.database)
Expand All @@ -24,16 +29,23 @@ def create_connection(self):
def create_answer(self, question_id, user_name, answer_id):
sql = ''' INSERT INTO QUIZ (question_id, user_name, answer_id)
VALUES(?,?,?) '''
with self.create_connection() as conn:
with self._create_connection() as conn:
cur = conn.cursor()
cur.execute(sql, (question_id, user_name, answer_id))
conn.commit()

def count_answers_by_id(self, question_id: int):
sql = ''' SELECT * FROM QUIZ WHERE question_id=?'''
with self.create_connection() as conn:
with self._create_connection() as conn:
cur = conn.cursor()
cur.execute(sql, (question_id,))
rows = cur.fetchall()
print(f"rows: {rows}")
return len(rows)

def update_answer(self, question_id: int, user_name, answer_id):
sql = ''' UPDATE QUIZ SET answer_id=? WHERE question_id=? AND user_name=?'''
with self._create_connection() as conn:
cur = conn.cursor()
cur.execute(sql, (answer_id, question_id, user_name))
conn.commit()
6 changes: 1 addition & 5 deletions 6_architecture/concurrency/multiprocessing_calls.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import time
from multiprocessing import Pool


def call_function(user_name):
Expand All @@ -11,15 +10,12 @@ def call_function(user_name):
start_time = time.time()

users = []
for user_id in range(1, 15):
for user_id in range(1, 16):
users.append(user_id)

for user in users:
call_function(user)

# pool = Pool(15)
# pool.map(call_function, users)

end_time = time.time()
duration = end_time - start_time
print(f"Duration: {duration}")
12 changes: 2 additions & 10 deletions 6_architecture/concurrency/multithreading_calls.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from threading import Thread
import time


Expand All @@ -10,19 +9,12 @@ def call_function(user_name):
if __name__ == '__main__':
start_time = time.time()
users = []
for user_id in range(1, 15):
for user_id in range(1, 16):
users.append(user_id)

# creating threads
threads = []
for user in users:
th = Thread(target=call_function, args=(user,))
th.start()
threads.append(th)
call_function(user)

# check the end of all threads
for th in threads:
th.join()
end_time = time.time()
duration = end_time - start_time
print(f"Duration: {duration}")

0 comments on commit 28f7810

Please sign in to comment.