package repository import ( "context" "kontor-api-gin/internal/models" "log/slog" "gorm.io/gorm" ) type CardSetRepository struct { db *gorm.DB } func (repository *CardSetRepository) Import(dtos models.CardSetDTOList) { slog.Info("CardSetRepository#Import") for index, dto := range dtos { slog.Info("importing", "index", index, "profile", dto) model := dto.ConvertDTOToModel() existing_item := models.CardSet{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 NewCardSetRepository(db *gorm.DB) *CardSetRepository { repo := &CardSetRepository{db: db} return repo } func (repository *CardSetRepository) FindAll(context context.Context) ([]models.CardSet, error) { var cardsets []models.CardSet result := repository.db.Find(&cardsets) slog.Info("CardSetRepository#FindAll", "count", result.RowsAffected) return cardsets, nil } func (repository *CardSetRepository) GetById(context context.Context, id string) (models.CardSet, error) { var cardset = models.CardSet{ID: id} slog.Info("CardSetRepository#GetById", "id", id) result := repository.db.First(&cardset) if result.Error != nil { slog.Info("CardSetRepository#GetById", "error", result.Error.Error()) } return cardset, result.Error }