-
Notifications
You must be signed in to change notification settings - Fork 0
/
user_base.py
176 lines (143 loc) · 4.76 KB
/
user_base.py
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
import os
import json
import requests
import psycopg2
class UserBase:
"""
Base interface implementation for API's to manage users.
"""
def __init__(self):
self.connection = psycopg2.connect(
host=os.getenv('POSTGRES_HOSTNAME'),
port=os.getenv('POSTGRES_PORT'),
user=os.getenv('POSTGRES_USERNAME'),
password=os.getenv('POSTGRES_PASSWORD'),
database=os.getenv('POSTGRES_DBNAME')
)
self.cursor = self.connection.cursor()
self.cursor.execute('''
CREATE TABLE IF NOT EXISTS users (
id SERIAL PRIMARY KEY,
name VARCHAR(64) NOT NULL UNIQUE,
display_name VARCHAR(64) NOT NULL
)
''')
# create a user
def create_user(self, request: str) -> str:
"""
:param request: A json string with the user details
{
"name" : "<user_name>",
"display_name" : "<display name>"
}
:return: A json string with the response {"id" : "<user_id>"}
Constraint:
* user name must be unique
* name can be max 64 characters
* display name can be max 64 characters
"""
try:
result = ""
json_request = json.loads(request)
self.cursor.execute('''
INSERT INTO users (name, display_name) VALUES (%s, %s) RETURNING id
''',
(json_request['name'], json_request['display_name']))
result = json.dumps({"id": self.cursor.fetchone()[0]})
return result
except Exception as e:
print(e)
def list_users(self) -> str:
"""
:return: A json list with the response
[
{
"name" : "<user_name>",
"display_name" : "<display name>",
"creation_time" : "<some date:time format>"
}
]
"""
self.cursor.execute('''
SELECT * FROM users
''')
result = self.cursor.fetchall()
return json.dumps(result)
# describe user
def describe_user(self, request: str) -> str:
"""
:param request: A json string with the user details
{
"id" : "<user_id>"
}
:return: A json string with the response
{
"name" : "<user_name>",
"description" : "<some description>",
"creation_time" : "<some date:time format>"
}
"""
try:
result = ""
json_request = json.loads(request)
self.cursor.execute('''
SELECT * FROM users WHERE id = %s
''',
(json_request['id'],))
result = json.dumps(self.cursor.fetchone())
return result
except Exception as e:
print(e)
# update user
def update_user(self, request: str) -> str:
"""
:param request: A json string with the user details
{
"id" : "<user_id>",
"user" : {
"name" : "<user_name>",
"display_name" : "<display name>"
}
}
:return:
Constraint:
* user name cannot be updated
* name can be max 64 characters
* display name can be max 128 characters
"""
try:
result = ""
json_request = json.loads(request)
self.cursor.execute('''
UPDATE users SET display_name = %s WHERE id = %s
''',
(json_request['display_name'], json_request['id']))
result = json.dumps({"id": self.cursor.fetchone()[0]})
return result
except Exception as e:
print(e)
def get_user_teams(self, request: str) -> str:
"""
:param request:
{
"id" : "<user_id>"
}
:return: A json list with the response.
[
{
"name" : "<team_name>",
"description" : "<some description>",
"creation_time" : "<some date:time format>"
}
]
"""
try:
result = ""
json_request = json.loads(request)
self.cursor.execute('''
SELECT * FROM users JOIN teams ON users.id = teams.user_id WHERE users.id = %s
''', (json_request['id'],))
result = json.dumps(self.cursor.fetchone())
return result
except Exception as e:
print(e)