add methods in Repository and Model for importing data
This commit is contained in:
@@ -35,3 +35,26 @@ func (repository *AssignmentRepository) GetById(context context.Context, id stri
|
||||
}
|
||||
return assignment, result.Error
|
||||
}
|
||||
|
||||
func (repository *AssignmentRepository) Import(assignments models.AssignmentDTOList) {
|
||||
slog.Info("AssignmentRepository#Import")
|
||||
for index, assignment := range assignments {
|
||||
slog.Info("importing", "index", index, "profile", assignment)
|
||||
model := assignment.ConvertDTOToModel()
|
||||
existing_item := models.Assignment{ID: assignment.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