You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.3 KiB
Go

3 years ago
package api
import (
"net/http"
"strconv"
3 years ago
"dymatrix.de/jspahl/todo/internal/types"
"github.com/gin-gonic/gin"
)
3 years ago
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) {})
3 years ago
}