From a34db9ad2c3dafd59fbcb1698e7e5b55435873ea Mon Sep 17 00:00:00 2001 From: michael Date: Mon, 3 Feb 2025 01:42:16 +0100 Subject: [PATCH] implement frameworks solution --- src/framework_driver/db/db_setup.py | 8 ++++++-- src/framework_driver/ui/src/controller.js | 7 +++++-- src/framework_driver/ui/src/model.js | 12 +++++++++++- src/framework_driver/ui/src/view.js | 8 +++++++- 4 files changed, 29 insertions(+), 6 deletions(-) diff --git a/src/framework_driver/db/db_setup.py b/src/framework_driver/db/db_setup.py index f6e2958..a678c65 100644 --- a/src/framework_driver/db/db_setup.py +++ b/src/framework_driver/db/db_setup.py @@ -31,8 +31,12 @@ def delete_table(conn, table_name): def main(): database = "framework_driver/db/shop.db" - #TODO: Create User Table - sql_user_table = """ + sql_user_table = """CREATE TABLE IF NOT EXISTS users ( + id INTEGER PRIMARY KEY, + name TEXT NOT NULL, + email TEXT NOT NULL, + password TEXT NOT NULL + ) """ sql_product_table = """CREATE TABLE IF NOT EXISTS products ( diff --git a/src/framework_driver/ui/src/controller.js b/src/framework_driver/ui/src/controller.js index 31a48ab..f5c3ffa 100644 --- a/src/framework_driver/ui/src/controller.js +++ b/src/framework_driver/ui/src/controller.js @@ -2,8 +2,11 @@ const Controller = { async createUser(name, email, password){ - //TODO: use model to send request to backend, then call View to render response - alert('User Creation functionality not implemented.'); + const userCreateResponse = await Model.createUser(name, email, password); + if(userCreateResponse.status=="success"){ + const user = userCreateResponse.data; + View.renderUser(user); + } }, async showProducts() { diff --git a/src/framework_driver/ui/src/model.js b/src/framework_driver/ui/src/model.js index ed6f21c..07446e2 100644 --- a/src/framework_driver/ui/src/model.js +++ b/src/framework_driver/ui/src/model.js @@ -1,6 +1,16 @@ // Model: Handles data fetching const Model = { - //TODO: Add function to send CreateUserRequest to Backend + + async createUser(name, email, password) { + const response = await fetch(`http://127.0.0.1:8000/create_user`, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ user_name: name, user_email: email, user_password: password }), + }); + return response.json(); + }, async fetchProducts() { const response = await fetch('http://127.0.0.1:8000/view_products'); diff --git a/src/framework_driver/ui/src/view.js b/src/framework_driver/ui/src/view.js index 2d661c7..008d430 100644 --- a/src/framework_driver/ui/src/view.js +++ b/src/framework_driver/ui/src/view.js @@ -1,6 +1,12 @@ // View: Handles rendering const View = { - //TODO: Add function to render UserCreateResponse + renderUser(user) { + const main = document.getElementById('main'); + main.innerHTML = ` +

${user.name}

+

email: ${user.email}

+ `; + }, renderProducts(products) { const main = document.getElementById('main');