Init project with vote module
This commit is contained in:
commit
98f3c2aedc
11 changed files with 671 additions and 0 deletions
34
models/models.go
Normal file
34
models/models.go
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
package models
|
||||
|
||||
type User struct {
|
||||
Username string `gorm:"primaryKey" json:"username"`
|
||||
Password string `json:"password"`
|
||||
OwnedPlans []Plan `gorm:"foreignKey:Owner;references:Username" json:"-"`
|
||||
MemberPlans []Plan `gorm:"many2many:user_plans;" json:"-"`
|
||||
Votes []Vote `gorm:"foreignKey:UsernameID" json:"-"`
|
||||
}
|
||||
|
||||
// CREATE TABLE plans(id INTEGER PRIMARY KEY AUTOINCREMENT, name STRING, owner STRING, FOREIGN KEY(owner) REFERENCES users(username))
|
||||
// CREATE TABLE plan_user_relations(username STRING, plan INTEGER, PRIMARY KEY(username, plan), FOREIGN KEY username REFERENCES user(username), FOREIGN KEY plan REFERENCES plans(id))
|
||||
type Plan struct {
|
||||
ID uint `gorm:"primaryKey;autoIncrement:true" json:"id"`
|
||||
Name string `json:"name"`
|
||||
Owner string `json:"owner"`
|
||||
Members []User `gorm:"many2many:user_plans;" json:"-"`
|
||||
Polls []Poll `gorm:"foreignKey:PlanID;references:ID" json:"-"`
|
||||
}
|
||||
|
||||
// CREATE TABLE polls(id INTEGER PRIMARY KEY AUTOINCREMENT, plan INTEGER, name STRING, options JSON, FOREIGN KEY plan REFERENCES plans(id))
|
||||
type Poll struct {
|
||||
ID uint `gorm:"primaryKey;autoIncrement:true" json:"id"`
|
||||
PlanID uint `json:"-"`
|
||||
Options string `json:"options"`
|
||||
Votes []Vote `gorm:"foreignKey:PollID;references:ID" json:"-"`
|
||||
}
|
||||
|
||||
// CREATE TABLE votes(id INTEGER, poll INTEGER, user STRING, value JSON, FOREIGN KEY poll REFERENCES polls(id), FOREIGN KEY user REFERENCES user(username))
|
||||
type Vote struct {
|
||||
PollID uint `gorm:"primaryKey" json:"-"`
|
||||
UsernameID string `gorm:"primaryKey" json:"username_id"`
|
||||
Value string `json:"value"`
|
||||
}
|
||||
52
models/plans.go
Normal file
52
models/plans.go
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
package models
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"gorm.io/gorm"
|
||||
. "planner/errors"
|
||||
)
|
||||
|
||||
func GetPlan(orm *gorm.DB, user User, id uint) (*Plan, error) {
|
||||
var plan Plan = Plan{
|
||||
ID: id,
|
||||
}
|
||||
result := orm.Take(&plan)
|
||||
|
||||
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
|
||||
return nil, ErrNotFound
|
||||
} else if result.Error != nil {
|
||||
return nil, result.Error
|
||||
}
|
||||
|
||||
if plan.Owner == user.Username {
|
||||
return &plan, nil
|
||||
}
|
||||
|
||||
isMember, err := plan.IsMember(orm, &user)
|
||||
|
||||
if !isMember || err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &plan, nil
|
||||
}
|
||||
|
||||
func (p *Plan) GetAllUsers(orm *gorm.DB) ([]User, error) {
|
||||
var users []User
|
||||
err := orm.Model(p).Association("Members").Find(&users)
|
||||
return users, err
|
||||
}
|
||||
|
||||
func (p *Plan) IsMember(orm *gorm.DB, u *User) (bool, error) {
|
||||
var user User
|
||||
result := orm.
|
||||
Table("users u").
|
||||
Select("u.*").
|
||||
Joins("JOIN user_plans up on u.username=up.user_username AND up.plan_id = ?", p.ID).
|
||||
Where("u.username = ?", u.Username).
|
||||
Take(&user).Error
|
||||
if errors.Is(result, gorm.ErrRecordNotFound) {
|
||||
return false, ErrNotMember
|
||||
}
|
||||
return result == nil, result
|
||||
}
|
||||
44
models/polls.go
Normal file
44
models/polls.go
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
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
|
||||
}
|
||||
11
models/users.go
Normal file
11
models/users.go
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
package models
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func (u *User) GetPlans(orm *gorm.DB) ([]Plan, error) {
|
||||
var plans []Plan
|
||||
err := orm.Model(u).Association("MemberPlans").Find(&plans)
|
||||
return plans, err
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue