2024-11-09 19:35:44 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
|
|
|
|
"log"
|
|
|
|
|
"net/http"
|
2025-02-11 21:16:39 +01:00
|
|
|
"strconv"
|
2024-11-09 19:35:44 +01:00
|
|
|
|
|
|
|
|
. "planner/errors"
|
|
|
|
|
"planner/models"
|
|
|
|
|
. "planner/models"
|
|
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
_ "github.com/mattn/go-sqlite3"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
|
fmt.Println("Opening database db.sqlite")
|
|
|
|
|
|
|
|
|
|
orm := bootstrapDatabase()
|
|
|
|
|
|
|
|
|
|
if orm == nil {
|
|
|
|
|
log.Fatal("Failed to init database")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
db, _ := orm.DB()
|
|
|
|
|
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")
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
2025-02-11 21:16:39 +01:00
|
|
|
r.GET("/user", func(c *gin.Context) {
|
2024-11-09 19:35:44 +01:00
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
2025-02-11 21:16:39 +01:00
|
|
|
r.POST("/user", func(c *gin.Context) {
|
2024-11-09 19:35:44 +01:00
|
|
|
var u User
|
|
|
|
|
if c.ShouldBind(&u) == nil {
|
|
|
|
|
orm.Create(&u)
|
|
|
|
|
c.Status(http.StatusCreated)
|
|
|
|
|
} else {
|
|
|
|
|
fmt.Print("Could not bind model")
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
2025-02-11 21:16:39 +01:00
|
|
|
r.GET("/users/:id", func(c *gin.Context) {
|
|
|
|
|
u := ExtractUser(orm, c)
|
|
|
|
|
if u == nil {
|
2024-11-09 19:35:44 +01:00
|
|
|
return
|
|
|
|
|
}
|
2025-02-11 21:16:39 +01:00
|
|
|
|
|
|
|
|
requested := c.Param("id")
|
|
|
|
|
if requested == "" {
|
|
|
|
|
c.Status(http.StatusBadRequest)
|
|
|
|
|
return
|
2024-11-09 19:35:44 +01:00
|
|
|
}
|
|
|
|
|
|
2025-02-11 21:16:39 +01:00
|
|
|
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 {
|
2024-11-09 19:35:44 +01:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var plan_req struct {
|
2025-02-11 21:16:39 +01:00
|
|
|
Name string `json:"name" form:"name"`
|
2024-11-09 19:35:44 +01:00
|
|
|
}
|
|
|
|
|
c.Bind(&plan_req)
|
|
|
|
|
var plan Plan = Plan{
|
|
|
|
|
Name: plan_req.Name,
|
|
|
|
|
Owner: u.Username,
|
2025-01-14 20:14:07 +01:00
|
|
|
Members: []Member{
|
2025-02-11 21:16:39 +01:00
|
|
|
{
|
|
|
|
|
UserID: u.Username,
|
|
|
|
|
Type: "user",
|
|
|
|
|
Status: "ready",
|
|
|
|
|
JoinCode: "owner",
|
|
|
|
|
},
|
2024-11-09 19:35:44 +01:00
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
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) {
|
2025-02-11 21:16:39 +01:00
|
|
|
u := ExtractUser(orm, c)
|
2024-11-09 19:35:44 +01:00
|
|
|
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) {
|
2025-02-11 21:16:39 +01:00
|
|
|
user := ExtractUser(orm, c)
|
2024-11-09 19:35:44 +01:00
|
|
|
if user == nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var params struct {
|
|
|
|
|
Id uint `uri:"id"`
|
|
|
|
|
}
|
|
|
|
|
bind_result := c.BindUri(¶ms)
|
|
|
|
|
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())
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
2025-02-11 21:16:39 +01:00
|
|
|
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)
|
|
|
|
|
})
|
|
|
|
|
|
2024-11-09 19:35:44 +01:00
|
|
|
r.POST("/plans/:id/polls", func(c *gin.Context) {
|
2025-02-11 21:16:39 +01:00
|
|
|
user := ExtractUser(orm, c)
|
2024-11-09 19:35:44 +01:00
|
|
|
if user == nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var params struct {
|
|
|
|
|
Id uint `uri:"id"`
|
|
|
|
|
}
|
|
|
|
|
bind_result := c.BindUri(¶ms)
|
|
|
|
|
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) {
|
2025-02-11 21:16:39 +01:00
|
|
|
user := ExtractUser(orm, c)
|
2024-11-09 19:35:44 +01:00
|
|
|
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
|
|
|
|
|
orm.Where("plan_id = ?", params.Id).Find(&polls)
|
|
|
|
|
c.JSON(http.StatusOK, polls)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
r.GET("/polls/:poll_id", func(c *gin.Context) {
|
2025-02-11 21:16:39 +01:00
|
|
|
user := ExtractUser(orm, c)
|
2024-11-09 19:35:44 +01:00
|
|
|
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, _ := models.GetPoll(orm, *user, params.PollId)
|
|
|
|
|
c.JSON(http.StatusOK, poll)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
r.GET("/polls/:poll_id/votes", func(c *gin.Context) {
|
2025-02-11 21:16:39 +01:00
|
|
|
user := ExtractUser(orm, c)
|
2024-11-09 19:35:44 +01:00
|
|
|
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 []Vote
|
|
|
|
|
orm.Where("poll_id = ?", params.PollId).Find(&votes)
|
|
|
|
|
c.JSON(http.StatusOK, &votes)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
r.POST("/polls/:poll_id/votes", func(c *gin.Context) {
|
2025-02-11 21:16:39 +01:00
|
|
|
user := ExtractUser(orm, c)
|
2024-11-09 19:35:44 +01:00
|
|
|
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)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
r.Run()
|
|
|
|
|
}
|