add methods in Repository and Model for importing data
This commit is contained in:
@@ -12,6 +12,11 @@ type PermissionRepository struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
func NewPermissionRepository(db *gorm.DB) *PermissionRepository {
|
||||
repository := &PermissionRepository{db: db}
|
||||
return repository
|
||||
}
|
||||
|
||||
func (repository *PermissionRepository) FindAll(context context.Context) ([]models.Permission, error) {
|
||||
|
||||
var permissions []models.Permission
|
||||
@@ -22,7 +27,25 @@ func (repository *PermissionRepository) FindAll(context context.Context) ([]mode
|
||||
return permissions, nil
|
||||
}
|
||||
|
||||
func NewPermissionRepository(db *gorm.DB) *PermissionRepository {
|
||||
repository := &PermissionRepository{db: db}
|
||||
return repository
|
||||
func (repository *PermissionRepository) Import(permissions models.PermissionDTOList) {
|
||||
slog.Info("PermissionRepository#Import")
|
||||
for index, permission := range permissions {
|
||||
slog.Info("importing", "index", index, "profile", permission)
|
||||
model := permission.ConvertDTOToModel()
|
||||
existing_item := models.Permission{ID: permission.ID}
|
||||
result_existing := repository.db.First(&existing_item)
|
||||
if result_existing.Error != nil {
|
||||
slog.Info("check existing id: not found - inserting")
|
||||
result := repository.db.Create(&model)
|
||||
if result.Error != nil {
|
||||
slog.Info("error during import", "error", result.Error.Error())
|
||||
}
|
||||
} else {
|
||||
slog.Info("check existing id: found - updating")
|
||||
result := repository.db.Save(&model)
|
||||
if result.Error != nil {
|
||||
slog.Info("error during updating", "error", result.Error.Error())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user