package com.casic.PgInterface.devTable.manager;

import com.casic.PgInterface.core.hibernate.HibernateEntityDao;
import com.casic.PgInterface.devTable.domain.PgRoad;
import com.casic.PgInterface.devTable.dto.PgRoadDto;
import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;

@Service
public class PgRoadManager extends HibernateEntityDao<PgRoad> {

    public List<PgRoadDto> getAllRoad(){
        Criteria criteria = createCriteria(PgRoad.class);
        criteria.add(Restrictions.eq("active", 1));
        criteria.addOrder(Order.desc("id"));

        List<PgRoad> pgRoadList = criteria.list();
        List<PgRoadDto> pgRoadDtoList = new ArrayList<PgRoadDto>();
        for (PgRoad pgRoad : pgRoadList) {
            pgRoadDtoList.add(PgRoadDto.ConvertToDTO(pgRoad));
        }

        return pgRoadDtoList;
    }

    public PgRoad getPgRoadByName(String roadName){
        Criteria criteria=createCriteria(PgRoad.class);

        criteria.add(Restrictions.eq("roadName",roadName));
        List<PgRoad> pgRoadList=criteria.list();

        if(pgRoadList!=null&&pgRoadList.size()!=0)
            return pgRoadList.get(0);
        else
            return null;
    }



}