package com.izouma.nineth.service.openapi;

import com.izouma.nineth.dto.openapi.TransferCollectionDTO;
import com.izouma.nineth.repo.TransferCollectionRepo;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Iterator;
import java.util.List;

/**
 * openApi Service
 *
 * @author ZouYuHong
 */
@Slf4j
@Service
@AllArgsConstructor
public class OpenApiService {

    private final TransferCollectionRepo collectionRepo;


    public List<TransferCollectionDTO> transferCollections(Long projectId) {
        List<TransferCollectionDTO> list = collectionRepo.transferCollections(projectId);
        if (list != null && !list.isEmpty()) {
            Iterator<TransferCollectionDTO> iterator = list.iterator();
            while (iterator.hasNext()) {
                TransferCollectionDTO item = iterator.next();
                if (item != null && (item.getPic() != null && !item.getPic().isEmpty())) {
                    item.setImg(item.getPic().get(0).getUrl());
                    item.setPic(null);
                }
                if (item == null){
                    iterator.remove();
                }
            }
        }
        return list;
    }
}
