Simplified join with a global code
This commit is contained in:
parent
bc6b57bc54
commit
efd337b6ce
4 changed files with 93 additions and 77 deletions
|
|
@ -21,22 +21,11 @@ func createPlan(c *gin.Context) {
|
||||||
Name string `json:"name" form:"name"`
|
Name string `json:"name" form:"name"`
|
||||||
}
|
}
|
||||||
c.Bind(&plan_req)
|
c.Bind(&plan_req)
|
||||||
var plan Plan = Plan{
|
|
||||||
Name: plan_req.Name,
|
|
||||||
Owner: u.Username,
|
|
||||||
Members: []Member{
|
|
||||||
{
|
|
||||||
UserID: u.Username,
|
|
||||||
Type: "user",
|
|
||||||
Status: "ready",
|
|
||||||
JoinCode: "owner",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
result := db.Create(&plan)
|
|
||||||
|
|
||||||
if result.Error != nil {
|
_, err := CreatePlan(db, u, plan_req.Name)
|
||||||
c.JSON(http.StatusInternalServerError, result.Error)
|
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, err)
|
||||||
} else {
|
} else {
|
||||||
c.JSON(http.StatusOK, plan_req)
|
c.JSON(http.StatusOK, plan_req)
|
||||||
}
|
}
|
||||||
|
|
@ -69,7 +58,7 @@ func getPlan(c *gin.Context) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
plan, err := GetPlan(db, *user, params.Id)
|
plan, err := user.GetPlan(db, params.Id)
|
||||||
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
c.JSON(http.StatusOK, plan)
|
c.JSON(http.StatusOK, plan)
|
||||||
|
|
@ -94,7 +83,7 @@ func listPlanMembers(c *gin.Context) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
plan, err := GetPlan(db, *user, uint(plan_id))
|
plan, err := user.GetPlan(db, uint(plan_id))
|
||||||
members, err := plan.GetAllUsers(db)
|
members, err := plan.GetAllUsers(db)
|
||||||
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
|
|
@ -120,15 +109,17 @@ func addPlanMember(c *gin.Context) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
plan, err := GetPlan(db, *user, uint(plan_id))
|
plan, err := user.GetPlan(db, uint(plan_id))
|
||||||
|
|
||||||
var new_member Member
|
var new_member struct {
|
||||||
|
Name string `json:"name" form:"name"`
|
||||||
|
}
|
||||||
if err := c.ShouldBind(&new_member); err != nil {
|
if err := c.ShouldBind(&new_member); err != nil {
|
||||||
c.String(http.StatusInternalServerError, err.Error())
|
c.String(http.StatusInternalServerError, err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = plan.AddMember(db, &new_member)
|
err = plan.AddMember(db, &Member{Name: new_member.Name, Type: "non-user"})
|
||||||
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
c.JSON(http.StatusOK, new_member)
|
c.JSON(http.StatusOK, new_member)
|
||||||
|
|
@ -144,6 +135,7 @@ func addPlanMember(c *gin.Context) {
|
||||||
func joinPlan(c *gin.Context) {
|
func joinPlan(c *gin.Context) {
|
||||||
user := extractUser(db, c)
|
user := extractUser(db, c)
|
||||||
if user == nil {
|
if user == nil {
|
||||||
|
c.Status(http.StatusUnauthorized)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -152,35 +144,34 @@ func joinPlan(c *gin.Context) {
|
||||||
c.Status(http.StatusBadRequest)
|
c.Status(http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
plan, err := GetPlan(db, *user, uint(plan_id))
|
plan, err := GetPlan(db, uint(plan_id))
|
||||||
if err != nil {
|
if err != nil || plan == nil {
|
||||||
c.Status(http.StatusInternalServerError)
|
c.Status(http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
member, err := plan.GetMember(db, user)
|
|
||||||
var query struct {
|
var query struct {
|
||||||
JoinCode string `fdb:"code"`
|
JoinCode string `form:"code"`
|
||||||
}
|
}
|
||||||
if c.ShouldBindQuery(&query) != nil || query.JoinCode == "" {
|
if c.ShouldBindQuery(&query) != nil || query.JoinCode == "" {
|
||||||
c.Status(http.StatusBadRequest)
|
c.Status(http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if member.Status != "pending" {
|
if query.JoinCode != plan.JoinCode {
|
||||||
c.String(http.StatusConflict, "User is not pending")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if member.JoinCode == query.JoinCode {
|
|
||||||
member.Status = "ready"
|
|
||||||
err := db.Model(&member).Update("status", member.Status).Error
|
|
||||||
if err != nil {
|
|
||||||
c.String(http.StatusInternalServerError, err.Error())
|
|
||||||
return
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
c.String(http.StatusConflict, "Invalid join code")
|
c.String(http.StatusConflict, "Invalid join code")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
is_member, err := plan.IsMember(db, user)
|
||||||
|
if err != nil {
|
||||||
|
c.Status(http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if is_member {
|
||||||
|
c.String(http.StatusConflict, "User already a member")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
plan.AddMember(db, &Member{Type: "user", UserID: user.Username})
|
||||||
c.Status(http.StatusOK)
|
c.Status(http.StatusOK)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -198,7 +189,7 @@ func createPlanPoll(c *gin.Context) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
plan, err := GetPlan(db, *user, params.Id)
|
plan, err := user.GetPlan(db, params.Id)
|
||||||
|
|
||||||
if errors.Is(err, ErrNotFound) {
|
if errors.Is(err, ErrNotFound) {
|
||||||
c.Status(http.StatusNotFound)
|
c.Status(http.StatusNotFound)
|
||||||
|
|
|
||||||
|
|
@ -9,15 +9,13 @@ type User struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type Member struct {
|
type Member struct {
|
||||||
ID uint `gorm:"primaryKey;autoIncrement:true" json:"-"`
|
ID uint `gorm:"primaryKey;autoIncrement:true" json:"-"`
|
||||||
PlanID uint `json:"-"`
|
PlanID uint `json:"-"`
|
||||||
Plan Plan `json:"-"`
|
Plan Plan `json:"-"`
|
||||||
Type string `gorm:"check:type in ('user','non-user')" json:"type"`
|
Type string `gorm:"check:type in ('user','non-user')" json:"type"`
|
||||||
Name string `gorm:"check:type=='member' OR name IS NOT NULL" json:"name"`
|
Name string `gorm:"check:type=='member' OR name IS NOT NULL" json:"name"`
|
||||||
Status string `gorm:"check:status in ('pending','ready')" json:"status"`
|
UserID string `json:"username"`
|
||||||
JoinCode string `gorm:"uniqueIndex;check:type=='non-member' OR join_code IS NOT NULL" json:"join_code,omitempty"`
|
User User `gorm:"foreignKey:UserID" json:"-"`
|
||||||
UserID string `json:"username"`
|
|
||||||
User User `gorm:"foreignKey:UserID" json:"-"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// CREATE TABLE plans(id INTEGER PRIMARY KEY AUTOINCREMENT, name STRING, owner STRING, FOREIGN KEY(owner) REFERENCES users(username))
|
// CREATE TABLE plans(id INTEGER PRIMARY KEY AUTOINCREMENT, name STRING, owner STRING, FOREIGN KEY(owner) REFERENCES users(username))
|
||||||
|
|
@ -27,6 +25,7 @@ type Plan struct {
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Owner string `json:"owner"`
|
Owner string `json:"owner"`
|
||||||
Description string `json:"description"`
|
Description string `json:"description"`
|
||||||
|
JoinCode string `gorm:"not null" json:"join_code,omitempty"`
|
||||||
Members []Member `json:"-"`
|
Members []Member `json:"-"`
|
||||||
Polls []Poll `gorm:"foreignKey:PlanID;references:ID" json:"-"`
|
Polls []Poll `gorm:"foreignKey:PlanID;references:ID" json:"-"`
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,32 @@ import (
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
func GetPlan(orm *gorm.DB, user User, id uint) (*Plan, error) {
|
func CreatePlan(db *gorm.DB, user *User, name string) (*Plan, error) {
|
||||||
|
join_code := make([]byte, 32)
|
||||||
|
_, err := rand.Read(join_code)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var plan Plan = Plan{
|
||||||
|
Name: name,
|
||||||
|
Owner: user.Username,
|
||||||
|
Members: []Member{
|
||||||
|
{
|
||||||
|
UserID: user.Username,
|
||||||
|
Type: "user",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
JoinCode: base64.URLEncoding.EncodeToString(join_code),
|
||||||
|
}
|
||||||
|
result := db.Create(&plan)
|
||||||
|
if result.Error != nil {
|
||||||
|
return nil, result.Error
|
||||||
|
}
|
||||||
|
return &plan, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetPlan(orm *gorm.DB, id uint) (*Plan, error) {
|
||||||
var plan Plan = Plan{
|
var plan Plan = Plan{
|
||||||
ID: id,
|
ID: id,
|
||||||
}
|
}
|
||||||
|
|
@ -20,16 +45,6 @@ func GetPlan(orm *gorm.DB, user User, id uint) (*Plan, error) {
|
||||||
return nil, result.Error
|
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
|
return &plan, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -81,6 +96,9 @@ func (p *Plan) AddMember(orm *gorm.DB, new_member *Member) error {
|
||||||
}
|
}
|
||||||
new_member.PlanID = p.ID
|
new_member.PlanID = p.ID
|
||||||
if new_member.Type == "non-user" {
|
if new_member.Type == "non-user" {
|
||||||
|
if new_member.Name == "" {
|
||||||
|
return errors.New("name required for non user")
|
||||||
|
}
|
||||||
found, err := p.HasNonUser(orm, new_member.Name)
|
found, err := p.HasNonUser(orm, new_member.Name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil
|
return nil
|
||||||
|
|
@ -88,9 +106,7 @@ func (p *Plan) AddMember(orm *gorm.DB, new_member *Member) error {
|
||||||
if found {
|
if found {
|
||||||
return errors.New("Non user name taken")
|
return errors.New("Non user name taken")
|
||||||
}
|
}
|
||||||
new_member.Status = "ready"
|
|
||||||
return orm.Create(&new_member).Error
|
return orm.Create(&new_member).Error
|
||||||
|
|
||||||
} else if new_member.Type == "user" {
|
} else if new_member.Type == "user" {
|
||||||
user, err := GetUser(orm, new_member.UserID)
|
user, err := GetUser(orm, new_member.UserID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -105,24 +121,7 @@ func (p *Plan) AddMember(orm *gorm.DB, new_member *Member) error {
|
||||||
return errors.New("User already is member")
|
return errors.New("User already is member")
|
||||||
}
|
}
|
||||||
|
|
||||||
new_member.Status = "pending"
|
return orm.Create(&new_member).Error
|
||||||
var res error
|
|
||||||
for retries := 3; retries >= 0; retries -= 1 {
|
|
||||||
join_code := make([]byte, 32)
|
|
||||||
_, err = rand.Read(join_code)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
new_member.JoinCode = base64.URLEncoding.EncodeToString(join_code)
|
|
||||||
res = orm.Create(&new_member).Error
|
|
||||||
|
|
||||||
if res == nil {
|
|
||||||
break
|
|
||||||
} else if !errors.Is(res, gorm.ErrDuplicatedKey) {
|
|
||||||
return res
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return res
|
|
||||||
} else {
|
} else {
|
||||||
return errors.New("Invalid type for user")
|
return errors.New("Invalid type for user")
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,8 @@
|
||||||
package core
|
package core
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
|
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -14,6 +16,31 @@ func (u *User) GetPlans(orm *gorm.DB) ([]Plan, error) {
|
||||||
return plans, err.Error
|
return plans, err.Error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (u *User) GetPlan(db *gorm.DB, plan_id uint) (Plan, error) {
|
||||||
|
var plan Plan = Plan{
|
||||||
|
ID: plan_id,
|
||||||
|
}
|
||||||
|
result := db.Take(&plan)
|
||||||
|
|
||||||
|
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
|
||||||
|
return plan, ErrNotFound
|
||||||
|
} else if result.Error != nil {
|
||||||
|
return plan, result.Error
|
||||||
|
}
|
||||||
|
|
||||||
|
if plan.Owner == u.Username {
|
||||||
|
return plan, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
isMember, err := plan.IsMember(db, u)
|
||||||
|
|
||||||
|
if !isMember || err != nil {
|
||||||
|
return plan, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return plan, nil
|
||||||
|
}
|
||||||
|
|
||||||
func GetUser(orm *gorm.DB, username string) (User, error) {
|
func GetUser(orm *gorm.DB, username string) (User, error) {
|
||||||
user := User{Username: username}
|
user := User{Username: username}
|
||||||
if err := orm.Take(&user).Error; err != nil {
|
if err := orm.Take(&user).Error; err != nil {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue