kylelhk / Pictionary

CITS5505 Agile Web Development - Group Project
MIT License
1 stars 0 forks source link

Refactor database queries for Home page and Gallery page #66

Open trieuH opened 2 months ago

trieuH commented 2 months ago

i was wondering if we could make the logic more easier with with something like one below. The one below is not tested and is just the idea.

 drawings = Drawing.query.all()

    results = []
    for drawing in drawings:
    ### LOGIC FOR STATUS
        if drawing.creator_id == current_user.id:
            status = "My Creation"
        else:
            guess = Guess.query.filter_by(drawing_id=drawing.id, guesser_id=current_user.id).first()
            status = "New"  # default status
            if guess:
                status = "Guessed Correctly" if guess.is_correct else "Guessed Incorrectly"

        results.append({
            "drawing_id": drawing.id,
            "creator": drawing.creator.username,
            "category": drawing.word.category,
            "status": status,
            "date_created": drawing.created_at.strftime("%Y-%m-%d %H:%M"),
        })

    return jsonify(results)

if we have time to refactor, we can consider above. otherwise, just leaving here for discussion

_Originally posted by @iheathers in https://github.com/kylelhk/Pictionary/pull/59#discussion_r1601878524_

trieuH commented 2 months ago

This approach is definitely easier to understand, however since it's fetching all of the drawings, it may be less efficient for the Home page where only new drawings need to be returned. It would be great to use on the /get-gallery-data endpoint to populate the Gallery page though. We can see if this style of query can be adapted for the Home page as well.