Added membership for users
This commit is contained in:
parent
acfe6b7d0c
commit
a7cd86962e
5 changed files with 238 additions and 28 deletions
158
planner.go
158
planner.go
|
|
@ -5,6 +5,7 @@ import (
|
|||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
. "planner/errors"
|
||||
"planner/models"
|
||||
|
|
@ -59,7 +60,7 @@ func main() {
|
|||
}
|
||||
})
|
||||
|
||||
r.GET("/users", func(c *gin.Context) {
|
||||
r.GET("/user", func(c *gin.Context) {
|
||||
var q struct {
|
||||
Name string `form:"name"`
|
||||
}
|
||||
|
|
@ -73,7 +74,7 @@ func main() {
|
|||
}
|
||||
})
|
||||
|
||||
r.POST("/users", func(c *gin.Context) {
|
||||
r.POST("/user", func(c *gin.Context) {
|
||||
var u User
|
||||
if c.ShouldBind(&u) == nil {
|
||||
orm.Create(&u)
|
||||
|
|
@ -83,30 +84,47 @@ func main() {
|
|||
}
|
||||
})
|
||||
|
||||
r.POST("/plans", func(c *gin.Context) {
|
||||
username, _, ok := c.Request.BasicAuth()
|
||||
if !ok {
|
||||
c.Status(http.StatusUnauthorized)
|
||||
r.GET("/users/:id", func(c *gin.Context) {
|
||||
u := ExtractUser(orm, c)
|
||||
if u == nil {
|
||||
return
|
||||
}
|
||||
u := User{
|
||||
Username: username,
|
||||
|
||||
requested := c.Param("id")
|
||||
if requested == "" {
|
||||
c.Status(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
if result := orm.Take(&u); result.Error != nil {
|
||||
c.String(http.StatusNotFound, "Unable to find user "+username)
|
||||
other := User{Username: requested}
|
||||
res := orm.Take(&other)
|
||||
if res.Error != nil {
|
||||
c.String(http.StatusInternalServerError, res.Error.Error())
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, other)
|
||||
})
|
||||
|
||||
r.POST("/plans", func(c *gin.Context) {
|
||||
u := ExtractUser(orm, c)
|
||||
if u == nil {
|
||||
return
|
||||
}
|
||||
|
||||
var plan_req struct {
|
||||
Name string `json:"name"`
|
||||
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},
|
||||
{
|
||||
UserID: u.Username,
|
||||
Type: "user",
|
||||
Status: "ready",
|
||||
JoinCode: "owner",
|
||||
},
|
||||
},
|
||||
}
|
||||
result := orm.Create(&plan)
|
||||
|
|
@ -119,7 +137,7 @@ func main() {
|
|||
})
|
||||
|
||||
r.GET("/plans", func(c *gin.Context) {
|
||||
u := extract_user(orm, c)
|
||||
u := ExtractUser(orm, c)
|
||||
if u == nil {
|
||||
return
|
||||
}
|
||||
|
|
@ -132,7 +150,7 @@ func main() {
|
|||
})
|
||||
|
||||
r.GET("/plans/:id", func(c *gin.Context) {
|
||||
user := extract_user(orm, c)
|
||||
user := ExtractUser(orm, c)
|
||||
if user == nil {
|
||||
return
|
||||
}
|
||||
|
|
@ -158,8 +176,110 @@ func main() {
|
|||
}
|
||||
})
|
||||
|
||||
r.GET("/plans/:id/members", func(c *gin.Context) {
|
||||
user := ExtractUser(orm, c)
|
||||
if user == nil {
|
||||
return
|
||||
}
|
||||
|
||||
plan_id, err := strconv.Atoi(c.Param("id"))
|
||||
if err != nil {
|
||||
c.Status(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
plan, err := GetPlan(orm, *user, uint(plan_id))
|
||||
members, err := plan.GetAllUsers(orm)
|
||||
|
||||
if err == nil {
|
||||
c.JSON(http.StatusOK, members)
|
||||
} else if errors.Is(err, ErrNotMember) {
|
||||
c.Status(http.StatusForbidden)
|
||||
} else if errors.Is(err, ErrNotFound) {
|
||||
c.Status(http.StatusNotFound)
|
||||
} else {
|
||||
c.String(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
})
|
||||
|
||||
r.POST("/plans/:id/members", func(c *gin.Context) {
|
||||
user := ExtractUser(orm, c)
|
||||
if user == nil {
|
||||
return
|
||||
}
|
||||
|
||||
plan_id, err := strconv.Atoi(c.Param("id"))
|
||||
if err != nil {
|
||||
c.Status(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
plan, err := GetPlan(orm, *user, uint(plan_id))
|
||||
|
||||
var new_member Member
|
||||
if err := c.ShouldBind(&new_member); err != nil {
|
||||
c.String(http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
err = plan.AddMember(orm, &new_member)
|
||||
|
||||
if err == nil {
|
||||
c.JSON(http.StatusOK, new_member)
|
||||
} else if errors.Is(err, ErrNotMember) {
|
||||
c.Status(http.StatusForbidden)
|
||||
} else if errors.Is(err, ErrNotFound) {
|
||||
c.Status(http.StatusNotFound)
|
||||
} else {
|
||||
c.String(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
})
|
||||
|
||||
r.GET("/plans/:id/join", func(c *gin.Context) {
|
||||
user := ExtractUser(orm, c)
|
||||
if user == nil {
|
||||
return
|
||||
}
|
||||
|
||||
plan_id, err := strconv.Atoi(c.Param("id"))
|
||||
if err != nil {
|
||||
c.Status(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
plan, err := GetPlan(orm, *user, uint(plan_id))
|
||||
if err != nil {
|
||||
c.Status(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
member, err := plan.GetMember(orm, user)
|
||||
var query struct {
|
||||
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 := orm.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")
|
||||
return
|
||||
}
|
||||
c.Status(http.StatusOK)
|
||||
})
|
||||
|
||||
r.POST("/plans/:id/polls", func(c *gin.Context) {
|
||||
user := extract_user(orm, c)
|
||||
user := ExtractUser(orm, c)
|
||||
if user == nil {
|
||||
return
|
||||
}
|
||||
|
|
@ -204,7 +324,7 @@ func main() {
|
|||
})
|
||||
|
||||
r.GET("/plans/:id/polls", func(c *gin.Context) {
|
||||
user := extract_user(orm, c)
|
||||
user := ExtractUser(orm, c)
|
||||
if user == nil {
|
||||
return
|
||||
}
|
||||
|
|
@ -225,7 +345,7 @@ func main() {
|
|||
})
|
||||
|
||||
r.GET("/polls/:poll_id", func(c *gin.Context) {
|
||||
user := extract_user(orm, c)
|
||||
user := ExtractUser(orm, c)
|
||||
if user == nil {
|
||||
return
|
||||
}
|
||||
|
|
@ -246,7 +366,7 @@ func main() {
|
|||
})
|
||||
|
||||
r.GET("/polls/:poll_id/votes", func(c *gin.Context) {
|
||||
user := extract_user(orm, c)
|
||||
user := ExtractUser(orm, c)
|
||||
if user == nil {
|
||||
return
|
||||
}
|
||||
|
|
@ -267,7 +387,7 @@ func main() {
|
|||
})
|
||||
|
||||
r.POST("/polls/:poll_id/votes", func(c *gin.Context) {
|
||||
user := extract_user(orm, c)
|
||||
user := ExtractUser(orm, c)
|
||||
if user == nil {
|
||||
return
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue