diff --git a/app.py b/app.py index 41ecfda..106b539 100644 --- a/app.py +++ b/app.py @@ -39,14 +39,25 @@ def get_all_quotes() -> list[Quote]: return quotes -def add_quote(new_quote: Quote): - db_connexion = get_database_connection() - db_connexion.execute( +def add_quote(new_quote: Quote) -> bool: + db_connection = get_database_connection() + + matching_quote = db_connection.execute( + "SELECT author, content, timestamp FROM quotes WHERE author = ? AND content = ?", + (new_quote.author, new_quote.content) + ).fetchall() + + if matching_quote: + # There is already an author and a quote with the same content, let's not do duplicates... + return False + + db_connection.execute( "INSERT INTO quotes (author, content) VALUES (?, ?)", (new_quote.author, new_quote.content) ) - db_connexion.commit() - db_connexion.close() + db_connection.commit() + db_connection.close() + return True @app.route('/', methods=["POST", "GET"]) @@ -62,8 +73,11 @@ def index(): else: quote = Quote(form["username"], form["message"]) if quote.proof_of_work_is_valid(form["proof-of-work"]): - add_quote(Quote(form["username"], form["message"])) - message = "Your quote was added successfully" + if add_quote(Quote(form["username"], form["message"])): + message = "Your quote was added successfully" + else: + message = "Couldn't add your quote. Is it a duplicate?" + response_code = 400 else: message = "You didn't do the work, silly!" response_code = 400