Planner/models/polls.go
2024-11-09 19:35:44 +01:00

44 lines
728 B
Go

package models
import (
"fmt"
"strings"
"gorm.io/gorm"
"planner/errors"
)
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 errors.ErrInvalidOption
}
if res := orm.Create(Vote{PollID: p.ID, UsernameID: user.Username, Value: option}); res.Error != nil {
return res.Error
}
return nil
}