56 lines
1.7 KiB
Go
56 lines
1.7 KiB
Go
package repositories
|
|
|
|
import (
|
|
"GoMembership/internal/database"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
"GoMembership/internal/models"
|
|
)
|
|
|
|
type SubscriptionModelsRepositoryInterface interface {
|
|
CreateSubscriptionModel(subscriptionModel *models.SubscriptionModel) (uint, error)
|
|
GetMembershipModelNames() ([]string, error)
|
|
GetModelByName(modelname *string) (*models.SubscriptionModel, error)
|
|
GetSubscriptions(where map[string]interface{}) (*[]models.SubscriptionModel, error)
|
|
}
|
|
|
|
type SubscriptionModelsRepository struct{}
|
|
|
|
func (sr *SubscriptionModelsRepository) CreateSubscriptionModel(subscriptionModel *models.SubscriptionModel) (uint, error) {
|
|
|
|
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
|
|
}
|
|
|
|
func (sr *SubscriptionModelsRepository) GetSubscriptions(where map[string]interface{}) (*[]models.SubscriptionModel, error) {
|
|
var subscriptions []models.SubscriptionModel
|
|
result := database.DB.Where(where).Find(&subscriptions)
|
|
if result.Error != nil {
|
|
if result.Error == gorm.ErrRecordNotFound {
|
|
return nil, gorm.ErrRecordNotFound
|
|
}
|
|
return nil, result.Error
|
|
}
|
|
return &subscriptions, nil
|
|
}
|