commit
9f70d855cc
2 changed files with 113 additions and 19 deletions
|
@ -46,6 +46,30 @@ class Entry:
|
|||
|
||||
return res
|
||||
|
||||
@dataclass
|
||||
class User:
|
||||
id: str
|
||||
username: str
|
||||
password_salt: str
|
||||
password_hash: str
|
||||
|
||||
def validate(self, only_populated_fields=False) -> Tuple[bool, str]:
|
||||
if (self.username == "" or self.username is None) and not only_populated_fields:
|
||||
return False, "name cannot be empty"
|
||||
|
||||
return True, ""
|
||||
|
||||
def as_dict(self) -> Dict:
|
||||
res = {}
|
||||
|
||||
res["id"] = self.id
|
||||
res["username"] = self.username
|
||||
res["password_salt"] = self.password_salt
|
||||
res["password_hash"] = self.password_hash
|
||||
|
||||
return res
|
||||
|
||||
|
||||
|
||||
class DB:
|
||||
conn: sqlite3.Connection
|
||||
|
@ -53,27 +77,53 @@ class DB:
|
|||
def __init__(self, filename: str):
|
||||
self.conn = sqlite3.connect(filename)
|
||||
|
||||
def _setup(self, filename: str):
|
||||
def _setup(self):
|
||||
cursor = self.conn.cursor()
|
||||
|
||||
cursor.execute('''
|
||||
CREATE TABLE IF NOT EXISTS entries
|
||||
([ID] INTEGER PRIMARY KEY, [title] TEXT, [latitude] FLOAT, [longitude] FLOAT, [votes] INTEGER, [image_url], STRING)
|
||||
''')
|
||||
cursor.execute('''
|
||||
CREATE TABLE IF NOT EXISTS users
|
||||
([ID] INTEGER PRIMARY KEY, [username] TEXT, [password_salt] TEXT, [password_hash] TEXT)
|
||||
''')
|
||||
cursor.execute('''
|
||||
CREATE TABLE IF NOT EXISTS places_visited
|
||||
([ID] INTEGER PRIMARY KEY, [userID] INTEGER FOREIGN KEY, [entryID] INTEGER FOREIGN KEY)
|
||||
''')
|
||||
|
||||
cursor.commit()
|
||||
|
||||
def getEntries(self):
|
||||
def getAllEntries(self):
|
||||
arrayEntries = []
|
||||
cursor = self.conn.cursor()
|
||||
cursor.execute('SELECT * FROM entries')
|
||||
|
||||
myresult = cursor.fetchall()
|
||||
result = cursor.fetchall()
|
||||
|
||||
for x in myresult:
|
||||
print(x)
|
||||
for x in result:
|
||||
entry = Entry(x[0], x[1], x[2], x[3], x[4], x[5])
|
||||
arrayEntries.append(entry)
|
||||
return arrayEntries
|
||||
|
||||
def addEntry(self, title, latitude, longitude, votes, image_url):
|
||||
def getEntry(self, ID):
|
||||
cursor = self.conn.cursor()
|
||||
cursor.execute('SELECT * FROM entries WHERE ID = ?', ID)
|
||||
|
||||
result = cursor.fetchall()
|
||||
entry = Entry(result[0], result[1], result[2], result[3], result[4], result[5])
|
||||
|
||||
return entry
|
||||
|
||||
def addEntry(self, Entry):
|
||||
title = Entry.name
|
||||
latitude = Entry.location_lat
|
||||
longitude = Entry.location_long
|
||||
votes = Entry.votes
|
||||
image_url = Entry.image_url
|
||||
insertArray = [title, latitude, longitude, votes, image_url]
|
||||
|
||||
cursor = self.conn.cursor()
|
||||
cursor.execute('INSERT INTO entries (title, latitude, longitude, votes, image_url) VALUES (?, ?, ?, ?, ?);', insertArray)
|
||||
cursor.commit()
|
||||
|
@ -83,8 +133,56 @@ class DB:
|
|||
cursor.execute('DELETE FROM entries WHERE ID = ?', ID)
|
||||
cursor.commit()
|
||||
|
||||
def updateEntry(self, ID, title, latitude, longitude, votes, image_url):
|
||||
def updateEntry(self, Entry):
|
||||
ID = Entry.id
|
||||
title = Entry.name
|
||||
latitude = Entry.location_lat
|
||||
longitude = Entry.location_long
|
||||
votes = Entry.votes
|
||||
image_url = Entry.image_url
|
||||
updateArray = [ID, title, latitude, longitude, votes, image_url]
|
||||
|
||||
cursor = self.conn.cursor()
|
||||
cursor.execute('UPDATE entries SET title = ?, latitude = ?, longitude = ?, votes = ?, image_url = ?;', updateArray)
|
||||
cursor.commit()
|
||||
|
||||
def getUser(self, ID):
|
||||
cursor = self.conn.cursor()
|
||||
cursor.execute('SELECT * FROM users WHERE ID = ?', ID)
|
||||
|
||||
result = cursor.fetchall()
|
||||
user = User(result[0], result[1], result[2], result[3])
|
||||
|
||||
return user
|
||||
|
||||
def addUser(self, User):
|
||||
username = User.name
|
||||
password_salt = User.password_salt
|
||||
password_hash = User.password_hash
|
||||
insertArray = [username, password_salt, password_hash]
|
||||
|
||||
cursor = self.conn.cursor()
|
||||
cursor.execute('INSERT INTO user (username, password_salt, password_hash) VALUES (?, ?, ?);', insertArray)
|
||||
cursor.commit()
|
||||
|
||||
def deleteUser(self, ID):
|
||||
cursor = self.conn.cursor()
|
||||
cursor.execute('DELETE FROM users WHERE ID = ?', ID)
|
||||
cursor.commit()
|
||||
|
||||
def getUserLocations(self, ID):
|
||||
array_locations = []
|
||||
cursor = self.conn.cursor()
|
||||
cursor.execute('SELECT * FROM places_visited WHERE userID = ?', ID)
|
||||
|
||||
result = cursor.fetchall()
|
||||
|
||||
for x in result:
|
||||
array_locations.append(x[2])
|
||||
return array_locations
|
||||
#should return a list of IDs of places the user has visited
|
||||
|
||||
def deleteUserLocation(self, ID):
|
||||
cursor = self.conn.cursor()
|
||||
cursor.execute('DELETE FROM places_visited WHERE ID = ?', ID)
|
||||
cursor.commit()
|
|
@ -18,20 +18,17 @@ class Endpoints:
|
|||
app.add_url_rule(paths.CREATE_ENTRY, view_func=self.create_entry, methods=["POST"])
|
||||
|
||||
def list_entries(self):
|
||||
# TODO: populate from databaase
|
||||
entries = self.db.getAllEntries()
|
||||
|
||||
a = db.Entry("203fc6a0-9587-41a4-9862-e1b72039b98b", "Birmingham Duck Pond", -1.2345, 33.4567, 0, None)
|
||||
b = db.Entry("b140e048-ea2c-4827-b670-ef41ba48c56d", "Northwich Duck Pond", -3.2345, 25.4567, 0, None)
|
||||
for i in range(len(entries)):
|
||||
entries[i] = entries[i].as_dict()
|
||||
|
||||
return flask.jsonify([a.as_dict(), b.as_dict()])
|
||||
return flask.jsonify(entries)
|
||||
|
||||
def get_entry(self, entry_id: str):
|
||||
# TODO: Fetch from database
|
||||
entry = self.get_entry(entry_id)
|
||||
|
||||
return flask.jsonify({
|
||||
"id": entry_id,
|
||||
"TODO": "TODO"
|
||||
})
|
||||
return flask.jsonify(entry.as_dict())
|
||||
|
||||
def update_entry(self):
|
||||
return "", 204
|
||||
|
@ -58,9 +55,8 @@ class Endpoints:
|
|||
if not validation_result:
|
||||
return flask.abort(400, error_text)
|
||||
|
||||
# TODO: store in database
|
||||
# TODO: Form responses
|
||||
self.addEntry(new_entry)
|
||||
|
||||
return flask.jsonify({
|
||||
"id": uuid.uuidv4()
|
||||
"id": new_entry.id
|
||||
})
|
||||
|
|
Reference in a new issue