|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"dymatrix.de/jspahl/todo/internal/types"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ItemDTO struct {
|
|
|
|
id int
|
|
|
|
message string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (item ItemDTO) GetId() int {
|
|
|
|
return item.id
|
|
|
|
}
|
|
|
|
func (item ItemDTO) GetMessage() string {
|
|
|
|
return item.message
|
|
|
|
}
|
|
|
|
|
|
|
|
type UserDTO struct {
|
|
|
|
id int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (user UserDTO) GetId() int {
|
|
|
|
return user.id
|
|
|
|
}
|
|
|
|
|
|
|
|
func ItemToDTO(item types.IItem) ItemDTO {
|
|
|
|
var itemDto = ItemDTO{}
|
|
|
|
itemDto.id = item.GetId()
|
|
|
|
itemDto.message = item.GetMessage()
|
|
|
|
return itemDto
|
|
|
|
}
|
|
|
|
func ItemsToDTOs(items []types.IItem) []ItemDTO {
|
|
|
|
var itemDtos []ItemDTO
|
|
|
|
for _, item := range items {
|
|
|
|
itemDtos = append(itemDtos, ItemToDTO(item))
|
|
|
|
}
|
|
|
|
return itemDtos
|
|
|
|
}
|
|
|
|
|
|
|
|
func RegisterRoutes(router *gin.RouterGroup, prov types.IPersitenceProvider) {
|
|
|
|
router.GET("/item", func(c *gin.Context) {
|
|
|
|
items, err := prov.GetAllItems()
|
|
|
|
if err != nil {
|
|
|
|
c.String(http.StatusInternalServerError, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
itemDTOs := ItemsToDTOs(items)
|
|
|
|
c.JSON(http.StatusOK, itemDTOs)
|
|
|
|
})
|
|
|
|
router.GET("/item/:id", func(c *gin.Context) {
|
|
|
|
id, err := strconv.Atoi(c.Param("id"))
|
|
|
|
if err != nil {
|
|
|
|
c.String(http.StatusBadRequest, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
item, err := prov.GetItem(id)
|
|
|
|
if err != nil {
|
|
|
|
c.String(http.StatusInternalServerError, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.JSON(http.StatusOK, ItemToDTO(item))
|
|
|
|
})
|
|
|
|
router.GET("/itemByUser/:id", func(c *gin.Context) {
|
|
|
|
id, err := strconv.Atoi(c.Param("id"))
|
|
|
|
if err != nil {
|
|
|
|
c.String(404, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
items, err := prov.GetItemByUser(UserDTO{id: id})
|
|
|
|
if err != nil {
|
|
|
|
c.String(http.StatusInternalServerError, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.JSON(http.StatusOK, ItemsToDTOs(items))
|
|
|
|
})
|
|
|
|
router.POST("/item", func(c *gin.Context) {
|
|
|
|
var item struct {
|
|
|
|
message string
|
|
|
|
}
|
|
|
|
if err := c.BindJSON(&item); err != nil {
|
|
|
|
c.String(http.StatusBadRequest, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
prov.CreateItem(ItemDTO{message: item.message})
|
|
|
|
c.Status(200)
|
|
|
|
})
|
|
|
|
router.PUT("/item", func(c *gin.Context) {
|
|
|
|
var item ItemDTO
|
|
|
|
if err := c.BindJSON(&item); err != nil {
|
|
|
|
c.String(http.StatusBadRequest, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := prov.UpdateItem(item); err != nil {
|
|
|
|
c.String(http.StatusInternalServerError, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.Status(200)
|
|
|
|
})
|
|
|
|
router.DELETE("/item/:id", func(c *gin.Context) {
|
|
|
|
id, err := strconv.Atoi(c.Param("id"))
|
|
|
|
if err != nil {
|
|
|
|
c.String(404, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := prov.DeleteItem(ItemDTO{id: id}); err != nil {
|
|
|
|
c.String(404, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.Status(200)
|
|
|
|
})
|
|
|
|
router.GET("/link/:itemId/:userId", func(c *gin.Context) {
|
|
|
|
itemId, err := strconv.Atoi(c.Param("itemId"))
|
|
|
|
if err != nil {
|
|
|
|
c.String(404, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
userId, err := strconv.Atoi(c.Param("userId"))
|
|
|
|
if err != nil {
|
|
|
|
c.String(404, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := prov.Link(UserDTO{id: userId}, ItemDTO{id: itemId}); err != nil {
|
|
|
|
c.String(404, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.Status(200)
|
|
|
|
})
|
|
|
|
router.POST("/user", func(c *gin.Context) {
|
|
|
|
var user struct {
|
|
|
|
id int
|
|
|
|
}
|
|
|
|
if err := c.BindJSON(&user); err != nil {
|
|
|
|
c.String(http.StatusBadRequest, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
prov.CreateUser(UserDTO{user.id})
|
|
|
|
c.Status(200)
|
|
|
|
})
|
|
|
|
}
|