package com.izouma.nineth.converter;

import com.alibaba.fastjson.JSON;
import com.izouma.nineth.enums.Rarity;
import org.apache.commons.lang3.StringUtils;

import javax.persistence.AttributeConverter;
import javax.persistence.Converter;
import java.util.List;

@Converter
public class RarityConverter implements AttributeConverter<List<Rarity>, String> {
    @Override
    public String convertToDatabaseColumn(List<Rarity> rarity) {
        if (rarity != null) {
            return JSON.toJSONString(rarity);
        }
        return null;
    }

    @Override
    public List<Rarity> convertToEntityAttribute(String s) {
        if (StringUtils.isNotEmpty(s)) {
            try {
                return JSON.parseArray(s, Rarity.class);
            } catch (Exception ignored) {
            }
        }
        return null;
    }
}
