I am studying gofiber, I am using GORM and github.com/morkid/paginate, I want to remove the child element from the response. this is the response
{
"data": {
"items": [
{
"ID": 1,
"CreatedAt": "2023-01-22T02:33:12.604+08:00",
"UpdatedAt": "2023-01-22T02:33:12.604+08:00",
"DeletedAt": null,
"email": "ilham@mail.com",
"username": "",
"names": "",
"posts": [
{
"id": 1,
"created_at": "2023-01-22T01:59:42.611+08:00",
"updated_at": "2023-01-22T01:59:42.611+08:00",
"deleted_at": null,
"title": "using loop",
"body": "tes body postman 2",
"UserID": 1,
// i want remove this
"User": {
"ID": 0,
"CreatedAt": "0001-01-01T00:00:00Z",
"UpdatedAt": "0001-01-01T00:00:00Z",
"DeletedAt": null,
"email": "",
"username": "",
"names": "",
"posts": null
}
}
]
}
],
"page": 0,
"size": 10,
"max_page": 1,
"total_pages": 1,
"total": 1,
"last": false,
"first": true,
"visible": 1
}
}
i want the response like this
{
"data": {
"items": [
{
"ID": 1,
"CreatedAt": "2023-01-22T02:33:12.604+08:00",
"UpdatedAt": "2023-01-22T02:33:12.604+08:00",
"DeletedAt": null,
"email": "ilham@mail.com",
"username": "",
"names": "",
"posts": [
{
"id": 1,
"created_at": "2023-01-22T01:59:42.611+08:00",
"updated_at": "2023-01-22T01:59:42.611+08:00",
"deleted_at": null,
"title": "using loop",
"body": "tes body postman 2",
"UserID": 1,
}
]
}
],
"page": 0,
"size": 10,
"max_page": 1,
"total_pages": 1,
"total": 1,
"last": false,
"first": true,
"visible": 1
}
}
this is my user model
package models
import "gorm.io/gorm"
type User struct {
gorm.Model
Email string `gorm:"unique" json:"email"`
Username string `gorm:"unique" json:"username"`
Password string `json:"-"`
Names string `json:"names"`
Post []Post `gorm:"Foreignkey:UserID;association_foreignkey:ID;" json:"posts"`
}
this is my post model
package models
import (
"time"
"gorm.io/gorm"
)
type Post struct {
ID uint `gorm:"primaryKey" json:"id"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
DeletedAt gorm.DeletedAt `gorm:"index" json:"deleted_at"`
Title string `json:"title"`
Body string `json:"body"`
UserID uint `gorm:"column:user_id"`
User User
}
and this is the handler to get all user data
func GetAllUser(c *fiber.Ctx) error {
var users []models.User
model := initializers.DB.Preload("Post").Find(&users)
return c.Status(fiber.StatusOK).JSON(fiber.Map{
"data": initializers.PG.With(model).Request(c.Request()).Response(&[]models.User{}),
})
}
i am using gofiber and gorm to create this code