CRUD

In this class I worked to create a Binary Quiz that met full CRUD fucntionality. In realation to my quiz this meant that a quiz score and attempt could create, read, uptate, and delete.

CRUD functionality on my feature: LINK to quiz class _CRUD(Resource):

    def post(self):
        """
        Create a new group.
        """
        # current_user = g.current_user
        # Obtain the request data sent by the RESTful client API
        data = request.get_json()
        # Create a new group object using the data from the request
        chat = quizgrading(data['quizgrade'], data['attempt'], data['id'], data['username'])
        # Save the chat object using the Object Relational Mapper (ORM) method defined in the model
        chat.create()
        # Return response to the client in JSON format, converting Python dictionaries to JSON format
        return jsonify(chat.read())
    
    def get(self):
        chats = quizgrading.query.all()
        allChats = []
        for i in range(len(chats)):
            allChats.append(chats[i].read())

        # Return a JSON restful response to the client
        return jsonify(allChats)

    def put(self):
        # Obtain the current user
        # Obtain the request data
        data = request.get_json()
        # Find the current post from the database table(s)
        post = quizgrading.query.get(data['id'])
        # Update the post
        post._quizgrade = data['quizgrade']
        post._attempt = data['attempt']
        post._user_id = data['user_id']
        post._username = data['username']
        # Save the post
        post.update()
        # Return response
        return jsonify(post.read())

    def delete(self):
        # Obtain the request data
        data = request.get_json()
        # Find the current post from the database table(s)
        post = quizgrading.query.get(data['id'])
        # Delete the post using the ORM method defined in the model
        post.delete()
        # Return response
        return jsonify({"message": "Post deleted"})