add methods in Repository and Model for importing data
This commit is contained in:
@@ -36,3 +36,26 @@ func (repository *ProfileRepository) FindProfileByEmail(email string) (*models.P
|
||||
}
|
||||
return &profile, nil
|
||||
}
|
||||
|
||||
func (repository *ProfileRepository) Import(profiles models.ProfileDTOList) {
|
||||
slog.Info("ProfileRepository#Import")
|
||||
for index, profile := range profiles {
|
||||
slog.Info("importing", "index", index, "profile", profile)
|
||||
model := profile.ConvertDTOToModel()
|
||||
existing_profile := models.Profile{ID: profile.ID}
|
||||
result_existing := repository.db.First(&existing_profile)
|
||||
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