package com.eshore.gringotts.web.domain.resource.service;

import com.eshore.gringotts.web.domain.base.service.SimpleServiceSupport;
import com.eshore.gringotts.web.domain.resource.entity.DataResource;
import com.eshore.gringotts.web.domain.resource.entity.format.ResourceFormat;
import com.eshore.gringotts.web.domain.resource.entity.type.ResourceType;
import com.eshore.gringotts.web.domain.resource.repository.DataResourceRepository;
import com.eshore.gringotts.web.domain.resource.repository.ResourceFormatRepository;
import com.eshore.gringotts.web.domain.resource.repository.ResourceTypeRepository;
import com.eshore.gringotts.web.domain.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * 数据资源服务
 *
 * @author lanyuanxiaoyao
 */
@Slf4j
@Service
public class DataResourceService extends SimpleServiceSupport<DataResource> {
    private final ResourceTypeRepository resourceTypeRepository;
    private final ResourceFormatRepository resourceFormatRepository;

    public DataResourceService(DataResourceRepository repository, ResourceTypeRepository resourceTypeRepository, ResourceFormatRepository resourceFormatRepository, UserService userService) {
        super(repository, userService);
        this.resourceTypeRepository = resourceTypeRepository;
        this.resourceFormatRepository = resourceFormatRepository;
    }

    @Override
    public Long save(DataResource entity) {
        ResourceType type = resourceTypeRepository.save(entity.getType());
        ResourceFormat format = resourceFormatRepository.save(entity.getFormat());
        entity.setType(type);
        entity.setFormat(format);
        return super.save(entity);
    }
}
