57 lines
1.5 KiB
Go
57 lines
1.5 KiB
Go
package models
|
|
|
|
import (
|
|
"GoMembership/pkg/logger"
|
|
"strings"
|
|
"time"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type Consent struct {
|
|
ID uint `gorm:"primaryKey" json:"id"`
|
|
CreatedAt time.Time
|
|
UpdatedAt time.Time
|
|
FirstName string `gorm:"not null" json:"first_name" binding:"safe_content"`
|
|
LastName string `gorm:"not null" json:"last_name" binding:"safe_content"`
|
|
Email string `json:"email" binding:"email,safe_content"`
|
|
ConsentType string `gorm:"not null" json:"consent_type" binding:"safe_content"`
|
|
UserID *uint `json:"user_id"`
|
|
User *User `gorm:"constraint:OnUpdate:CASCADE,OnDelete:SET NULL;" json:"-" binding:"-"`
|
|
}
|
|
|
|
func (c *Consent) BeforeSave(tx *gorm.DB) (err error) {
|
|
c.Email = strings.ToLower(c.Email)
|
|
return nil
|
|
}
|
|
func (c *Consent) Create(db *gorm.DB) error {
|
|
if err := db.Create(c).Error; err != nil {
|
|
return err
|
|
}
|
|
logger.Info.Printf("Consent created: %#v", c)
|
|
return db.First(c, c.ID).Error // Refresh the user object with all associations
|
|
}
|
|
|
|
func (c *Consent) Update(db *gorm.DB) error {
|
|
|
|
return db.Transaction(func(tx *gorm.DB) error {
|
|
// Check if the user exists in the database
|
|
var existingConsent Consent
|
|
|
|
logger.Info.Printf("updating Consent: %#v", c)
|
|
if err := tx.First(&existingConsent, c.ID).Error; err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := tx.Model(&existingConsent).Updates(c).Error; err != nil {
|
|
return err
|
|
}
|
|
return tx.First(c, c.ID).Error
|
|
})
|
|
|
|
}
|
|
|
|
func (c *Consent) Delete(db *gorm.DB) error {
|
|
return db.Delete(&c).Error
|
|
}
|