|
1 | | -import psycopg2 |
2 | 1 | from psycopg2.extras import RealDictCursor |
3 | 2 |
|
4 | 3 |
|
5 | 4 | class UserRepository: |
6 | | - def __init__(self, db_url): |
7 | | - self.db_url = db_url |
8 | | - |
9 | | - def get_connection(self): |
10 | | - return psycopg2.connect(self.db_url) |
| 5 | + def __init__(self, conn): |
| 6 | + self.conn = conn |
11 | 7 |
|
12 | 8 | def get_content(self): |
13 | | - with self.get_connection() as conn: |
14 | | - with conn.cursor(cursor_factory=RealDictCursor) as cur: |
15 | | - cur.execute("SELECT * FROM users") |
16 | | - return cur.fetchall() |
| 9 | + with self.conn.cursor(cursor_factory=RealDictCursor) as cur: |
| 10 | + cur.execute("SELECT * FROM users") |
| 11 | + return cur.fetchall() |
| 12 | + |
| 13 | + def get_by_term(self, search_term=''): |
| 14 | + with self.conn.cursor(cursor_factory=RealDictCursor) as cur: |
| 15 | + cur.execute("SELECT * FROM users WHERE name ILIKE %s", (f'%{search_term}%',)) |
| 16 | + return cur.fetchall() |
17 | 17 |
|
18 | 18 | def find(self, id): |
19 | | - with self.get_connection() as conn: |
20 | | - with conn.cursor(cursor_factory=RealDictCursor) as cur: |
21 | | - cur.execute("SELECT * FROM users WHERE id = %s", (id,)) |
22 | | - return cur.fetchone() |
| 19 | + with self.conn.cursor(cursor_factory=RealDictCursor) as cur: |
| 20 | + cur.execute("SELECT * FROM users WHERE id = %s", (id,)) |
| 21 | + return cur.fetchone() |
23 | 22 |
|
24 | 23 | def save(self, user_data): |
25 | | - with self.get_connection() as conn: |
26 | | - with conn.cursor() as cur: |
27 | | - if 'id' not in user_data: |
28 | | - # New user |
29 | | - cur.execute( |
30 | | - "INSERT INTO users (name, email) VALUES (%s, %s) RETURNING id", |
31 | | - (user_data['name'], user_data['email']) |
32 | | - ) |
33 | | - user_data['id'] = cur.fetchone()[0] |
34 | | - else: |
35 | | - # Existing user |
36 | | - cur.execute( |
37 | | - "UPDATE users SET name = %s, email = %s WHERE id = %s", |
38 | | - (user_data['name'], user_data['email'], user_data['id']) |
39 | | - ) |
40 | | - conn.commit() |
| 24 | + if 'id' not in user_data: |
| 25 | + id = self._create(user_data) |
| 26 | + else: |
| 27 | + id = self._update(user_data) |
| 28 | + return id |
| 29 | + |
| 30 | + |
| 31 | + def _update(self, user_data): |
| 32 | + with self.conn.cursor() as cur: |
| 33 | + cur.execute( |
| 34 | + "UPDATE users SET name = %s, email = %s WHERE id = %s", |
| 35 | + (user_data['name'], user_data['email'], user_data['id']) |
| 36 | + ) |
| 37 | + self.conn.commit() |
41 | 38 | return user_data['id'] |
42 | 39 |
|
| 40 | + |
| 41 | + def _create(self, user_data): |
| 42 | + with self.conn.cursor() as cur: |
| 43 | + cur.execute( |
| 44 | + "INSERT INTO users (name, email) VALUES (%s, %s) RETURNING id", |
| 45 | + (user_data['name'], user_data['email']) |
| 46 | + ) |
| 47 | + user_data['id'] = cur.fetchone()[0] |
| 48 | + self.conn.commit() |
| 49 | + return user_data['id'] |
| 50 | + |
| 51 | + |
43 | 52 | def destroy(self, id): |
44 | | - with self.get_connection() as conn: |
45 | | - with conn.cursor() as cur: |
46 | | - cur.execute("DELETE FROM users WHERE id = %s", (id,)) |
47 | | - conn.commit() |
| 53 | + with self.conn.cursor() as cur: |
| 54 | + cur.execute("DELETE FROM users WHERE id = %s", (id,)) |
| 55 | + self.conn.commit() |
0 commit comments