forked from Cookies_Github_mirror/AquaDX
[O] Refactor code
This commit is contained in:
@@ -1,70 +0,0 @@
|
||||
package icu.samnyan.aqua.sega.general.service;
|
||||
|
||||
import icu.samnyan.aqua.sega.general.dao.CardRepository;
|
||||
import icu.samnyan.aqua.sega.general.model.Card;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
|
||||
/**
|
||||
* @author samnyan (privateamusement@protonmail.com)
|
||||
*/
|
||||
@Service
|
||||
public class CardService {
|
||||
|
||||
private final CardRepository cardRepository;
|
||||
|
||||
@Autowired
|
||||
public CardService(CardRepository cardRepository) {
|
||||
this.cardRepository = cardRepository;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find a card by External Id
|
||||
* @param extId External Id
|
||||
* @return Optional of a Card
|
||||
*/
|
||||
public Optional<Card> getCardByExtId(String extId) {
|
||||
return cardRepository.findByExtId(Long.parseLong(extId));
|
||||
}
|
||||
|
||||
/**
|
||||
* Find a card by External Id
|
||||
*
|
||||
* @param extId External Id
|
||||
* @return Optional of a Card
|
||||
*/
|
||||
public Optional<Card> getCardByExtId(Long extId) {
|
||||
return cardRepository.findByExtId(extId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Find a card by it's access code
|
||||
* @param accessCode String represent of a access code
|
||||
* @return Optional of a Card
|
||||
*/
|
||||
public Optional<Card> getCardByAccessCode(String accessCode) {
|
||||
return cardRepository.findByLuid(accessCode);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a new card with access code
|
||||
* @param accessCode String represent of a access code
|
||||
* @return a new registered Card
|
||||
*/
|
||||
public Card registerByAccessCode(String accessCode) {
|
||||
Card card = new Card();
|
||||
card.setLuid(accessCode);
|
||||
long extId = ThreadLocalRandom.current().nextLong(99999999);
|
||||
while (cardRepository.findByExtId(extId).isPresent()) {
|
||||
extId = ThreadLocalRandom.current().nextLong(99999999);
|
||||
}
|
||||
card.setExtId(extId);
|
||||
card.setRegisterTime(LocalDateTime.now());
|
||||
card.setAccessTime(LocalDateTime.now());
|
||||
return cardRepository.save(card);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package icu.samnyan.aqua.sega.general.service
|
||||
|
||||
import icu.samnyan.aqua.sega.general.dao.CardRepository
|
||||
import icu.samnyan.aqua.sega.general.model.Card
|
||||
import org.springframework.stereotype.Service
|
||||
import java.time.LocalDateTime
|
||||
import java.util.*
|
||||
import java.util.concurrent.ThreadLocalRandom
|
||||
|
||||
/**
|
||||
* @author samnyan (privateamusement@protonmail.com)
|
||||
*/
|
||||
@Service
|
||||
class CardService(val cardRepo: CardRepository) {
|
||||
/**
|
||||
* Find a card by External Id
|
||||
* @param extId External Id
|
||||
* @return Optional of a Card
|
||||
*/
|
||||
fun getCardByExtId(extId: String): Optional<Card> = cardRepo.findByExtId(extId.toLong())
|
||||
|
||||
/**
|
||||
* Find a card by External Id
|
||||
*
|
||||
* @param extId External Id
|
||||
* @return Optional of a Card
|
||||
*/
|
||||
fun getCardByExtId(extId: Long?): Optional<Card> = cardRepo.findByExtId(extId)
|
||||
|
||||
/**
|
||||
* Find a card by it's access code
|
||||
* @param accessCode String represent of a access code
|
||||
* @return Optional of a Card
|
||||
*/
|
||||
fun getCardByAccessCode(accessCode: String?): Optional<Card> = cardRepo.findByLuid(accessCode)
|
||||
|
||||
/**
|
||||
* Register a new card with access code
|
||||
* @param accessCode String represent of a access code
|
||||
* @return a new registered Card
|
||||
*/
|
||||
fun registerByAccessCode(accessCode: String?): Card {
|
||||
val card = Card()
|
||||
card.luid = accessCode
|
||||
var extId = ThreadLocalRandom.current().nextLong(99999999)
|
||||
while (cardRepo.findByExtId(extId).isPresent) {
|
||||
extId = ThreadLocalRandom.current().nextLong(99999999)
|
||||
}
|
||||
card.extId = extId
|
||||
card.registerTime = LocalDateTime.now()
|
||||
card.accessTime = LocalDateTime.now()
|
||||
return cardRepo.save(card)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user