package repository import ( "context" "kontor-api-echo/internal/models" "log/slog" "github.com/uptrace/bun" ) type AssignmentRepository struct { db *bun.DB } func NewAssignmentRepository(db *bun.DB) *AssignmentRepository { repository := &AssignmentRepository{db: db} return repository } func (repository *AssignmentRepository) FindAll(context context.Context) ([]models.Assignment, error) { var assignments []models.Assignment result := repository.db.Find(&assignments) slog.Info("AssignmentRepository#FindAll", "count", result.RowsAffected) return assignments, nil } func (repository *AssignmentRepository) GetById(context context.Context, id string) (models.Assignment, error) { var assignment = models.Assignment{ID: id} slog.Info("AssignmentRepository#GetById", "id", id) result := repository.db.First(&assignment) if result.Error != nil { slog.Info("AssignmentRepository#GetById", "error", result.Error.Error()) } 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()) } } } }