Bläddra i källkod

Feature 'add a product' of product manager implemented

Jovian (Toshiba) 6 år sedan
förälder
incheckning
9e1c11e335
2 ändrade filer med 31 tillägg och 11 borttagningar
  1. 9 0
      app_product.py
  2. 22 11
      consumer/connect.py

+ 9 - 0
app_product.py

@@ -3,6 +3,7 @@ from consumer.credentials import getCredentials
 from model.product import Product
 from basic_display import BasicDisplay
 from cli.pro_action_asker import ProActionAsker
+from cli.name_asker import NameAsker
 
 print('|Product management application|')
 
@@ -10,6 +11,8 @@ credentials = getCredentials()
 consumer = DataConsumer(credentials)
 
 actionAsker = ProActionAsker()
+nameAsker = NameAsker()
+
 display = BasicDisplay()
 
 running = True
@@ -22,6 +25,12 @@ while running:
     elif action == 'list':
         proList = consumer.getProductSet()
         display.productList(proList)
+    elif action == 'add':
+        name = nameAsker.ask()
+        print('Adding product', name, '...')
+        consumer.addProduct(name)
+        consumer.commit()
+        print('Done.')
     else:
         print('Action', action, 'not implemented yet.')
 

+ 22 - 11
consumer/connect.py

@@ -1,20 +1,31 @@
 import psycopg2
 
 class DataConsumer():
-	"""To query database"""
-	def __init__(self, credentials):
-		self.conn = psycopg2.connect(credentials)
+    """To query database"""
+    def __init__(self, credentials):
+        self.conn = psycopg2.connect(credentials)
 
-	def __del__(self):
-		self.conn.close()
+    def __del__(self):
+        self.conn.close()
 
-	def getProductSet(self):
-		cursor = self.conn.cursor()
+    def getProductSet(self):
+        cursor = self.conn.cursor()
 
-		cursor.execute("SELECT * from Product")
+        cursor.execute("SELECT * from Product")
 
-		rows = cursor.fetchall()
+        rows = cursor.fetchall()
 
-		cursor.close()
+        cursor.close()
 
-		return rows
+        return rows
+
+    def addProduct(self, name): 
+        cursor = self.conn.cursor()
+
+        cursor.execute("INSERT INTO Product (pro_name) VALUES (%s)", (name,))
+
+        cursor.close()
+
+    def commit(self):
+        """Make the changes to the database persistent"""
+        self.conn.commit()