package es.uv.dagarcos.master.service;

import es.uv.dagarcos.master.repository.PlayerGlobalRepository;
import es.uv.dagarcos.master.domain.PlayerGlobal;

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

@Service
public class PlayerGlobalService {

    @Autowired
    private PlayerGlobalRepository playerRepository;

    public PlayerGlobal getOrCreate(String externalPlayerId) {

        PlayerGlobal player = playerRepository
                .findByExternalPlayerId(externalPlayerId)
                .orElseGet(() -> playerRepository.save(
                        new PlayerGlobal(externalPlayerId)
                ));

        player.touch();
        playerRepository.save(player);

        return player;
    }
}


