Skip to content
Snippets Groups Projects
Commit fdec3b37 authored by Chude, Chiamaka A (PG/T - Comp Sci & Elec Eng)'s avatar Chude, Chiamaka A (PG/T - Comp Sci & Elec Eng)
Browse files

Update - implemented the event driven architecture with kafka event store and...

Update - implemented the event driven architecture with kafka event store and streaming service for asynchronous communication between the product and user microservice
parent eca07e4b
No related branches found
No related tags found
1 merge request!5Update - implemented the event driven architecture using kafka
File added
No preview for this file type
...@@ -18,17 +18,17 @@ def check_old_password(data): ...@@ -18,17 +18,17 @@ def check_old_password(data):
# Check if the email already exists # Check if the email already exists
email_check_query = "SELECT COUNT(*) FROM dbo.User_table WHERE UserID = ?" email_check_query = "SELECT COUNT(*) FROM dbo.User_table WHERE UserID = ?"
cursor.execute(email_check_query, user_id) cursor.execute(email_check_query, user_id) #executes the query
count = cursor.fetchone()[0] count = cursor.fetchone()[0]
if count == 0: if count == 0:
return ({"Error" : "Email does not exist"}, 0) return ({"Error" : "Email does not exist"}, 0)
else: else:
#selects password information from database
query = "SELECT t1.Email, t1.UserID, t2.Iterations, t2.PasswordHash, t2.PasswordSalt FROM dbo.User_table as t1 INNER JOIN dbo.AuthInfo as t2 ON t1.UserID = t2.UserID where t1.UserID= ?" query = "SELECT t1.Email, t1.UserID, t2.Iterations, t2.PasswordHash, t2.PasswordSalt FROM dbo.User_table as t1 INNER JOIN dbo.AuthInfo as t2 ON t1.UserID = t2.UserID where t1.UserID= ?"
cursor.execute(query, user_id) cursor.execute(query, user_id) #executes query
row = cursor.fetchone() row = cursor.fetchone()
...@@ -67,13 +67,13 @@ def set_new_password(data): ...@@ -67,13 +67,13 @@ def set_new_password(data):
user_id = data["user_id"] user_id = data["user_id"]
# #selects user ID
select_userID_query = "SELECT UserID from dbo.User_table where UserID = ?" select_userID_query = "SELECT UserID from dbo.User_table where UserID = ?"
cursor.execute(select_userID_query, user_id) cursor.execute(select_userID_query, user_id)
UserID = cursor.fetchone()[0] UserID = cursor.fetchone()[0]
# #updates password
update_authinfo_query = '''UPDATE dbo.AuthInfo update_authinfo_query = '''UPDATE dbo.AuthInfo
SET PasswordHash = ?, PasswordSalt = ?, Iterations = ?, TempPlainText = ? SET PasswordHash = ?, PasswordSalt = ?, Iterations = ?, TempPlainText = ?
WHERE UserID = ?''' WHERE UserID = ?'''
......
from flask import jsonify
import pyodbc
from models.database_connection import connect_db
def get_username(id):
try: #error handling
connection = connect_db()
cursor = connection.cursor()
query = "SELECT Username FROM dbo.User_table where UserID = ?"
cursor.execute(query, id)
row = cursor.fetchone() #fetch data
if row:
return row[0] # Assuming the username is in the first column
else:
return None
#connection.close()
return username
except pyodbc.Error as e: #error handling
print(f"Database error in fetch_user_info: {e}")
return None
except Exception as e: #error handling
print(f"Unexpected error occured in fetch_user_info: {e}")
return None
finally:
if cursor:
cursor.close()
if connection:
connection.close()
#from app import db
from flask import jsonify
import pyodbc
from models.database_connection import connect_db
def fetch_user_info(id):
try: #error handling
connection = connect_db()
cursor = connection.cursor()
query = "SELECT First_name, Last_name FROM dbo.User_table where UserID = ?"
cursor.execute(query, id)
row = cursor.fetchone() #fetch data
columns = [column[0] for column in cursor.description]
user_data = dict(zip(columns, row))
#connection.close()
return user_data
except pyodbc.Error as e: #error handling
print(f"Database error in fetch_user_info: {e}")
return None
except Exception as e: #error handling
print(f"Unexpected error occured in fetch_user_info: {e}")
return None
finally:
if cursor:
cursor.close()
if connection:
connection.close()
\ No newline at end of file
from publishers.kafkaPublishers import create_profile_updated_topic
\ No newline at end of file
File added
File added
from kafka import KafkaProducer
from kafka.admin import KafkaAdminClient, NewTopic
import json
producer = KafkaProducer(bootstrap_servers="localhost:9092")
def create_profile_updated_topic():
# Create KafkaAdminClient instance
admin_client = KafkaAdminClient(bootstrap_servers="localhost:9092")
# Define the topic name and configuration
topic_name = "profile_updated_topic"
num_partitions = 1
replication_factor = 1
# Retrieve the list of existing topics
topic_metadata = admin_client.list_topics()
# Check if the topic already exists
if topic_name not in topic_metadata:
# Define the configuration for the new topic
new_topic = NewTopic(name=topic_name, num_partitions=num_partitions, replication_factor=replication_factor)
# Create the new topic
admin_client.create_topics(new_topics=[new_topic], validate_only=False)
def publish_username_updated_event(event_data):
# Serialize the event data to JSON
event_json = json.dumps(event_data)
# Publish the event to the Kafka topic
data_to_send = producer.send("profile_updated_topic", value=event_json.encode("utf-8"))
try:
record_metadata = data_to_send.get(timeout=10)
print("Message sent successfully!")
print("Topic:", record_metadata.topic)
print("Partition:", record_metadata.partition)
print("Offset:", record_metadata.offset)
except Exception as e:
print("Failed to send message:", e)
producer.flush()
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment