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"`
|
||||
}
|
||||
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 {
|
||||
c.JSON(http.StatusInternalServerError, result.Error)
|
||||
_, err := CreatePlan(db, u, plan_req.Name)
|
||||
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, err)
|
||||
} else {
|
||||
c.JSON(http.StatusOK, plan_req)
|
||||
}
|
||||
|
|
@ -69,7 +58,7 @@ func getPlan(c *gin.Context) {
|
|||
return
|
||||
}
|
||||
|
||||
plan, err := GetPlan(db, *user, params.Id)
|
||||
plan, err := user.GetPlan(db, params.Id)
|
||||
|
||||
if err == nil {
|
||||
c.JSON(http.StatusOK, plan)
|
||||
|
|
@ -94,7 +83,7 @@ func listPlanMembers(c *gin.Context) {
|
|||
return
|
||||
}
|
||||
|
||||
plan, err := GetPlan(db, *user, uint(plan_id))
|
||||
plan, err := user.GetPlan(db, uint(plan_id))
|
||||
members, err := plan.GetAllUsers(db)
|
||||
|
||||
if err == nil {
|
||||
|
|
@ -120,15 +109,17 @@ func addPlanMember(c *gin.Context) {
|
|||
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 {
|
||||
c.String(http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
err = plan.AddMember(db, &new_member)
|
||||
err = plan.AddMember(db, &Member{Name: new_member.Name, Type: "non-user"})
|
||||
|
||||
if err == nil {
|
||||
c.JSON(http.StatusOK, new_member)
|
||||
|
|
@ -144,6 +135,7 @@ func addPlanMember(c *gin.Context) {
|
|||
func joinPlan(c *gin.Context) {
|
||||
user := extractUser(db, c)
|
||||
if user == nil {
|
||||
c.Status(http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
|
|
@ -152,35 +144,34 @@ func joinPlan(c *gin.Context) {
|
|||
c.Status(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
plan, err := GetPlan(db, *user, uint(plan_id))
|
||||
if err != nil {
|
||||
plan, err := GetPlan(db, uint(plan_id))
|
||||
if err != nil || plan == nil {
|
||||
c.Status(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
member, err := plan.GetMember(db, user)
|
||||
var query struct {
|
||||
JoinCode string `fdb:"code"`
|
||||
JoinCode string `form:"code"`
|
||||
}
|
||||
if c.ShouldBindQuery(&query) != nil || query.JoinCode == "" {
|
||||
c.Status(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
if member.Status != "pending" {
|
||||
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 {
|
||||
if query.JoinCode != plan.JoinCode {
|
||||
c.String(http.StatusConflict, "Invalid join code")
|
||||
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)
|
||||
}
|
||||
|
||||
|
|
@ -198,7 +189,7 @@ func createPlanPoll(c *gin.Context) {
|
|||
return
|
||||
}
|
||||
|
||||
plan, err := GetPlan(db, *user, params.Id)
|
||||
plan, err := user.GetPlan(db, params.Id)
|
||||
|
||||
if errors.Is(err, ErrNotFound) {
|
||||
c.Status(http.StatusNotFound)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue