github.com/DapperCollectives/CAST/backend@v0.0.0-20230921221157-1350c8be7c96/main/models/proposal_results.go (about)

     1  package models
     2  
     3  import (
     4  	"time"
     5  
     6  	s "github.com/DapperCollectives/CAST/backend/main/shared"
     7  	"github.com/georgysavva/scany/pgxscan"
     8  )
     9  
    10  type ProposalResults struct {
    11  	Proposal_id       int                `json:"proposalId" validate:"required"`
    12  	Results       	  map[string]int     `json:"results" validate:"required"`
    13  	Results_float 	   map[string]float64  `json:"resultsFloat" validate:"required"`
    14  	Updated_at        time.Time          `json:"updatedAt" validate:"required"`
    15  	Cid           	  *string            `json:"cid,omitempty"`
    16  	Achievements_done bool               `json:"achievementsDone"`
    17  }
    18  
    19  func NewProposalResults(id int, choices []s.Choice) *ProposalResults {
    20  	p := new(ProposalResults)
    21  	p.Results = make(map[string]int)
    22  	p.Results_float = make(map[string]float64)
    23  
    24  	for _, choice := range choices {
    25  		p.Results[choice.Choice_text] = 0
    26  		p.Results_float[choice.Choice_text] = 0.0
    27  	}
    28  
    29  	p.Proposal_id = id
    30  	return p
    31  }
    32  
    33  func (r *ProposalResults) GetLatestProposalResultsById(db *s.Database) error {
    34  	return pgxscan.Get(db.Context, db.Conn, r,
    35  		`
    36  		SELECT * FROM proposal_results
    37  		WHERE proposal_id = $1
    38  		ORDER BY updated_at DESC
    39  		LIMIT 1
    40  		`, r.Proposal_id)
    41  }