made Licence optional
This commit is contained in:
@@ -26,6 +26,6 @@ func (r *LicenceRepository) FindCategoriesByIDs(ids []uint) ([]models.Category,
|
||||
|
||||
func (r *LicenceRepository) FindCategoryByName(categoryName string) (models.Category, error) {
|
||||
var category models.Category
|
||||
err := database.DB.Where("category = ?", categoryName).First(&category).Error
|
||||
err := database.DB.Where("name = ?", categoryName).First(&category).Error
|
||||
return category, err
|
||||
}
|
||||
|
||||
@@ -42,7 +42,8 @@ func (ur *UserRepository) UpdateUser(user *models.User) (*models.User, error) {
|
||||
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("Licence.Categories").
|
||||
if err := tx.Preload("Licence").
|
||||
Preload("Licence.Categories").
|
||||
Preload("Membership").
|
||||
First(&existingUser, user.ID).Error; err != nil {
|
||||
return err
|
||||
@@ -56,13 +57,29 @@ func (ur *UserRepository) UpdateUser(user *models.User) (*models.User, error) {
|
||||
return errors.ErrNoRowsAffected
|
||||
}
|
||||
|
||||
// Handle the update of the LicenceCategories explicitly
|
||||
if user.Licence.ID != 0 {
|
||||
// Replace the Categories with the new list
|
||||
if err := tx.Model(&existingUser.Licence).Association("Categories").Replace(user.Licence.Categories); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
// Handle the update or creation of Licence and its Categories
|
||||
// if user.Licence != nil {
|
||||
// if existingUser.Licence == nil {
|
||||
// // Create new Licence if it doesn't exist
|
||||
// logger.Error.Printf("Licence creation: %+v", user.Licence)
|
||||
// if err := tx.Create(user.Licence).Error; err != nil {
|
||||
// return err
|
||||
// }
|
||||
// // Update user with new licence ID
|
||||
// // if err := tx.Model(&existingUser).Update("licence_id", user.Licence.ID).Error; err != nil {
|
||||
// // return err
|
||||
// // }
|
||||
// } else {
|
||||
// // Update existing licence
|
||||
// if err := tx.Model(&existingUser.Licence).Updates(user.Licence).Error; err != nil {
|
||||
// return err
|
||||
// }
|
||||
// }
|
||||
// // Replace the Categories with the new list
|
||||
// if err := tx.Model(&existingUser.Licence).Association("Categories").Replace(user.Licence.Categories); err != nil {
|
||||
// return err
|
||||
// }
|
||||
// }
|
||||
|
||||
// Update the Membership if provided
|
||||
if user.Membership.ID != 0 {
|
||||
@@ -71,13 +88,6 @@ func (ur *UserRepository) UpdateUser(user *models.User) (*models.User, error) {
|
||||
}
|
||||
}
|
||||
|
||||
// Update the Licence fields if provided
|
||||
if user.Licence.ID != 0 {
|
||||
if err := tx.Model(&existingUser.Licence).Updates(user.Licence).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user