File size: 5,772 Bytes
f2c3e81
a26bbe7
 
f2c3e81
a26bbe7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import streamlit as st
import requests
from typing import List, Optional

# API base URL
BASE_URL = "https://geeksiddhant-enrollmentapi.hf.space"

# Initialize session state for customers list
if 'customers' not in st.session_state:
    st.session_state.customers = []

def fetch_customers():
    """Fetch all customers from the API"""
    try:
        response = requests.get(f"{BASE_URL}/customers")
        if response.status_code == 200:
            st.session_state.customers = response.json()
    except requests.exceptions.RequestException as e:
        st.error(f"Error fetching customers: {str(e)}")

def create_customer(name: str, email: str, phone: Optional[str] = None, address: Optional[str] = None):
    """Create a new customer"""
    try:
        # Find the next available ID
        next_id = max([c['id'] for c in st.session_state.customers], default=0) + 1
        
        customer_data = {
            "id": next_id,
            "name": name,
            "email": email,
            "phone": phone,
            "address": address
        }
        
        response = requests.post(f"{BASE_URL}/customers", json=customer_data)
        if response.status_code == 200:
            st.success("Customer created successfully!")
            fetch_customers()
        else:
            st.error(f"Error creating customer: {response.text}")
    except requests.exceptions.RequestException as e:
        st.error(f"Error creating customer: {str(e)}")

def update_customer(customer_id: int, name: str, email: str, phone: Optional[str] = None, address: Optional[str] = None):
    """Update an existing customer"""
    try:
        customer_data = {
            "id": customer_id,
            "name": name,
            "email": email,
            "phone": phone,
            "address": address
        }
        
        response = requests.put(f"{BASE_URL}/customers/{customer_id}", json=customer_data)
        if response.status_code == 200:
            st.success("Customer updated successfully!")
            fetch_customers()
        else:
            st.error(f"Error updating customer: {response.text}")
    except requests.exceptions.RequestException as e:
        st.error(f"Error updating customer: {str(e)}")

def delete_customer(customer_id: int):
    """Delete a customer"""
    try:
        response = requests.delete(f"{BASE_URL}/customers/{customer_id}")
        if response.status_code == 200:
            st.success("Customer deleted successfully!")
            fetch_customers()
        else:
            st.error(f"Error deleting customer: {response.text}")
    except requests.exceptions.RequestException as e:
        st.error(f"Error deleting customer: {str(e)}")

# Streamlit UI
st.title("Customer Management System")

# Sidebar for navigation
page = st.sidebar.selectbox("Choose an operation", ["View Customers", "Add Customer", "Update Customer", "Delete Customer"])

# Fetch customers on initial load
fetch_customers()

if page == "View Customers":
    st.header("All Customers")
    if st.session_state.customers:
        for customer in st.session_state.customers:
            st.write(f"**ID:** {customer['id']}")
            st.write(f"**Name:** {customer['name']}")
            st.write(f"**Email:** {customer['email']}")
            if customer.get('phone'):
                st.write(f"**Phone:** {customer['phone']}")
            if customer.get('address'):
                st.write(f"**Address:** {customer['address']}")
            st.write("---")
    else:
        st.info("No customers found.")

elif page == "Add Customer":
    st.header("Add New Customer")
    with st.form("add_customer_form"):
        name = st.text_input("Name")
        email = st.text_input("Email")
        phone = st.text_input("Phone (optional)")
        address = st.text_area("Address (optional)")
        
        submitted = st.form_submit_button("Add Customer")
        if submitted:
            if name and email:
                create_customer(name, email, phone, address)
            else:
                st.warning("Please fill in the required fields (Name and Email)")

elif page == "Update Customer":
    st.header("Update Customer")
    if st.session_state.customers:
        customer_id = st.selectbox(
            "Select Customer to Update",
            options=[c['id'] for c in st.session_state.customers]
        )
        
        # Get the selected customer's data
        selected_customer = next((c for c in st.session_state.customers if c['id'] == customer_id), None)
        
        if selected_customer:
            with st.form("update_customer_form"):
                name = st.text_input("Name", value=selected_customer['name'])
                email = st.text_input("Email", value=selected_customer['email'])
                phone = st.text_input("Phone", value=selected_customer.get('phone', ''))
                address = st.text_area("Address", value=selected_customer.get('address', ''))
                
                submitted = st.form_submit_button("Update Customer")
                if submitted:
                    if name and email:
                        update_customer(customer_id, name, email, phone, address)
                    else:
                        st.warning("Please fill in the required fields (Name and Email)")
    else:
        st.info("No customers available to update.")

elif page == "Delete Customer":
    st.header("Delete Customer")
    if st.session_state.customers:
        customer_id = st.selectbox(
            "Select Customer to Delete",
            options=[c['id'] for c in st.session_state.customers]
        )
        
        if st.button("Delete Customer"):
            delete_customer(customer_id)
    else:
        st.info("No customers available to delete.")