API package refactor
This commit is contained in:
parent
a7cd86962e
commit
bc6b57bc54
14 changed files with 474 additions and 416 deletions
27
apis/base.go
Normal file
27
apis/base.go
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
package apis
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
var db *gorm.DB
|
||||
|
||||
func BindAPIs(r *gin.Engine, cfg_db *gorm.DB) error {
|
||||
if cfg_db == nil {
|
||||
return errors.New("Database is null")
|
||||
}
|
||||
db = cfg_db
|
||||
r.GET("/", func(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"message": "pong",
|
||||
})
|
||||
})
|
||||
bindPlanAPIs(r)
|
||||
bindPollAPIs(r)
|
||||
bindUserAPIs(r)
|
||||
return nil
|
||||
}
|
||||
26
apis/extract.go
Normal file
26
apis/extract.go
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
package apis
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/gorm"
|
||||
"net/http"
|
||||
"planner/core"
|
||||
)
|
||||
|
||||
func extractUser(orm *gorm.DB, c *gin.Context) *core.User {
|
||||
username, _, ok := c.Request.BasicAuth()
|
||||
if !ok {
|
||||
c.Status(http.StatusUnauthorized)
|
||||
return nil
|
||||
}
|
||||
u := core.User{
|
||||
Username: username,
|
||||
}
|
||||
|
||||
result := orm.Take(&u)
|
||||
if result.Error != nil {
|
||||
c.String(http.StatusNotFound, "Unable to find user "+username)
|
||||
return nil
|
||||
}
|
||||
return &u
|
||||
}
|
||||
262
apis/plans.go
Normal file
262
apis/plans.go
Normal file
|
|
@ -0,0 +1,262 @@
|
|||
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)
|
||||
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)
|
||||
} else {
|
||||
c.JSON(http.StatusOK, plan_req)
|
||||
}
|
||||
}
|
||||
|
||||
func listPlans(c *gin.Context) {
|
||||
u := extractUser(db, c)
|
||||
if u == nil {
|
||||
return
|
||||
}
|
||||
plans, err := u.GetPlans(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 := GetPlan(db, *user, 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 := GetPlan(db, *user, 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 := GetPlan(db, *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(db, &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())
|
||||
}
|
||||
}
|
||||
|
||||
func joinPlan(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 := GetPlan(db, *user, uint(plan_id))
|
||||
if err != nil {
|
||||
c.Status(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
member, err := plan.GetMember(db, user)
|
||||
var query struct {
|
||||
JoinCode string `fdb:"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 {
|
||||
c.String(http.StatusConflict, "Invalid join code")
|
||||
return
|
||||
}
|
||||
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 := GetPlan(db, *user, 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)
|
||||
}
|
||||
91
apis/polls.go
Normal file
91
apis/polls.go
Normal file
|
|
@ -0,0 +1,91 @@
|
|||
package apis
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"planner/core"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func getPoll(c *gin.Context) {
|
||||
user := extractUser(db, c)
|
||||
if user == nil {
|
||||
return
|
||||
}
|
||||
|
||||
var params struct {
|
||||
PollId uint `uri:"poll_id"`
|
||||
}
|
||||
|
||||
bind_result := c.BindUri(¶ms)
|
||||
if bind_result != nil {
|
||||
fmt.Println(bind_result)
|
||||
return
|
||||
}
|
||||
fmt.Println(params)
|
||||
|
||||
poll, _ := core.GetPoll(db, *user, params.PollId)
|
||||
c.JSON(http.StatusOK, poll)
|
||||
}
|
||||
|
||||
func getPollVotes(c *gin.Context) {
|
||||
user := extractUser(db, c)
|
||||
if user == nil {
|
||||
return
|
||||
}
|
||||
|
||||
var params struct {
|
||||
PollId uint `uri:"poll_id"`
|
||||
}
|
||||
|
||||
bind_result := c.BindUri(¶ms)
|
||||
if bind_result != nil {
|
||||
fmt.Println(bind_result)
|
||||
return
|
||||
}
|
||||
|
||||
var votes []core.Vote
|
||||
db.Where("poll_id = ?", params.PollId).Find(&votes)
|
||||
c.JSON(http.StatusOK, &votes)
|
||||
}
|
||||
|
||||
func pollVote(c *gin.Context) {
|
||||
user := extractUser(db, c)
|
||||
if user == nil {
|
||||
return
|
||||
}
|
||||
|
||||
var path_params struct {
|
||||
PollId uint `uri:"poll_id"`
|
||||
}
|
||||
|
||||
bind_result := c.BindUri(&path_params)
|
||||
if bind_result != nil {
|
||||
fmt.Println(bind_result)
|
||||
return
|
||||
}
|
||||
|
||||
poll, err := core.GetPoll(db, *user, path_params.PollId)
|
||||
if err != nil {
|
||||
c.String(http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
var vote_params struct {
|
||||
Vote string `json:"vote"`
|
||||
}
|
||||
c.Bind(&vote_params)
|
||||
|
||||
if err := poll.SetVote(db, *user, vote_params.Vote); err != nil {
|
||||
c.String(http.StatusBadRequest, err.Error())
|
||||
}
|
||||
|
||||
c.Status(http.StatusOK)
|
||||
}
|
||||
|
||||
func bindPollAPIs(r *gin.Engine) {
|
||||
r.GET("/polls/:poll_id", getPoll)
|
||||
r.GET("/polls/:poll_id/votes", getPollVotes)
|
||||
r.POST("/polls/:poll_id/votes", pollVote)
|
||||
}
|
||||
64
apis/users.go
Normal file
64
apis/users.go
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
package apis
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
. "planner/core"
|
||||
)
|
||||
|
||||
func getUserByName(c *gin.Context) {
|
||||
var q struct {
|
||||
Name string `fdb:"name"`
|
||||
}
|
||||
if c.ShouldBind(&q) == nil {
|
||||
user := User{
|
||||
Username: q.Name,
|
||||
}
|
||||
db.Take(&user)
|
||||
fmt.Println(user)
|
||||
c.JSON(http.StatusOK, user)
|
||||
}
|
||||
}
|
||||
|
||||
func createUser(c *gin.Context) {
|
||||
var u User
|
||||
if c.ShouldBind(&u) == nil {
|
||||
db.Create(&u)
|
||||
c.Status(http.StatusCreated)
|
||||
} else {
|
||||
fmt.Print("Could not bind model")
|
||||
c.String(http.StatusInternalServerError, "Unable to bind user")
|
||||
}
|
||||
}
|
||||
|
||||
func login(c *gin.Context) {
|
||||
var q struct {
|
||||
Username string `json:"username" form:"username"`
|
||||
Password string `json:"password" form:"password"`
|
||||
}
|
||||
if c.ShouldBind(&q) == nil {
|
||||
if q.Username == "" {
|
||||
c.String(http.StatusBadRequest, "Login data is null")
|
||||
} else {
|
||||
user := User{
|
||||
Username: q.Username,
|
||||
}
|
||||
db.Take(&user)
|
||||
if user.Password == q.Password {
|
||||
c.JSON(http.StatusOK, map[string]string{"username": user.Username})
|
||||
} else {
|
||||
c.Status(http.StatusForbidden)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
c.String(http.StatusBadRequest, "Unable to bind data")
|
||||
}
|
||||
}
|
||||
func bindUserAPIs(r *gin.Engine) {
|
||||
r.POST("/login", login)
|
||||
r.GET("/user", getUserByName)
|
||||
r.POST("/user", createUser)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue