added validation; DB is global now
This commit is contained in:
@@ -1,27 +1,40 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
"GoMembership/internal/database"
|
||||
|
||||
"GoMembership/internal/models"
|
||||
)
|
||||
|
||||
type SubscriptionModelsRepository interface {
|
||||
type SubscriptionModelsRepositoryInterface interface {
|
||||
CreateSubscriptionModel(subscriptionModel *models.SubscriptionModel) (int64, error)
|
||||
GetMembershipModelNames() ([]string, error)
|
||||
GetModelByName(modelname *string) (*models.SubscriptionModel, error)
|
||||
}
|
||||
|
||||
type subscriptionModelsRepository struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
type SubscriptionModelsRepository struct{}
|
||||
|
||||
func NewSubscriptionModelsRepository(db *gorm.DB) SubscriptionModelsRepository {
|
||||
return &subscriptionModelsRepository{db}
|
||||
}
|
||||
func (sr *SubscriptionModelsRepository) CreateSubscriptionModel(subscriptionModel *models.SubscriptionModel) (int64, error) {
|
||||
|
||||
func (repo *subscriptionModelsRepository) CreateSubscriptionModel(subscriptionModel *models.SubscriptionModel) (int64, error) {
|
||||
result := repo.db.Create(subscriptionModel)
|
||||
result := database.DB.Create(subscriptionModel)
|
||||
if result.Error != nil {
|
||||
return 0, result.Error
|
||||
}
|
||||
return subscriptionModel.ID, nil
|
||||
}
|
||||
|
||||
func (sr *SubscriptionModelsRepository) GetModelByName(modelname *string) (*models.SubscriptionModel, error) {
|
||||
var model models.SubscriptionModel
|
||||
if err := database.DB.Where("name = ?", modelname).First(&model).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &model, nil
|
||||
}
|
||||
|
||||
func (sr *SubscriptionModelsRepository) GetMembershipModelNames() ([]string, error) {
|
||||
var names []string
|
||||
if err := database.DB.Model(&models.SubscriptionModel{}).Pluck("name", &names).Error; err != nil {
|
||||
return []string{}, err
|
||||
}
|
||||
return names, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user