I have an api for multiple creation of user call logs. The problem i have is in having the UserID to be included in the CreateInBatches
My API payload is as below
[
{
"formatted_number":"+1xxxxxxxx09",
"cached_matched_number":"+1xxxxxxx09",
},
{
"formatted_number":"+1xxxxxxxx11",
"cached_matched_number":"+1xxxxxxx11",
},
{
"formatted_number":"+1xxxxxxxx10",
"cached_matched_number":"+1xxxxxxx10"
}]
I want the logged person to be able to save this data at once.
Below is my struct
type UserCallLog struct {
ID uuid.UUID `gorm:"type:uuid;default:uuid_generate_v4();primaryKey"`
FormattedNumber string `json:"formatted_number" gorm:"type:varchar(255);index"`
CachedMatchedNumber string `json:"cached_matched_number" gorm:"type:varchar(255);index"`
UserID uuid.UUID `json:"user_id" binding:"omitempty,uuid4" gorm:"index"`
User User `gorm:"foreignKey:UserID;references:ID"`
CreatedAt time.Time
UpdatedAt time.Time
}
Kindly help me know what the problem in my code as below
func CreateCallLogs(c *gin.Context) {
user_id, _ := c.Get("id")
var callmetrics []models.UserCallLogMetric
var err []string
jsonError := c.ShouldBindJSON(&callmetrics)
if jsonError != nil {
err = append(err, utilities.Validate(jsonError)...)
utilities.ShowError(c, http.StatusBadRequest, err)
return
}
callmetrics.UserID = user_id.(uuid.UUID)
result :=database.DB.CreateInBatches(callmetrics, 100)
if result.Error != nil {
err = append(err, result.Error.Error())
utilities.ShowError(c, http.StatusBadRequest, err)
return
}
utilities.ShowMessage(c, http.StatusOK, "call logs created successfully")
}
I will greatly appreciate the help