frontend: disabled button while processing password reset
This commit is contained in:
20
go-backend/internal/repositories/banking_repository.go
Normal file
20
go-backend/internal/repositories/banking_repository.go
Normal file
@@ -0,0 +1,20 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"GoMembership/internal/database"
|
||||
"GoMembership/internal/models"
|
||||
)
|
||||
|
||||
type BankAccountRepositoryInterface interface {
|
||||
CreateBankAccount(account *models.BankAccount) (uint, error)
|
||||
}
|
||||
|
||||
type BankAccountRepository struct{}
|
||||
|
||||
func (repo *BankAccountRepository) CreateBankAccount(account *models.BankAccount) (uint, error) {
|
||||
result := database.DB.Create(account)
|
||||
if result.Error != nil {
|
||||
return 0, result.Error
|
||||
}
|
||||
return account.ID, nil
|
||||
}
|
||||
21
go-backend/internal/repositories/consents_repository.go
Normal file
21
go-backend/internal/repositories/consents_repository.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"GoMembership/internal/database"
|
||||
"GoMembership/internal/models"
|
||||
)
|
||||
|
||||
type ConsentRepositoryInterface interface {
|
||||
CreateConsent(consent *models.Consent) (uint, error)
|
||||
}
|
||||
|
||||
type ConsentRepository struct{}
|
||||
|
||||
func (repo *ConsentRepository) CreateConsent(consent *models.Consent) (uint, error) {
|
||||
result := database.DB.Create(consent)
|
||||
|
||||
if result.Error != nil {
|
||||
return 0, result.Error
|
||||
}
|
||||
return consent.ID, nil
|
||||
}
|
||||
31
go-backend/internal/repositories/licence_repository.go
Normal file
31
go-backend/internal/repositories/licence_repository.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"GoMembership/internal/database"
|
||||
"GoMembership/internal/models"
|
||||
)
|
||||
|
||||
type LicenceInterface interface {
|
||||
FindCategoryByName(categoryName string) (models.Category, error)
|
||||
FindCategoriesByIDs(ids []uint) ([]models.Category, error)
|
||||
GetAllCategories() ([]models.Category, error)
|
||||
}
|
||||
|
||||
type LicenceRepository struct{}
|
||||
|
||||
func (r *LicenceRepository) GetAllCategories() ([]models.Category, error) {
|
||||
var categories []models.Category
|
||||
err := database.DB.Find(&categories).Error
|
||||
return categories, err
|
||||
}
|
||||
func (r *LicenceRepository) FindCategoriesByIDs(ids []uint) ([]models.Category, error) {
|
||||
var categories []models.Category
|
||||
err := database.DB.Where("id IN ?", ids).Find(&categories).Error
|
||||
return categories, err
|
||||
}
|
||||
|
||||
func (r *LicenceRepository) FindCategoryByName(categoryName string) (models.Category, error) {
|
||||
var category models.Category
|
||||
err := database.DB.Where("name = ?", categoryName).First(&category).Error
|
||||
return category, err
|
||||
}
|
||||
37
go-backend/internal/repositories/membership_repository.go
Normal file
37
go-backend/internal/repositories/membership_repository.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"GoMembership/internal/database"
|
||||
|
||||
"gorm.io/gorm"
|
||||
|
||||
"GoMembership/internal/models"
|
||||
)
|
||||
|
||||
type MembershipRepositoryInterface interface {
|
||||
CreateMembership(membership *models.Membership) (uint, error)
|
||||
FindMembershipByUserID(userID uint) (*models.Membership, error)
|
||||
}
|
||||
|
||||
type MembershipRepository struct{}
|
||||
|
||||
func (repo *MembershipRepository) CreateMembership(membership *models.Membership) (uint, error) {
|
||||
result := database.DB.Create(membership)
|
||||
if result.Error != nil {
|
||||
return 0, result.Error
|
||||
}
|
||||
return membership.ID, nil
|
||||
}
|
||||
|
||||
func (repo *MembershipRepository) FindMembershipByUserID(userID uint) (*models.Membership, error) {
|
||||
|
||||
var membership models.Membership
|
||||
result := database.DB.First(&membership, userID)
|
||||
if result.Error != nil {
|
||||
if result.Error == gorm.ErrRecordNotFound {
|
||||
return nil, gorm.ErrRecordNotFound
|
||||
}
|
||||
return nil, result.Error
|
||||
}
|
||||
return &membership, nil
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"GoMembership/internal/database"
|
||||
|
||||
"gorm.io/gorm"
|
||||
|
||||
"GoMembership/internal/models"
|
||||
)
|
||||
|
||||
type SubscriptionModelsRepositoryInterface interface {
|
||||
CreateSubscriptionModel(subscriptionModel *models.SubscriptionModel) (uint, error)
|
||||
UpdateSubscription(subscription *models.SubscriptionModel) (*models.SubscriptionModel, error)
|
||||
GetSubscriptionModelNames() ([]string, error)
|
||||
GetSubscriptions(where map[string]interface{}) (*[]models.SubscriptionModel, error)
|
||||
// GetUsersBySubscription(id uint) (*[]models.SubscriptionModel, error)
|
||||
DeleteSubscription(id *uint) 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) UpdateSubscription(subscription *models.SubscriptionModel) (*models.SubscriptionModel, error) {
|
||||
|
||||
result := database.DB.Model(&models.SubscriptionModel{ID: subscription.ID}).Updates(subscription)
|
||||
if result.Error != nil {
|
||||
return nil, result.Error
|
||||
}
|
||||
return subscription, nil
|
||||
}
|
||||
|
||||
func (sr *SubscriptionModelsRepository) DeleteSubscription(id *uint) error {
|
||||
|
||||
result := database.DB.Delete(&models.SubscriptionModel{}, id)
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func GetSubscriptionByName(modelname *string) (*models.SubscriptionModel, error) {
|
||||
var model models.SubscriptionModel
|
||||
result := database.DB.Where("name = ?", modelname).First(&model)
|
||||
if result.Error != nil {
|
||||
return nil, result.Error
|
||||
}
|
||||
return &model, nil
|
||||
}
|
||||
|
||||
func (sr *SubscriptionModelsRepository) GetSubscriptionModelNames() ([]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
|
||||
}
|
||||
|
||||
func GetUsersBySubscription(subscriptionID uint) (*[]models.User, error) {
|
||||
var users []models.User
|
||||
|
||||
err := database.DB.Preload("Membership").
|
||||
Preload("Membership.SubscriptionModel").
|
||||
Preload("BankAccount").
|
||||
Preload("Licence").
|
||||
Preload("Licence.Categories").
|
||||
Joins("JOIN memberships ON users.membership_id = memberships.id").
|
||||
Joins("JOIN subscription_models ON memberships.subscription_model_id = subscription_models.id").
|
||||
Where("subscription_models.id = ?", subscriptionID).
|
||||
Find(&users).Error
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &users, nil
|
||||
|
||||
}
|
||||
10
go-backend/internal/repositories/user_permissions.go
Normal file
10
go-backend/internal/repositories/user_permissions.go
Normal file
@@ -0,0 +1,10 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"GoMembership/internal/database"
|
||||
"GoMembership/internal/models"
|
||||
)
|
||||
|
||||
func (r *UserRepository) SetUserStatus(id uint, status uint) error {
|
||||
return database.DB.Model(&models.User{}).Where("id = ?", id).Update("status", status).Error
|
||||
}
|
||||
159
go-backend/internal/repositories/user_repository.go
Normal file
159
go-backend/internal/repositories/user_repository.go
Normal file
@@ -0,0 +1,159 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
|
||||
"GoMembership/internal/database"
|
||||
|
||||
"gorm.io/gorm/clause"
|
||||
|
||||
"GoMembership/internal/models"
|
||||
"GoMembership/pkg/errors"
|
||||
"GoMembership/pkg/logger"
|
||||
)
|
||||
|
||||
type UserRepositoryInterface interface {
|
||||
CreateUser(user *models.User) (uint, error)
|
||||
UpdateUser(user *models.User) (*models.User, error)
|
||||
GetUsers(where map[string]interface{}) (*[]models.User, error)
|
||||
GetUserByEmail(email string) (*models.User, error)
|
||||
IsVerified(userID *uint) (bool, error)
|
||||
GetVerificationOfToken(token *string, verificationType *string) (*models.Verification, error)
|
||||
SetVerificationToken(verification *models.Verification) (token string, err error)
|
||||
DeleteVerification(id uint, verificationType string) error
|
||||
DeleteUser(id uint) error
|
||||
SetUserStatus(id uint, status uint) error
|
||||
}
|
||||
|
||||
type UserRepository struct{}
|
||||
|
||||
func (ur *UserRepository) DeleteUser(id uint) error {
|
||||
return database.DB.Delete(&models.User{}, "id = ?", id).Error
|
||||
}
|
||||
|
||||
func PasswordExists(userID *uint) (bool, error) {
|
||||
var user models.User
|
||||
result := database.DB.Select("password").First(&user, userID)
|
||||
if result.Error != nil {
|
||||
return false, result.Error
|
||||
}
|
||||
return user.Password != "", nil
|
||||
}
|
||||
|
||||
func (ur *UserRepository) CreateUser(user *models.User) (uint, error) {
|
||||
result := database.DB.Create(user)
|
||||
if result.Error != nil {
|
||||
logger.Error.Printf("Create User error: %#v", result.Error)
|
||||
return 0, result.Error
|
||||
}
|
||||
return user.ID, nil
|
||||
}
|
||||
|
||||
func (ur *UserRepository) UpdateUser(user *models.User) (*models.User, error) {
|
||||
if user == nil {
|
||||
return nil, errors.ErrNoData
|
||||
}
|
||||
|
||||
err := database.DB.Transaction(func(tx *gorm.DB) error {
|
||||
// Check if the user exists in the database
|
||||
var existingUser models.User
|
||||
|
||||
if err := tx.Preload(clause.Associations).
|
||||
Preload("Membership").
|
||||
Preload("Membership.SubscriptionModel").
|
||||
Preload("Licence").
|
||||
Preload("Licence.Categories").
|
||||
First(&existingUser, user.ID).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
// Update the user's main fields
|
||||
result := tx.Session(&gorm.Session{FullSaveAssociations: true}).Omit("Password").Updates(user)
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
if result.RowsAffected == 0 {
|
||||
return errors.ErrNoRowsAffected
|
||||
}
|
||||
|
||||
if user.Password != "" {
|
||||
if err := tx.Model(&models.User{}).
|
||||
Where("id = ?", user.ID).
|
||||
Update("Password", user.Password).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// Update the Membership if provided
|
||||
if user.Membership.ID != 0 {
|
||||
if err := tx.Model(&existingUser.Membership).Updates(user.Membership).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// Replace categories if Licence and Categories are provided
|
||||
if user.Licence != nil {
|
||||
if err := tx.Model(&user.Licence).Association("Categories").Replace(user.Licence.Categories); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var updatedUser models.User
|
||||
if err := database.DB.Preload("Licence.Categories").
|
||||
Preload("Membership").
|
||||
First(&updatedUser, user.ID).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &updatedUser, nil
|
||||
}
|
||||
|
||||
func (ur *UserRepository) GetUsers(where map[string]interface{}) (*[]models.User, error) {
|
||||
var users []models.User
|
||||
result := database.DB.
|
||||
Preload(clause.Associations).
|
||||
Preload("Membership.SubscriptionModel").
|
||||
Preload("Licence.Categories").
|
||||
Where(where).Find(&users)
|
||||
if result.Error != nil {
|
||||
if result.Error == gorm.ErrRecordNotFound {
|
||||
return nil, gorm.ErrRecordNotFound
|
||||
}
|
||||
return nil, result.Error
|
||||
}
|
||||
return &users, nil
|
||||
}
|
||||
|
||||
func GetUserByID(userID *uint) (*models.User, error) {
|
||||
var user models.User
|
||||
result := database.DB.
|
||||
Preload(clause.Associations).
|
||||
Preload("Membership").
|
||||
Preload("Membership.SubscriptionModel").
|
||||
Preload("Licence.Categories").
|
||||
First(&user, userID)
|
||||
if result.Error != nil {
|
||||
if result.Error == gorm.ErrRecordNotFound {
|
||||
return nil, gorm.ErrRecordNotFound
|
||||
}
|
||||
return nil, result.Error
|
||||
}
|
||||
return &user, nil
|
||||
}
|
||||
|
||||
func (ur *UserRepository) GetUserByEmail(email string) (*models.User, error) {
|
||||
var user models.User
|
||||
result := database.DB.Where("email = ?", email).First(&user)
|
||||
if result.Error != nil {
|
||||
if result.Error == gorm.ErrRecordNotFound {
|
||||
return nil, gorm.ErrRecordNotFound
|
||||
}
|
||||
return nil, result.Error
|
||||
}
|
||||
return &user, nil
|
||||
}
|
||||
57
go-backend/internal/repositories/user_verification.go
Normal file
57
go-backend/internal/repositories/user_verification.go
Normal file
@@ -0,0 +1,57 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"GoMembership/internal/constants"
|
||||
"GoMembership/internal/database"
|
||||
"GoMembership/internal/models"
|
||||
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
)
|
||||
|
||||
func (ur *UserRepository) IsVerified(userID *uint) (bool, error) {
|
||||
var user models.User
|
||||
result := database.DB.Select("status").First(&user, userID)
|
||||
if result.Error != nil {
|
||||
if result.Error == gorm.ErrRecordNotFound {
|
||||
return false, gorm.ErrRecordNotFound
|
||||
}
|
||||
return false, result.Error
|
||||
}
|
||||
return user.Status > constants.DisabledStatus, nil
|
||||
}
|
||||
|
||||
func (ur *UserRepository) GetVerificationOfToken(token *string, verificationType *string) (*models.Verification, error) {
|
||||
|
||||
var emailVerification models.Verification
|
||||
result := database.DB.Where("verification_token = ? AND type = ?", token, verificationType).First(&emailVerification)
|
||||
if result.Error != nil {
|
||||
if result.Error == gorm.ErrRecordNotFound {
|
||||
return nil, gorm.ErrRecordNotFound
|
||||
}
|
||||
return nil, result.Error
|
||||
}
|
||||
return &emailVerification, nil
|
||||
}
|
||||
|
||||
func (ur *UserRepository) SetVerificationToken(verification *models.Verification) (token string, err error) {
|
||||
|
||||
result := database.DB.Clauses(clause.OnConflict{
|
||||
Columns: []clause.Column{{Name: "user_id"}},
|
||||
DoUpdates: clause.AssignmentColumns([]string{"verification_token", "created_at", "type"}),
|
||||
}).Create(&verification)
|
||||
|
||||
if result.Error != nil {
|
||||
return "", result.Error
|
||||
}
|
||||
|
||||
return verification.VerificationToken, nil
|
||||
}
|
||||
|
||||
func (ur *UserRepository) DeleteVerification(id uint, verificationType string) error {
|
||||
result := database.DB.Where("user_id = ? AND type = ?", id, verificationType).Delete(&models.Verification{})
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user