package com.eactive.httpmockserver.client.service;

import com.eactive.httpmockserver.client.repository.ApiRequestRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;


@Service
@Transactional
public class ApiRequestMigrationService {

    @Autowired
    ApiRequestRepository apiRequestRepository;

    @EventListener(ApplicationReadyEvent.class)
    public void migrate() {
        apiRequestRepository.findAll().forEach(apiRequest -> {

            apiRequest.setHeaders(apiRequest.getHeaders().replaceAll("=", "::"));
            apiRequest.setQueryParams(apiRequest.getQueryParams().replaceAll("=", "::"));

            apiRequestRepository.save(apiRequest);
        });

    }

}
