Open MagnusLii opened 10 months ago
This code perform the deletion of the vote when it is run. When using SQLAlchemy it is not needed to use SQL query. It uses Object-Relational Mapping (ORM).
from flask import Flask, request, jsonify
from models import db, Vote
@app.route('/delete_vote/<int:vote_id>', methods=['DELETE'])
def delete_vote(vote_id):
vote = Vote.query.get(vote_id) # Get the vote by ID
if not vote:
return jsonify({'error': 'Vote not found'}), 404 # Vote not found
try:
db.session.delete(vote) # Delete the vote
db.session.commit() # Commit the changes
return jsonify({'message': 'Vote deleted successfully'}), 200
except Exception as e:
db.session.rollback() # Rollback in case of any error
return jsonify({'error': str(e)}), 500
The DB layout has changed thus this needs to be updated at some point.
Specific Vote Data Deletion Function
Goal
The goal is to create a function that handles the removal of all data associated with a specified vote from the database. It takes a vote ID, constructs and returns a query string that when sent to the server removes all information related to a vote.
Arguments
Returns
Function Name:
deleteSpecificVoteData