API package refactor
This commit is contained in:
parent
a7cd86962e
commit
bc6b57bc54
14 changed files with 474 additions and 416 deletions
|
|
@ -1,44 +0,0 @@
|
|||
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
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue