github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/examples/gno.land/r/x/nir1218_evaluation_proposal/category.gno (about)

     1  package evaluation
     2  
     3  import (
     4  	"gno.land/p/demo/avl"
     5  )
     6  
     7  type Category struct {
     8  	name        string
     9  	criteria    []string
    10  	status      string
    11  	votes       avl.Tree
    12  	tallyResult TallyResult
    13  }
    14  
    15  func NewCategory(name string, criteria []string) *Category {
    16  	tallyResult := TallyResult{}
    17  	tallyResult.results.Set(VoteYes, 0)
    18  	tallyResult.results.Set(VoteNo, 0)
    19  
    20  	c := &Category{
    21  		name:        name,
    22  		criteria:    criteria,
    23  		status:      "Proposed",
    24  		votes:       avl.Tree{},
    25  		tallyResult: tallyResult,
    26  	}
    27  	return c
    28  }
    29  
    30  func (c *Category) Approve() {
    31  	// TODO error handling
    32  	c.status = "Approved"
    33  }
    34  
    35  func (c Category) Status() string {
    36  	return c.status
    37  }
    38  
    39  func (c *Category) Tally() {
    40  	// TODO error handling
    41  	c.votes.Iterate("", "", func(address string, vote interface{}) bool {
    42  		v := vote.(Vote)
    43  		value, exists := c.tallyResult.results.Get(v.option)
    44  		if !exists {
    45  			return false
    46  		}
    47  		count := value.(int)
    48  		c.tallyResult.results.Set(v.option, count+1)
    49  		return true
    50  	})
    51  }