wip
This commit is contained in:
58
go-backend/internal/models/subscription.go
Normal file
58
go-backend/internal/models/subscription.go
Normal file
@@ -0,0 +1,58 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"GoMembership/pkg/logger"
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type Subscription 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 *Subscription) 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("Subscription 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 *Subscription) Update(db *gorm.DB) error {
|
||||
|
||||
return db.Transaction(func(tx *gorm.DB) error {
|
||||
// Check if the user exists in the database
|
||||
var existingSubscription Subscription
|
||||
|
||||
logger.Info.Printf("updating Subscription: %#v", s)
|
||||
if err := tx.First(&existingSubscription, s.ID).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := tx.Model(&existingSubscription).Updates(s).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
return tx.First(s, s.ID).Error
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
func (s *Subscription) Delete(db *gorm.DB) error {
|
||||
return db.Delete(&s).Error
|
||||
}
|
||||
Reference in New Issue
Block a user