forked from microsoft/github-copilot-vibe-coding-workshop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
217 lines (169 loc) · 7.62 KB
/
Copy pathdatabase.py
File metadata and controls
217 lines (169 loc) · 7.62 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
"""
Database operations for the FastAPI Social Media API
Handles SQLite database initialization and CRUD operations.
"""
import sqlite3
from datetime import datetime
from typing import List, Optional
DATABASE_URL = "sns_api.db"
def get_db_connection():
"""Get a database connection with row factory for dict-like access"""
conn = sqlite3.connect(DATABASE_URL)
conn.row_factory = sqlite3.Row # Enable dict-like access to rows
return conn
def init_database():
"""Initialize the SQLite database with required tables"""
conn = sqlite3.connect(DATABASE_URL)
cursor = conn.cursor()
# Create posts table
cursor.execute("""
CREATE TABLE IF NOT EXISTS posts (
id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT NOT NULL,
content TEXT NOT NULL,
created_at DATETIME NOT NULL,
updated_at DATETIME NOT NULL,
like_count INTEGER DEFAULT 0,
comment_count INTEGER DEFAULT 0
)
""")
# Create comments table
cursor.execute("""
CREATE TABLE IF NOT EXISTS comments (
id INTEGER PRIMARY KEY AUTOINCREMENT,
post_id INTEGER NOT NULL,
username TEXT NOT NULL,
content TEXT NOT NULL,
created_at DATETIME NOT NULL,
updated_at DATETIME NOT NULL,
FOREIGN KEY (post_id) REFERENCES posts (id)
)
""")
# Create likes table
cursor.execute("""
CREATE TABLE IF NOT EXISTS likes (
id INTEGER PRIMARY KEY AUTOINCREMENT,
post_id INTEGER NOT NULL,
username TEXT NOT NULL,
created_at DATETIME NOT NULL,
UNIQUE(post_id, username),
FOREIGN KEY (post_id) REFERENCES posts (id)
)
""")
conn.commit()
conn.close()
def get_post_like_count(post_id: int, conn: sqlite3.Connection) -> int:
"""Get the like count for a post"""
cursor = conn.execute("SELECT COUNT(*) FROM likes WHERE post_id = ?", (post_id,))
return cursor.fetchone()[0]
def get_post_comment_count(post_id: int, conn: sqlite3.Connection) -> int:
"""Get the comment count for a post"""
cursor = conn.execute("SELECT COUNT(*) FROM comments WHERE post_id = ?", (post_id,))
return cursor.fetchone()[0]
def update_post_counts(post_id: int, conn: sqlite3.Connection):
"""Update like_count and comment_count for a post"""
like_count = get_post_like_count(post_id, conn)
comment_count = get_post_comment_count(post_id, conn)
conn.execute(
"UPDATE posts SET like_count = ?, comment_count = ? WHERE id = ?",
(like_count, comment_count, post_id)
)
# Post operations
def get_all_posts(conn: sqlite3.Connection) -> List[sqlite3.Row]:
"""Get all posts ordered by creation date"""
cursor = conn.execute("""
SELECT id, username, content, created_at, updated_at, like_count, comment_count
FROM posts ORDER BY created_at DESC
""")
return cursor.fetchall()
def get_post_by_id(post_id: int, conn: sqlite3.Connection) -> Optional[sqlite3.Row]:
"""Get a post by its ID"""
cursor = conn.execute("""
SELECT id, username, content, created_at, updated_at, like_count, comment_count
FROM posts WHERE id = ?
""", (post_id,))
return cursor.fetchone()
def create_post(username: str, content: str, conn: sqlite3.Connection) -> int:
"""Create a new post and return its ID"""
now = datetime.utcnow().isoformat() + "Z"
cursor = conn.execute("""
INSERT INTO posts (username, content, created_at, updated_at, like_count, comment_count)
VALUES (?, ?, ?, ?, 0, 0)
""", (username, content, now, now))
return cursor.lastrowid
def update_post(post_id: int, username: str, content: str, conn: sqlite3.Connection):
"""Update an existing post"""
now = datetime.utcnow().isoformat() + "Z"
conn.execute("""
UPDATE posts SET username = ?, content = ?, updated_at = ? WHERE id = ?
""", (username, content, now, post_id))
def delete_post(post_id: int, conn: sqlite3.Connection):
"""Delete a post and its related comments and likes"""
# Delete related likes and comments first
conn.execute("DELETE FROM likes WHERE post_id = ?", (post_id,))
conn.execute("DELETE FROM comments WHERE post_id = ?", (post_id,))
# Delete the post
conn.execute("DELETE FROM posts WHERE id = ?", (post_id,))
def post_exists(post_id: int, conn: sqlite3.Connection) -> bool:
"""Check if a post exists"""
cursor = conn.execute("SELECT id FROM posts WHERE id = ?", (post_id,))
return cursor.fetchone() is not None
# Comment operations
def get_comments_by_post_id(post_id: int, conn: sqlite3.Connection) -> List[sqlite3.Row]:
"""Get all comments for a post"""
cursor = conn.execute("""
SELECT id, post_id, username, content, created_at, updated_at
FROM comments WHERE post_id = ? ORDER BY created_at DESC
""", (post_id,))
return cursor.fetchall()
def get_comment_by_id(comment_id: int, post_id: int, conn: sqlite3.Connection) -> Optional[sqlite3.Row]:
"""Get a comment by its ID and post ID"""
cursor = conn.execute("""
SELECT id, post_id, username, content, created_at, updated_at
FROM comments WHERE id = ? AND post_id = ?
""", (comment_id, post_id))
return cursor.fetchone()
def create_comment(post_id: int, username: str, content: str, conn: sqlite3.Connection) -> int:
"""Create a new comment and return its ID"""
now = datetime.utcnow().isoformat() + "Z"
cursor = conn.execute("""
INSERT INTO comments (post_id, username, content, created_at, updated_at)
VALUES (?, ?, ?, ?, ?)
""", (post_id, username, content, now, now))
return cursor.lastrowid
def update_comment(comment_id: int, post_id: int, username: str, content: str, conn: sqlite3.Connection):
"""Update an existing comment"""
now = datetime.utcnow().isoformat() + "Z"
conn.execute("""
UPDATE comments SET username = ?, content = ?, updated_at = ? WHERE id = ? AND post_id = ?
""", (username, content, now, comment_id, post_id))
def delete_comment(comment_id: int, post_id: int, conn: sqlite3.Connection):
"""Delete a comment"""
conn.execute("DELETE FROM comments WHERE id = ? AND post_id = ?", (comment_id, post_id))
def comment_exists(comment_id: int, post_id: int, conn: sqlite3.Connection) -> bool:
"""Check if a comment exists"""
cursor = conn.execute("SELECT id FROM comments WHERE id = ? AND post_id = ?", (comment_id, post_id))
return cursor.fetchone() is not None
# Like operations
def get_like(post_id: int, username: str, conn: sqlite3.Connection) -> Optional[sqlite3.Row]:
"""Get a like by post ID and username"""
cursor = conn.execute("""
SELECT id, post_id, username, created_at
FROM likes WHERE post_id = ? AND username = ?
""", (post_id, username))
return cursor.fetchone()
def create_like(post_id: int, username: str, conn: sqlite3.Connection) -> int:
"""Create a new like and return its ID"""
now = datetime.utcnow().isoformat() + "Z"
cursor = conn.execute("""
INSERT INTO likes (post_id, username, created_at)
VALUES (?, ?, ?)
""", (post_id, username, now))
return cursor.lastrowid
def delete_like(post_id: int, username: str, conn: sqlite3.Connection):
"""Delete a like"""
conn.execute("DELETE FROM likes WHERE post_id = ? AND username = ?", (post_id, username))
def like_exists(post_id: int, username: str, conn: sqlite3.Connection) -> bool:
"""Check if a like exists"""
cursor = conn.execute("SELECT id FROM likes WHERE post_id = ? AND username = ?", (post_id, username))
return cursor.fetchone() is not None