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------------------------- DATABASE ------------------------- #
|
6 |
+
conn = sqlite3.connect('library.db', check_same_thread=False)
|
7 |
+
c = conn.cursor()
|
8 |
+
|
9 |
+
def init_db():
|
10 |
+
c.execute('''CREATE TABLE IF NOT EXISTS books (
|
11 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
12 |
+
title TEXT,
|
13 |
+
author TEXT,
|
14 |
+
genre TEXT,
|
15 |
+
isbn TEXT UNIQUE,
|
16 |
+
status TEXT DEFAULT 'available'
|
17 |
+
)''')
|
18 |
+
|
19 |
+
c.execute('''CREATE TABLE IF NOT EXISTS members (
|
20 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
21 |
+
name TEXT,
|
22 |
+
email TEXT UNIQUE,
|
23 |
+
phone TEXT
|
24 |
+
)''')
|
25 |
+
|
26 |
+
c.execute('''CREATE TABLE IF NOT EXISTS transactions (
|
27 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
28 |
+
book_id INTEGER,
|
29 |
+
member_id INTEGER,
|
30 |
+
action TEXT,
|
31 |
+
date TEXT,
|
32 |
+
FOREIGN KEY(book_id) REFERENCES books(id),
|
33 |
+
FOREIGN KEY(member_id) REFERENCES members(id)
|
34 |
+
)''')
|
35 |
+
|
36 |
+
c.execute('''CREATE TABLE IF NOT EXISTS reservations (
|
37 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
38 |
+
book_id INTEGER,
|
39 |
+
member_id INTEGER,
|
40 |
+
reserved_at TEXT,
|
41 |
+
FOREIGN KEY(book_id) REFERENCES books(id),
|
42 |
+
FOREIGN KEY(member_id) REFERENCES members(id)
|
43 |
+
)''')
|
44 |
+
|
45 |
+
c.execute('''CREATE TABLE IF NOT EXISTS users (
|
46 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
47 |
+
username TEXT UNIQUE,
|
48 |
+
password TEXT,
|
49 |
+
role TEXT DEFAULT 'member'
|
50 |
+
)''')
|
51 |
+
|
52 |
+
conn.commit()
|
53 |
+
|
54 |
+
# ------------------------- FUNCTIONS ------------------------- #
|
55 |
+
def add_book(title, author, genre, isbn):
|
56 |
+
c.execute("INSERT INTO books (title, author, genre, isbn) VALUES (?, ?, ?, ?)", (title, author, genre, isbn))
|
57 |
+
conn.commit()
|
58 |
+
|
59 |
+
def get_books():
|
60 |
+
c.execute("SELECT * FROM books")
|
61 |
+
return c.fetchall()
|
62 |
+
|
63 |
+
def issue_book(book_id, member_id):
|
64 |
+
c.execute("UPDATE books SET status='issued' WHERE id=?", (book_id,))
|
65 |
+
c.execute("INSERT INTO transactions (book_id, member_id, action, date) VALUES (?, ?, 'issued', ?)", (book_id, member_id, datetime.now()))
|
66 |
+
conn.commit()
|
67 |
+
|
68 |
+
def return_book(book_id, member_id):
|
69 |
+
c.execute("UPDATE books SET status='available' WHERE id=?", (book_id,))
|
70 |
+
c.execute("INSERT INTO transactions (book_id, member_id, action, date) VALUES (?, ?, 'returned', ?)", (book_id, member_id, datetime.now()))
|
71 |
+
conn.commit()
|
72 |
+
|
73 |
+
def reserve_book(book_id, member_id):
|
74 |
+
c.execute("INSERT INTO reservations (book_id, member_id, reserved_at) VALUES (?, ?, ?)", (book_id, member_id, datetime.now()))
|
75 |
+
conn.commit()
|
76 |
+
|
77 |
+
def add_member(name, email, phone):
|
78 |
+
c.execute("INSERT INTO members (name, email, phone) VALUES (?, ?, ?)", (name, email, phone))
|
79 |
+
conn.commit()
|
80 |
+
|
81 |
+
def get_members():
|
82 |
+
c.execute("SELECT * FROM members")
|
83 |
+
return c.fetchall()
|
84 |
+
|
85 |
+
def add_user(username, password, role='member'):
|
86 |
+
c.execute("INSERT INTO users (username, password, role) VALUES (?, ?, ?)", (username, password, role))
|
87 |
+
conn.commit()
|
88 |
+
|
89 |
+
def authenticate_user(username, password):
|
90 |
+
c.execute("SELECT * FROM users WHERE username=? AND password=?", (username, password))
|
91 |
+
return c.fetchone()
|
92 |
+
|
93 |
+
def get_transactions():
|
94 |
+
c.execute("""SELECT t.id, b.title, m.name, t.action, t.date FROM transactions t
|
95 |
+
JOIN books b ON t.book_id = b.id
|
96 |
+
JOIN members m ON t.member_id = m.id ORDER BY t.date DESC""")
|
97 |
+
return c.fetchall()
|
98 |
+
|
99 |
+
def get_reservations():
|
100 |
+
c.execute("""SELECT r.id, b.title, m.name, r.reserved_at FROM reservations r
|
101 |
+
JOIN books b ON r.book_id = b.id
|
102 |
+
JOIN members m ON r.member_id = m.id""")
|
103 |
+
return c.fetchall()
|
104 |
+
|
105 |
+
def get_most_borrowed_books():
|
106 |
+
c.execute("SELECT b.title, COUNT(*) as borrow_count FROM transactions t JOIN books b ON t.book_id = b.id WHERE t.action='issued' GROUP BY b.title ORDER BY borrow_count DESC LIMIT 5")
|
107 |
+
return c.fetchall()
|
108 |
+
|
109 |
+
# ------------------------- STREAMLIT UI ------------------------- #
|
110 |
+
st.set_page_config(page_title="Library Management System", layout="wide")
|
111 |
+
|
112 |
+
if 'logged_in' not in st.session_state:
|
113 |
+
st.session_state.logged_in = False
|
114 |
+
st.session_state.role = ''
|
115 |
+
|
116 |
+
init_db()
|
117 |
+
|
118 |
+
if not st.session_state.logged_in:
|
119 |
+
st.title("π Login")
|
120 |
+
with st.form("login"):
|
121 |
+
username = st.text_input("Username")
|
122 |
+
password = st.text_input("Password", type="password")
|
123 |
+
submitted = st.form_submit_button("Login")
|
124 |
+
if submitted:
|
125 |
+
user = authenticate_user(username, password)
|
126 |
+
if user:
|
127 |
+
st.session_state.logged_in = True
|
128 |
+
st.session_state.username = username
|
129 |
+
st.session_state.role = user[3] # role
|
130 |
+
st.experimental_rerun()
|
131 |
+
else:
|
132 |
+
st.error("Invalid credentials")
|
133 |
+
else:
|
134 |
+
st.title("π Library Management System")
|
135 |
+
menu = ["Dashboard", "Add Book", "Issue/Return Book", "Reserve Book", "Audit Logs"]
|
136 |
+
if st.session_state.role == 'admin':
|
137 |
+
menu.append("Users")
|
138 |
+
choice = st.sidebar.selectbox("Menu", menu)
|
139 |
+
|
140 |
+
if choice == "Dashboard":
|
141 |
+
st.subheader("Library Overview")
|
142 |
+
books = get_books()
|
143 |
+
members = get_members()
|
144 |
+
st.metric("Total Books", len(books))
|
145 |
+
st.metric("Total Members", len(members))
|
146 |
+
st.metric("Issued Books", sum(1 for b in books if b[5] == 'issued'))
|
147 |
+
st.metric("Available Books", sum(1 for b in books if b[5] == 'available'))
|
148 |
+
|
149 |
+
st.subheader("π Most Borrowed Books")
|
150 |
+
popular = get_most_borrowed_books()
|
151 |
+
for title, count in popular:
|
152 |
+
st.write(f"πΉ {title}: {count} times")
|
153 |
+
|
154 |
+
elif choice == "Add Book":
|
155 |
+
st.subheader("Add a New Book")
|
156 |
+
with st.form("add_book"):
|
157 |
+
title = st.text_input("Title")
|
158 |
+
author = st.text_input("Author")
|
159 |
+
genre = st.text_input("Genre")
|
160 |
+
isbn = st.text_input("ISBN")
|
161 |
+
submitted = st.form_submit_button("Add Book")
|
162 |
+
if submitted:
|
163 |
+
add_book(title, author, genre, isbn)
|
164 |
+
st.success("β
Book added successfully")
|
165 |
+
|
166 |
+
elif choice == "Issue/Return Book":
|
167 |
+
st.subheader("Manage Book Transactions")
|
168 |
+
books = get_books()
|
169 |
+
members = get_members()
|
170 |
+
book_dict = {f"{b[1]} ({b[4]})": b[0] for b in books}
|
171 |
+
member_dict = {f"{m[1]} ({m[2]})": m[0] for m in members}
|
172 |
+
tab1, tab2 = st.tabs(["π¦ Issue", "π₯ Return"])
|
173 |
+
|
174 |
+
with tab1:
|
175 |
+
selected_book = st.selectbox("Select Book to Issue", list(book_dict.keys()))
|
176 |
+
selected_member = st.selectbox("Select Member", list(member_dict.keys()))
|
177 |
+
if st.button("Issue Book"):
|
178 |
+
issue_book(book_dict[selected_book], member_dict[selected_member])
|
179 |
+
st.success("π¦ Book Issued")
|
180 |
+
|
181 |
+
with tab2:
|
182 |
+
selected_book = st.selectbox("Select Book to Return", list(book_dict.keys()), key='return')
|
183 |
+
selected_member = st.selectbox("Select Member", list(member_dict.keys()), key='return_mem')
|
184 |
+
if st.button("Return Book"):
|
185 |
+
return_book(book_dict[selected_book], member_dict[selected_member])
|
186 |
+
st.success("π₯ Book Returned")
|
187 |
+
|
188 |
+
elif choice == "Reserve Book":
|
189 |
+
st.subheader("Reserve a Book")
|
190 |
+
books = get_books()
|
191 |
+
members = get_members()
|
192 |
+
available_books = [b for b in books if b[5] == 'available']
|
193 |
+
book_dict = {f"{b[1]} ({b[4]})": b[0] for b in available_books}
|
194 |
+
member_dict = {f"{m[1]} ({m[2]})": m[0] for m in members}
|
195 |
+
if book_dict and member_dict:
|
196 |
+
selected_book = st.selectbox("Select Book", list(book_dict.keys()))
|
197 |
+
selected_member = st.selectbox("Select Member", list(member_dict.keys()))
|
198 |
+
if st.button("Reserve Book"):
|
199 |
+
reserve_book(book_dict[selected_book], member_dict[selected_member])
|
200 |
+
st.success("π Book Reserved")
|
201 |
+
else:
|
202 |
+
st.warning("No available books or members to reserve")
|
203 |
+
|
204 |
+
elif choice == "Audit Logs":
|
205 |
+
st.subheader("Transaction History")
|
206 |
+
logs = get_transactions()
|
207 |
+
st.dataframe(logs, use_container_width=True)
|
208 |
+
st.subheader("Reservation Logs")
|
209 |
+
res = get_reservations()
|
210 |
+
st.dataframe(res, use_container_width=True)
|
211 |
+
|
212 |
+
elif choice == "Users" and st.session_state.role == 'admin':
|
213 |
+
st.subheader("User Management")
|
214 |
+
with st.form("add_user"):
|
215 |
+
username = st.text_input("Username")
|
216 |
+
password = st.text_input("Password", type="password")
|
217 |
+
role = st.selectbox("Role", ["admin", "librarian", "member"])
|
218 |
+
if st.form_submit_button("Add User"):
|
219 |
+
add_user(username, password, role)
|
220 |
+
st.success("π€ User Added")
|
221 |
+
|
222 |
+
st.sidebar.write("---")
|
223 |
+
if st.sidebar.button("Logout"):
|
224 |
+
st.session_state.logged_in = False
|
225 |
+
st.experimental_rerun()
|