package models import ( "GoMembership/pkg/logger" "time" "gorm.io/gorm" ) type SubscriptionModel struct { ID uint `json:"id" gorm:"primaryKey"` CreatedAt time.Time UpdatedAt time.Time Name string `gorm:"uniqueIndex:idx_subscriptions_name" json:"name" binding:"required,safe_content"` Details string `json:"details" binding:"safe_content"` Conditions string `json:"conditions" binding:"safe_content"` RequiredMembershipField string `json:"required_membership_field" binding:"safe_content"` MonthlyFee float32 `json:"monthly_fee"` HourlyRate float32 `json:"hourly_rate"` IncludedPerYear int16 `json:"included_hours_per_year"` IncludedPerMonth int16 `json:"included_hours_per_month"` } func (s *SubscriptionModel) 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(s).Error; err != nil { return err } logger.Info.Printf("SubscriptionModel created: %#v", s) // Preload all associations to retuvn the fully populated User return tx. First(s, s.ID).Error // Refresh the user object with all associations }) } func (s *SubscriptionModel) Update(db *gorm.DB) error { return db.Transaction(func(tx *gorm.DB) error { // Check if the user exists in the database var existingSubscriptionModel SubscriptionModel logger.Info.Printf("updating SubscriptionModel: %#v", s) if err := tx.First(&existingSubscriptionModel, s.ID).Error; err != nil { return err } if err := tx.Model(&existingSubscriptionModel).Updates(s).Error; err != nil { return err } return tx.First(s, s.ID).Error }) } func (s *SubscriptionModel) Delete(db *gorm.DB) error { return db.Delete(&s).Error }