package repository import ( "context" "kontor-api-gin/internal/models" "log/slog" "gorm.io/gorm" ) type ArticleAuthorRepository struct { db *gorm.DB } func NewArticleAuthorRepository(db *gorm.DB) *ArticleAuthorRepository { repo := &ArticleAuthorRepository{db: db} return repo } func (repository *ArticleAuthorRepository) FindAll(context context.Context) ([]models.ArticleAuthor, error) { var articleAuthors []models.ArticleAuthor result := repository.db.Find(&articleAuthors) slog.Info("ArticleAuthorRepository#FindAll", "count", result.RowsAffected) return articleAuthors, nil } func (repository *ArticleAuthorRepository) Import(dtos models.ArticleAuthorDTOList) { slog.Info("ArticleAuthorRepository#Import") for index, dto := range dtos { slog.Info("importing", "index", index, "profile", dto) model := dto.ConvertDTOToModel() existing_item := models.ArticleAuthor{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()) } } } }