def delete_customer_by_id(id): if request.method == 'DELETE': # Delete user by id customer = CustomerEntity(customer_id=id) c = Customer(connection_data) result = c.delete_Customer(customer) return jsonify({'message': result[0]}), result[1] else: # Update user by id data = request.json customer = CustomerEntity(customer_id=id, customer_name=data['customer_name'], contact_name=data['contact_name'], address=data['address'], city=data['city'], postal_code=data['postal_code'], country=data['country']) c = Customer(connection_data) result = c.update_Customer(customer) return jsonify({'message': result[0]}), result[1]
def get_one_customer(id): customer = CustomerEntity(customer_id=id) c = Customer(connection_data) result = c.get_one_Customer(customer) return jsonify({'data': result})
def get_by_id(self, customer: CustomerEntity): con = None try: con = psycopg2.connect(user=self.ConnectionData['user'], password=self.ConnectionData['password'], host=self.ConnectionData['host'], port=self.ConnectionData['port'], database=self.ConnectionData['database']) cur = con.cursor() sql = "SELECT * FROM TblCustomers WHERE customerid=%s" cur.execute(sql, (customer.CustomerID, )) con.commit() row = cur.fetchone() if row: c = CustomerEntity() c.fetch_data(row) return c, 200 con.close() return "Customer ID not found", 404 except (Exception, psycopg2.DatabaseError) as error: return str(error) finally: if con is not None: con.close()
def get_all(self): con = None try: con = psycopg2.connect(user=self.ConnectionData['user'], password=self.ConnectionData['password'], host=self.ConnectionData['host'], port=self.ConnectionData['port'], database=self.ConnectionData['database']) cur = con.cursor() sql = "SELECT * FROM TblCustomers" cur.execute(sql) con.commit() rows = cur.fetchall() result = [] for row in rows: c = CustomerEntity() c.fetch_data(row) result.append(c.to_json()) con.close() return result except (Exception, psycopg2.DatabaseError) as error: return str(error) finally: if con is not None: con.close()
def get_one_Customer(self, customer: CustomerEntity): conn = psycopg2.connect(host=self.connection_data['host'], port=self.connection_data['port'], user=self.connection_data['user'], password=self.connection_data['password'], database=self.connection_data['database']) cursor = conn.cursor() sql = "SELECT * FROM customers Where customerid=%s " cursor.execute(sql, ((customer.customer_id, ))) conn.commit() row = cursor.fetchone() # result = {} # for row in rows: customer = CustomerEntity() customer.fetch_data(row) # result.append(customer.to_json()) return customer.to_json()
def get_all_Customer(self): conn = psycopg2.connect(host=self.connection_data['host'], port=self.connection_data['port'], user=self.connection_data['user'], password=self.connection_data['password'], database=self.connection_data['database']) cursor = conn.cursor() sql = "SELECT * FROM customers" cursor.execute(sql) conn.commit() rows = cursor.fetchall() result = [] for row in rows: customer = CustomerEntity() customer.fetch_data(row) result.append(customer.to_json()) return result
def add_customer(): data = request.json customer = CustomerEntity(customer_name=data['customer_name'], contact_name=data['contact_name'], address=data['address'], city=data['city'], postal_code=data['postal_code'], country=data['country']) c = Customer(connection_data) message = c.insert(customer) if message is None: return jsonify({'message': 'Cannot insert data'}), 500 return jsonify({'message': message})