package models import ( "GoMembership/pkg/logger" "time" "gorm.io/gorm" ) type Insurance struct { ID uint `gorm:"primary_key" json:"id"` Cars []Car `gorm:"many2many:car_insurances;" json:"-"` Company string `json:"company" binding:"safe_content"` Reference string `json:"reference" binding:"safe_content"` Notes string `json:"notes" binding:"safe_content"` StartDate time.Time `json:"start_date"` EndDate time.Time `json:"end_date"` } func (i *Insurance) Create(db *gorm.DB) error { return db.Transaction(func(tx *gorm.DB) error { // Create the base User record (omit associations to handle them separately) if err := tx.Create(i).Error; err != nil { return err } logger.Info.Printf("Insurance created: %#v", i) // Preload all associations to return the fully populated User return tx. First(i, i.ID).Error // Refresh the user object with all associations }) } func (i *Insurance) Update(db *gorm.DB) error { return db.Transaction(func(tx *gorm.DB) error { // Check if the user exists in the database var existingInsurance Insurance logger.Info.Printf("updating Insurance: %#v", i) if err := tx.First(&existingInsurance, i.ID).Error; err != nil { return err } if err := tx.Model(&existingInsurance).Updates(i).Error; err != nil { return err } return tx.First(i, i.ID).Error }) } func (i *Insurance) Delete(db *gorm.DB) error { return db.Delete(&i).Error }