package com.codenetworkz.blog.config;

import com.codenetworkz.blog.dtos.CategoryDto;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;

@Component
public class StringToCategoryDtoConverter implements Converter<String, CategoryDto> {

    private CategoryDto defaultCategoryDto = new CategoryDto();

    public StringToCategoryDtoConverter() {
        this.defaultCategoryDto = defaultCategoryDto;
    }

    @Override
    public CategoryDto convert(String source) {
        try {
            // Attempt to parse or convert the source string
            return new CategoryDto(Long.parseLong(source));
        } catch (Exception e) {
            // If conversion fails, return the default object
            return defaultCategoryDto;
        }
    }
}