refactoring
This commit is contained in:
@@ -1,7 +1,10 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"GoMembership/pkg/logger"
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type SubscriptionModel struct {
|
||||
@@ -17,3 +20,39 @@ type SubscriptionModel struct {
|
||||
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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user