package es.uv.saic.service;

import java.util.List;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import es.uv.saic.domain.Link;
import es.uv.saic.domain.LinkRepository;

@Service
public class LinkService {

    @Autowired
	private LinkRepository lr;

    public List<Link> findAll() {
		return this.lr.findAll();
	}

    public Link findById(Integer id){
        Optional<Link> l = this.lr.findById(id);
        if(!l.isEmpty()){
            return l.get();
        }
        return null;
    }

    public Link findByToken(String token){
        return this.lr.findByToken(token);
    }

    public List<Link> findByRuct(Integer ruct){
        return this.lr.findByRuct(ruct);
    }

    public Link increase(Link l){
        Integer visites = l.getVisites();
        l.setVisites(visites == null ? 1 : visites+1);
        return this.save(l);
    }

    public Link save(Link l){
        Link lnew = this.lr.save(l);
        this.lr.flush();
        return lnew;
    }

    public void delete(Link l){
        this.lr.delete(l);
        this.lr.flush();
    }

}
