package com.ffii.core.common.mobile.service;

import java.util.Optional;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ffii.core.common.mobile.dao.AccessTokenDao;
import com.ffii.core.common.mobile.entity.AccessToken;
import com.ffii.core.support.AbstractIdEntityService;
import com.ffii.core.support.JdbcDao;

/** @author Fung */
@Service
public class AccessTokenService extends AbstractIdEntityService<AccessToken, Integer, AccessTokenDao> {
  public AccessTokenService(JdbcDao jdbcDao, AccessTokenDao dao) {
    super(jdbcDao, dao);
  }

  public Optional<AccessToken> findByDeviceIdAndAccessToken(String deviceId, String accessToken) {
    return dao.findByDeviceIdAndAccessToken(deviceId, accessToken);
  }

  @Transactional(rollbackFor = Exception.class)
  public void logout(String deviceId, String accessToken) {
    dao.deleteByDeviceIdAndAccessToken(deviceId, accessToken);
  }

  @Transactional(rollbackFor = Exception.class)
  public void logoutAll(String username) {
    dao.deleteByUsername(username);
  }
}
