murphyslemon / Raspberry_Pi_Server

International Sensor Development Project: This project is a voting system that uses a raspberry pi server and multiple voting devices. The system is easy, secure, scalable, and supports anonymous and registered voting. This repository focuses on the Raspberry Pi server.
0 stars 3 forks source link

deleteSpecificVoteData function #11

Open MagnusLii opened 10 months ago

MagnusLii commented 10 months ago

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

Javimetro commented 9 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
MagnusLii commented 9 months ago

The DB layout has changed thus this needs to be updated at some point.