API package refactor

This commit is contained in:
Manuel Forcén Muñoz 2025-02-12 19:29:00 +01:00
parent a7cd86962e
commit bc6b57bc54
14 changed files with 474 additions and 416 deletions

View file

@ -1,15 +1,9 @@
package main
import (
"errors"
"fmt"
"log"
"net/http"
"strconv"
. "planner/errors"
"planner/models"
. "planner/models"
"planner/apis"
"github.com/gin-gonic/gin"
_ "github.com/mattn/go-sqlite3"
@ -29,396 +23,6 @@ func main() {
defer db.Close()
r := gin.Default()
r.GET("/", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"message": "pong",
})
})
r.POST("/login", func(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,
}
orm.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")
}
})
r.GET("/user", func(c *gin.Context) {
var q struct {
Name string `form:"name"`
}
if c.ShouldBind(&q) == nil {
user := User{
Username: q.Name,
}
orm.Take(&user)
fmt.Println(user)
c.JSON(http.StatusOK, user)
}
})
r.POST("/user", func(c *gin.Context) {
var u User
if c.ShouldBind(&u) == nil {
orm.Create(&u)
c.Status(http.StatusCreated)
} else {
fmt.Print("Could not bind model")
}
})
r.GET("/users/:id", func(c *gin.Context) {
u := ExtractUser(orm, c)
if u == nil {
return
}
requested := c.Param("id")
if requested == "" {
c.Status(http.StatusBadRequest)
return
}
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" 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 := orm.Create(&plan)
if result.Error != nil {
c.JSON(http.StatusInternalServerError, result.Error)
} else {
c.JSON(http.StatusOK, plan_req)
}
})
r.GET("/plans", func(c *gin.Context) {
u := ExtractUser(orm, c)
if u == nil {
return
}
plans, err := u.GetPlans(orm)
if err == nil {
c.JSON(http.StatusOK, plans)
} else {
c.String(http.StatusInternalServerError, err.Error())
}
})
r.GET("/plans/:id", func(c *gin.Context) {
user := ExtractUser(orm, c)
if user == nil {
return
}
var params struct {
Id uint `uri:"id"`
}
bind_result := c.BindUri(&params)
if bind_result != nil {
return
}
plan, err := GetPlan(orm, *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())
}
})
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 := ExtractUser(orm, c)
if user == nil {
return
}
var params struct {
Id uint `uri:"id"`
}
bind_result := c.BindUri(&params)
if bind_result != nil {
return
}
plan, err := GetPlan(orm, *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,
}
orm.Create(&poll)
c.JSON(http.StatusCreated, poll)
})
r.GET("/plans/:id/polls", func(c *gin.Context) {
user := ExtractUser(orm, c)
if user == nil {
return
}
var params struct {
Id uint `uri:"id"`
}
bind_result := c.BindUri(&params)
if bind_result != nil {
c.Status(http.StatusBadRequest)
return
}
var polls []Poll
orm.Where("plan_id = ?", params.Id).Find(&polls)
c.JSON(http.StatusOK, polls)
})
r.GET("/polls/:poll_id", func(c *gin.Context) {
user := ExtractUser(orm, c)
if user == nil {
return
}
var params struct {
PollId uint `uri:"poll_id"`
}
bind_result := c.BindUri(&params)
if bind_result != nil {
fmt.Println(bind_result)
return
}
fmt.Println(params)
poll, _ := models.GetPoll(orm, *user, params.PollId)
c.JSON(http.StatusOK, poll)
})
r.GET("/polls/:poll_id/votes", func(c *gin.Context) {
user := ExtractUser(orm, c)
if user == nil {
return
}
var params struct {
PollId uint `uri:"poll_id"`
}
bind_result := c.BindUri(&params)
if bind_result != nil {
fmt.Println(bind_result)
return
}
var votes []Vote
orm.Where("poll_id = ?", params.PollId).Find(&votes)
c.JSON(http.StatusOK, &votes)
})
r.POST("/polls/:poll_id/votes", func(c *gin.Context) {
user := ExtractUser(orm, 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 := models.GetPoll(orm, *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(orm, *user, vote_params.Vote); err != nil {
c.String(http.StatusBadRequest, err.Error())
}
c.Status(http.StatusOK)
})
apis.BindAPIs(r, orm)
r.Run()
}