253 lines
4.8 KiB
Go
253 lines
4.8 KiB
Go
package apis
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
. "planner/core"
|
|
)
|
|
|
|
func createPlan(c *gin.Context) {
|
|
u := extractUser(db, c)
|
|
if u == nil {
|
|
return
|
|
}
|
|
|
|
var plan_req struct {
|
|
Name string `json:"name" form:"name"`
|
|
}
|
|
c.Bind(&plan_req)
|
|
|
|
_, err := CreatePlan(db, u, plan_req.Name)
|
|
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, err)
|
|
} else {
|
|
c.JSON(http.StatusOK, plan_req)
|
|
}
|
|
}
|
|
|
|
func listPlans(c *gin.Context) {
|
|
u := extractUser(db, c)
|
|
if u == nil {
|
|
return
|
|
}
|
|
plans, err := u.ListPlans(db)
|
|
if err == nil {
|
|
c.JSON(http.StatusOK, plans)
|
|
} else {
|
|
c.String(http.StatusInternalServerError, err.Error())
|
|
}
|
|
}
|
|
|
|
func getPlan(c *gin.Context) {
|
|
user := extractUser(db, c)
|
|
if user == nil {
|
|
return
|
|
}
|
|
|
|
var params struct {
|
|
Id uint `uri:"id"`
|
|
}
|
|
bind_result := c.BindUri(¶ms)
|
|
if bind_result != nil {
|
|
return
|
|
}
|
|
|
|
plan, err := user.GetPlan(db, params.Id)
|
|
|
|
if err == nil {
|
|
c.JSON(http.StatusOK, plan)
|
|
} 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())
|
|
}
|
|
}
|
|
|
|
func listPlanMembers(c *gin.Context) {
|
|
user := extractUser(db, c)
|
|
if user == nil {
|
|
return
|
|
}
|
|
|
|
plan_id, err := strconv.Atoi(c.Param("id"))
|
|
if err != nil {
|
|
c.Status(http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
plan, err := user.GetPlan(db, uint(plan_id))
|
|
members, err := plan.GetAllUsers(db)
|
|
|
|
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())
|
|
}
|
|
}
|
|
|
|
func addPlanMember(c *gin.Context) {
|
|
user := extractUser(db, c)
|
|
if user == nil {
|
|
return
|
|
}
|
|
|
|
plan_id, err := strconv.Atoi(c.Param("id"))
|
|
if err != nil {
|
|
c.Status(http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
plan, err := user.GetPlan(db, uint(plan_id))
|
|
|
|
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, &Member{Name: new_member.Name, Type: "non-user"})
|
|
|
|
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())
|
|
}
|
|
}
|
|
|
|
func joinPlan(c *gin.Context) {
|
|
user := extractUser(db, c)
|
|
if user == nil {
|
|
c.Status(http.StatusUnauthorized)
|
|
return
|
|
}
|
|
|
|
plan_id, err := strconv.Atoi(c.Param("id"))
|
|
if err != nil {
|
|
c.Status(http.StatusBadRequest)
|
|
return
|
|
}
|
|
plan, err := GetPlan(db, uint(plan_id))
|
|
if err != nil || plan == nil {
|
|
c.Status(http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
var query struct {
|
|
JoinCode string `form:"code"`
|
|
}
|
|
if c.ShouldBindQuery(&query) != nil || query.JoinCode == "" {
|
|
c.Status(http.StatusBadRequest)
|
|
return
|
|
}
|
|
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)
|
|
}
|
|
|
|
func createPlanPoll(c *gin.Context) {
|
|
user := extractUser(db, c)
|
|
if user == nil {
|
|
return
|
|
}
|
|
|
|
var params struct {
|
|
Id uint `uri:"id"`
|
|
}
|
|
bind_result := c.BindUri(¶ms)
|
|
if bind_result != nil {
|
|
return
|
|
}
|
|
|
|
plan, err := user.GetPlan(db, params.Id)
|
|
|
|
if errors.Is(err, ErrNotFound) {
|
|
c.Status(http.StatusNotFound)
|
|
return
|
|
} else if errors.Is(err, ErrNotMember) {
|
|
c.Status(http.StatusForbidden)
|
|
return
|
|
} else if err != nil {
|
|
c.String(http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
|
|
var poll_opts struct {
|
|
Options string `json:"options"`
|
|
}
|
|
bind_result = c.Bind(&poll_opts)
|
|
if bind_result != nil {
|
|
fmt.Println(bind_result)
|
|
return
|
|
}
|
|
|
|
poll := Poll{
|
|
PlanID: plan.ID,
|
|
Options: poll_opts.Options,
|
|
}
|
|
db.Create(&poll)
|
|
|
|
c.JSON(http.StatusCreated, poll)
|
|
}
|
|
|
|
func listPlanPolls(c *gin.Context) {
|
|
user := extractUser(db, c)
|
|
if user == nil {
|
|
return
|
|
}
|
|
|
|
var params struct {
|
|
Id uint `uri:"id"`
|
|
}
|
|
|
|
bind_result := c.BindUri(¶ms)
|
|
if bind_result != nil {
|
|
c.Status(http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
var polls []Poll
|
|
db.Where("plan_id = ?", params.Id).Find(&polls)
|
|
c.JSON(http.StatusOK, polls)
|
|
}
|
|
|
|
func bindPlanAPIs(r *gin.Engine) {
|
|
r.POST("/plans", createPlan)
|
|
r.GET("/plans", listPlans)
|
|
r.GET("/plans/:id", getPlan)
|
|
r.GET("/plans/:id/members", listPlanMembers)
|
|
r.POST("/plans/:id/members", addPlanMember)
|
|
r.GET("/plans/:id/join", joinPlan)
|
|
r.POST("/plans/:id/polls", createPlanPoll)
|
|
r.GET("/plans/:id/polls", listPlanPolls)
|
|
}
|