|
|
|
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 ItemsToDTOs(items []types.IItem) []ItemDTO {
|
|
|
|
var itemDtos []ItemDTO
|
|
|
|
for _, item := range items {
|
|
|
|
itemDtos = append(itemDtos, ItemDTO{id: item.GetId(), message: item.GetMessage()})
|
|
|
|
}
|
|
|
|
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, item.ToDTO())
|
|
|
|
})
|
|
|
|
router.GET("/itemByUser/:id", func(c *gin.Context) {
|
|
|
|
|
|
|
|
})
|
|
|
|
router.POST("/item", func(c *gin.Context) {})
|
|
|
|
router.PUT("/item", func(c *gin.Context) {})
|
|
|
|
router.DELETE("/item/:id", func(c *gin.Context) {})
|
|
|
|
router.GET("/link/:itemId/:userId", func(c *gin.Context) {})
|
|
|
|
}
|