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