fixing some stuff
parent
ff83f97e08
commit
5632d724f8
@ -1,7 +1,54 @@
|
||||
package api
|
||||
|
||||
import "github.com/gin-gonic/gin"
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
func RegisterRoutes(router *gin.RouterGroup) {
|
||||
"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) {})
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
package interfaces
|
||||
package types
|
||||
|
||||
type IItem interface {
|
||||
GetId() int
|
@ -1,9 +1,10 @@
|
||||
package interfaces
|
||||
package types
|
||||
|
||||
type IPersitenceProvider interface {
|
||||
CreateItem(item IItem) error
|
||||
UpdateItem(item IItem) error
|
||||
GetItem(id int) (IItem, error)
|
||||
GetItemByUser(user IUser) ([]IItem, error)
|
||||
GetAllItems() ([]IItem, error)
|
||||
Link(user IUser, item IItem) error
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package interfaces
|
||||
package types
|
||||
|
||||
type IUser interface {
|
||||
GetId() int
|
Loading…
Reference in New Issue