Planner/core/polls.go
Manuel Forcén Muñoz bc6b57bc54 API package refactor
2025-02-12 19:29:00 +01:00

43 lines
701 B
Go

package core
import (
"fmt"
"strings"
"gorm.io/gorm"
)
func GetPoll(orm *gorm.DB, user User, id uint) (*Poll, error) {
var poll Poll = Poll{
ID: id,
}
if result := orm.Take(&poll); result.Error != nil {
return nil, result.Error
}
fmt.Printf("%+v\n", poll.PlanID)
return &poll, nil
}
func (p *Poll) SetVote(orm *gorm.DB, user User, option string) error {
found := false
options := strings.Split(p.Options, ",")
for _, opt := range options {
if opt == option {
found = true
break
}
}
if !found {
return ErrInvalidOption
}
if res := orm.Create(Vote{PollID: p.ID, UsernameID: user.Username, Value: option}); res.Error != nil {
return res.Error
}
return nil
}