add methods in Repository and Model for importing data
This commit is contained in:
@@ -12,6 +12,29 @@ type TokenRepository struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
func (repository *TokenRepository) Import(dtos models.TokenDTOList) {
|
||||
slog.Info("TokenRepository#Import")
|
||||
for index, dto := range dtos {
|
||||
slog.Info("importing", "index", index, "video", dto)
|
||||
model := dto.ConvertDTOToModel()
|
||||
existing_item := models.Token{ID: dto.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())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func NewTokenRepository(db *gorm.DB) *TokenRepository {
|
||||
repository := &TokenRepository{db: db}
|
||||
return repository
|
||||
|
||||
Reference in New Issue
Block a user