Skip to content
Snippets Groups Projects
Commit bf9b77b5 authored by fcrisafulli-dev's avatar fcrisafulli-dev
Browse files

minor db updates

parent 2f30dbde
No related branches found
No related tags found
No related merge requests found
No preview for this file type
# Python 3 server example
from http.server import BaseHTTPRequestHandler, HTTPServer
import time
hostName = "localhost"
serverPort = 8080
def generate_content(path):
return f"""
<html>
<head></head>
<body>
<p>'{path}' was requested</p>
</body>
</html>
"""
class MyServer(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
self.wfile.write(bytes(generate_content(self.path),"utf-8"))
if __name__ == "__main__":
webServer = HTTPServer((hostName, serverPort), MyServer)
print("Server started on http://%s:%s" % (hostName, serverPort))
try:
webServer.serve_forever()
except KeyboardInterrupt:
pass
webServer.server_close()
print("Server stopped.")
No preview for this file type
{"Users": 0, "Collections": 4}
\ No newline at end of file
{"Users": 0, "Collections": 3}
\ No newline at end of file
......@@ -20,7 +20,7 @@ class CrisisEventsDatabase:
"""
raise "Not Implemented"
def get_collection(collection_id: int, user_id: int):
def get_collection(self, collection_id:int, user_id: int):
"""
Returns the collection data if the `user_id` matches in the corresponding `collection_id` entry.
......@@ -31,7 +31,7 @@ class CrisisEventsDatabase:
"""
raise "Not Implemented"
def set_collection(collection_id: int, authenticated_user_id: int, collection_data) -> bool:
def set_collection(self,collection_id:int, user_id: int, collection_json:str, summary:str, name:str) -> bool:
"""
Returns the collection data if the `authenticated_user_id` matches in the corresponding `collection_id` entry.
......
No preview for this file type
......@@ -60,8 +60,16 @@ class SqliteDatabaseAPI(database_api.CrisisEventsDatabase):
connection, cur = self._connect()
cur.execute("SELECT * FROM event_collections WHERE collection_id == ? AND owner_id == ?;", (collection_id,user_id,))
result = cur.fetchone()
json_result = {
"collection_id":result[0],
"user_id":result[1],
"collection_data":result[2],
"collection_summary":result[3],
"collection_name":result[4],
}
self._disconnect(connection, cur)
return result
return json_result
def set_collection(self,collection_id:int, user_id: int, collection_json:str, summary:str, name:str):
connection, cur = self._connect()
......
......@@ -10,14 +10,14 @@ database:CrisisEventsDatabase = None
def database_debug_view():
db_html = ""
for collection in database.get_sample_of_collections():
db_html += f"<br/><code>{collection}<code/>"
db_html += f"<br/><code>{collection}</code>"
return f"""
<html>
<head></head>
<body>
<p>This service uses {database.get_info()} as its database</p>
<p>Warning: if there are any '<' or '>' or '/' or '\\' in the database: this will break! </p>
<br/>
<code>UserID, CollectionID, CollectionData, CollectionSummary, CollectionName</code>
{db_html}
</body>
</html>
......@@ -28,10 +28,13 @@ def database_debug_view():
def database_service():
if request.method == 'POST':
print(request.json)
result = ''
try:
if request.json["command"] == "create_collection":
print("Creating collection")
command = request.json["command"]
if command == "create_collection":
database.create_collection(0,request.json["data"]["collection_name"])
elif command == "get_collection":
result = str(database.get_collection(0,0))
else:
print("Invalid command")
abort(400)
......@@ -39,7 +42,7 @@ def database_service():
print("Got poorly formatted request")
abort(400)
return '', 200
return result, 200
elif request.method == 'GET':
return database_debug_view(), 200
else:
......
import requests
import sys
# Specify the API URL we want to send our JSON to
url = 'http://127.0.0.1:5000/database_service'
# Specify the appropriate header for the POST request
headers = {'Content-type': 'application/json'}
# Specify the JSON data we want to send
data = '{"command": "create_collection", "data": {"collection_name":"SENT FROM POST title"}}'
data = '{"command": "get_collection", "data": {"collection_name":"SENT FROM POST title"}}'
response = requests.post(url, headers=headers, data=data)
print(response.text,response.status_code)
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment