Skip to content

Commit 8cdefa8

Browse files
committed
Added logic to save/get pastes.
1 parent 1d47ace commit 8cdefa8

File tree

1 file changed

+113
-0
lines changed

1 file changed

+113
-0
lines changed

app.py

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,23 @@
22
from flask_cors import CORS
33
import sqlite3
44
import os
5+
import time
6+
import uuid
7+
8+
CURRENT_DIRECTORY = os.path.dirname(os.path.realpath(__file__))
9+
DATABASE_FILE_NAME = '%s/pastebin.db' % CURRENT_DIRECTORY
10+
11+
CREATE_PASTEBIN_TABLE_QUERY = '''
12+
CREATE TABLE pastebin (
13+
id VARCHAR(128) NOT NULL,
14+
author VARCHAR(128) NOT NULL,
15+
text LONGTEXT NOT NULL,
16+
language VARCHAR(128) NOT NULL,
17+
type VARCHAR(128) NOT NULL,
18+
date DATETIME NOT NULL,
19+
PRIMARY KEY (id)
20+
);
21+
'''
522

623
app = Flask(__name__)
724
CORS(app)
@@ -15,5 +32,101 @@ def hello():
1532
def ping():
1633
return jsonify({ 'status': 'healthy', 'service': 'pastebin' })
1734

35+
@app.route('/v1/pastebin/save', methods = ['POST'])
36+
def save_paste():
37+
try:
38+
data = request.get_json()
39+
40+
id = str(uuid.uuid4())
41+
author = data['author']
42+
text = data['text']
43+
language = data['language']
44+
type = data['type']
45+
current_time = time.strftime('%Y-%m-%d %H:%M:%S')
46+
47+
connection = sqlite3.connect(DATABASE_FILE_NAME)
48+
cursor = connection.cursor()
49+
50+
cursor.execute('INSERT INTO pastebin VALUES(?, ?, ?, ?, ?, ?)', (id, author, text, language, type, current_time))
51+
52+
connection.commit()
53+
cursor.close()
54+
connection.close()
55+
56+
print('Created a new post: ({}, {}, {}, {}, {})'.format(id, text, language, type, current_time))
57+
58+
return jsonify({
59+
'status': 'success',
60+
'id': id
61+
})
62+
except Exception as e:
63+
return jsonify({
64+
'status': 'error',
65+
'message': 'Cannot save the new paste.',
66+
'error_message': str(e)
67+
})
68+
69+
@app.route('/v1/pastebin/<id>', methods = ['GET'])
70+
def get_paste(id):
71+
initialize_tables()
72+
73+
connection = sqlite3.connect(DATABASE_FILE_NAME)
74+
cursor = connection.cursor()
75+
76+
query = ('SELECT author, text, language, type, date FROM pastebin WHERE id = ?')
77+
cursor.execute(query, (id,))
78+
result = cursor.fetchall()
79+
80+
if len(result) != 1:
81+
cursor.close()
82+
connection.close()
83+
84+
return jsonify({
85+
'status': 'error',
86+
'message': 'No post can be found associated with the given id.'
87+
})
88+
89+
author, text, language, type, date, = result[0]
90+
cursor.close()
91+
connection.close()
92+
93+
return jsonify({
94+
'author': str(author),
95+
'text': text,
96+
'language': language,
97+
'type': type,
98+
'date': date
99+
})
100+
101+
def create_table():
102+
print('Creating a table!')
103+
connection = sqlite3.connect(DATABASE_FILE_NAME)
104+
cursor = connection.cursor()
105+
106+
cursor.execute(CREATE_PASTEBIN_TABLE_QUERY)
107+
108+
connection.commit()
109+
connection.close()
110+
111+
def insert_sample_data():
112+
print('Inserting sample data!')
113+
connection = sqlite3.connect(DATABASE_FILE_NAME)
114+
cursor = connection.cursor()
115+
116+
current_time = time.strftime('%Y-%m-%d %H:%M:%S')
117+
cursor.execute(('INSERT INTO pastebin VALUES(?, ?, ?, ?, ?, ?)'), ('4084cd36-905d-4a94-902d-1c499f345931', 'phamdann', 'print(5)', 'python', 'pastebin', current_time))
118+
cursor.execute(('INSERT INTO pastebin VALUES(?, ?, ?, ?, ?, ?)'), ('b6733a91-09c3-4ebc-80bc-79e03fd3b360', 'phamdann', 'print("Hello world!")', 'python', 'coderpad', current_time))
119+
120+
connection.commit()
121+
connection.close()
122+
123+
def initialize_tables():
124+
if os.path.exists(DATABASE_FILE_NAME):
125+
return
126+
127+
print('There is currently no database. Creating it now...')
128+
create_table()
129+
insert_sample_data()
130+
18131
if __name__ == '__main__':
19132
app.run(port=5000)

0 commit comments

Comments
 (0)