File size: 1,482 Bytes
20cccb6
 
 
0f77dec
20cccb6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
"""
crud.py

This module defines the operations for the Expressive TTS Arena project's database.
Since vote records are never updated or deleted, only functions to create and read votes are provided.
"""

# Third-Party Library Imports
from sqlalchemy.orm import Session

# Local Application Imports
from src.custom_types import VotingResults
from src.database.models import VoteResult


def create_vote(db: Session, vote_data: VotingResults) -> VoteResult:
    """
    Create a new vote record in the database based on the given VotingResults data.

    Args:
        db (Session): The SQLAlchemy database session.
        vote_data (VotingResults): The vote data to persist.

    Returns:
        VoteResult: The newly created vote record.
    """
    vote = VoteResult(
        comparison_type=vote_data["comparison_type"],
        winning_provider=vote_data["winning_provider"],
        winning_option=vote_data["winning_option"],
        option_a_provider=vote_data["option_a_provider"],
        option_b_provider=vote_data["option_b_provider"],
        option_a_generation_id=vote_data["option_a_generation_id"],
        option_b_generation_id=vote_data["option_b_generation_id"],
        voice_description=vote_data["voice_description"],
        text=vote_data["text"],
        is_custom_text=vote_data["is_custom_text"],
    )
    db.add(vote)
    try:
        db.commit()
    except Exception as e:
        db.rollback()
        raise e
    db.refresh(vote)
    return vote