working GET routes for items
This commit is contained in:
@@ -12,11 +12,24 @@ type ItemDTO struct {
|
||||
id int
|
||||
message string
|
||||
}
|
||||
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, ItemDTO{id: item.GetId(), message: item.GetMessage()})
|
||||
itemDtos = append(itemDtos, ItemToDTO(item))
|
||||
}
|
||||
return itemDtos
|
||||
}
|
||||
@@ -42,10 +55,20 @@ func RegisterRoutes(router *gin.RouterGroup, prov types.IPersitenceProvider) {
|
||||
c.String(http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, item.ToDTO())
|
||||
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) {})
|
||||
router.PUT("/item", func(c *gin.Context) {})
|
||||
|
||||
Reference in New Issue
Block a user