refactoring

This commit is contained in:
Alex
2025-03-24 17:45:33 +01:00
parent 741145b960
commit 28dfe7ecde
11 changed files with 528 additions and 155 deletions

View File

@@ -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
}