embed QRCodeTypeEntity to QRCodeEntity

This commit is contained in:
heyethereum
2024-07-19 00:45:44 +08:00
parent 2771ac4f73
commit 0af328977b
17 changed files with 40 additions and 48 deletions

View File

@@ -26,14 +26,14 @@ public class QRCodeEntity {
@Column(updatable = false, nullable = false)
private UUID id;
@JsonIgnore
@Column(name = "qr_code_type_id", nullable = false)
private Long qrCodeTypeId;
@JsonIgnore
private String userId;
private String contents;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "qr_code_type_id", nullable = false)
private QRCodeTypeEntity info;
@Column(name = "created_at", insertable = false, updatable = false)
private LocalDateTime createdAt;
}

View File

@@ -20,9 +20,8 @@ public class EmailModel extends QRCodeModel {
EmailEntity details;
public EmailModel(QRCodeEntity scannedQRCodeEntity, QRCodeTypeEntity qrCodeTypeEntity, EmailVerificationService emailVerificationService) {
public EmailModel(QRCodeEntity scannedQRCodeEntity, EmailVerificationService emailVerificationService) {
this.scannedQRCode = scannedQRCodeEntity;
this.qrCode = qrCodeTypeEntity;
this.emailVerificationService = emailVerificationService;
this.details = null;
}

View File

@@ -20,9 +20,8 @@ public class PhoneModel extends QRCodeModel {
PhoneEntity details;
public PhoneModel(QRCodeEntity scannedQRCodeEntity, QRCodeTypeEntity qrCodeTypeEntity, PhoneVerificationService phoneVerificationService) {
public PhoneModel(QRCodeEntity scannedQRCodeEntity, PhoneVerificationService phoneVerificationService) {
this.scannedQRCode = scannedQRCodeEntity;
this.qrCode = qrCodeTypeEntity;
this.phoneVerificationService = phoneVerificationService;
this.details = null;
}

View File

@@ -7,7 +7,5 @@ import lombok.Data;
@Data
public abstract class QRCodeModel {
QRCodeEntity scannedQRCode;
QRCodeTypeEntity qrCode;
public abstract void setDetails();
}

View File

@@ -20,9 +20,8 @@ public class SMSModel extends QRCodeModel {
SMSEntity details;
public SMSModel(QRCodeEntity scannedQRCodeEntity, QRCodeTypeEntity qrCodeTypeEntity, SMSVerificationService smsVerificationService) {
public SMSModel(QRCodeEntity scannedQRCodeEntity, SMSVerificationService smsVerificationService) {
this.scannedQRCode = scannedQRCodeEntity;
this.qrCode = qrCodeTypeEntity;
this.smsVerificationService = smsVerificationService;
this.details = null;
}

View File

@@ -20,9 +20,8 @@ public class TextModel extends QRCodeModel {
TextEntity details;
public TextModel(QRCodeEntity scannedQRCodeEntity, QRCodeTypeEntity qrCodeTypeEntity, TextVerificationService textVerificationService) {
public TextModel(QRCodeEntity scannedQRCodeEntity, TextVerificationService textVerificationService) {
this.scannedQRCode = scannedQRCodeEntity;
this.qrCode = qrCodeTypeEntity;
this.textVerificationService = textVerificationService;
this.details = null;
}

View File

@@ -12,7 +12,6 @@ import org.springframework.beans.factory.annotation.Autowired;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.List;
@EqualsAndHashCode(callSuper = true)
@Data
@@ -23,9 +22,8 @@ public class URLModel extends QRCodeModel {
URLEntity details;
@Autowired
public URLModel(QRCodeEntity scannedQRCodeEntity, QRCodeTypeEntity qrCodeTypeEntity, URLVerificationService urlVerificationService) {
public URLModel(QRCodeEntity scannedQRCodeEntity, URLVerificationService urlVerificationService) {
this.scannedQRCode = scannedQRCodeEntity;
this.qrCode = qrCodeTypeEntity;
this.urlVerificationService = urlVerificationService;
this.details = null;
}

View File

@@ -20,9 +20,8 @@ public class WifiModel extends QRCodeModel {
WifiEntity details;
public WifiModel(QRCodeEntity scannedQRCodeEntity, QRCodeTypeEntity qrCodeTypeEntity, WifiVerificationService wifiVerificationService) {
public WifiModel(QRCodeEntity scannedQRCodeEntity, WifiVerificationService wifiVerificationService) {
this.scannedQRCode = scannedQRCodeEntity;
this.qrCode = qrCodeTypeEntity;
this.wifiVerificationService = wifiVerificationService;
this.details = null;
}

View File

@@ -18,7 +18,7 @@ public class EmailFactory implements QRCodeFactory<EmailModel> {
@Override
public EmailModel create(QRCodeEntity scannedQRCodeEntity, QRCodeTypeEntity qrCodeTypeEntity) {
return new EmailModel(scannedQRCodeEntity, qrCodeTypeEntity, emailVerificationService);
public EmailModel create(QRCodeEntity scannedQRCodeEntity) {
return new EmailModel(scannedQRCodeEntity, emailVerificationService);
}
}

View File

@@ -18,7 +18,7 @@ public class PhoneFactory implements QRCodeFactory<PhoneModel> {
@Override
public PhoneModel create(QRCodeEntity scannedQRCodeEntity, QRCodeTypeEntity qrCodeTypeEntity) {
return new PhoneModel(scannedQRCodeEntity, qrCodeTypeEntity, phoneVerificationService);
public PhoneModel create(QRCodeEntity scannedQRCodeEntity) {
return new PhoneModel(scannedQRCodeEntity, phoneVerificationService);
}
}

View File

@@ -6,5 +6,5 @@ import com.safeqr.app.qrcode.model.QRCodeModel;
@FunctionalInterface
public interface QRCodeFactory<T extends QRCodeModel> {
T create(QRCodeEntity scannedQRCodeEntity, QRCodeTypeEntity qrCodeTypeEntity);
T create(QRCodeEntity scannedQRCodeEntity);
}

View File

@@ -18,15 +18,15 @@ public class QRCodeFactoryProvider {
this.applicationContext = applicationContext;
}
public QRCodeModel createQRCodeInstance(QRCodeEntity scannedQRCodeEntity, QRCodeTypeEntity qrCodeTypeEntity) {
return switch (qrCodeTypeEntity.getType().toUpperCase()) {
case QR_CODE_TYPE_URL-> applicationContext.getBean(URLFactory.class).create(scannedQRCodeEntity, qrCodeTypeEntity);
case QR_CODE_TYPE_PHONE -> applicationContext.getBean(PhoneFactory.class).create(scannedQRCodeEntity, qrCodeTypeEntity);
case QR_CODE_TYPE_SMS -> applicationContext.getBean(SMSFactory.class).create(scannedQRCodeEntity, qrCodeTypeEntity);
case QR_CODE_TYPE_EMAIL -> applicationContext.getBean(EmailFactory.class).create(scannedQRCodeEntity, qrCodeTypeEntity);
case QR_CODE_TYPE_WIFI -> applicationContext.getBean(WifiFactory.class).create(scannedQRCodeEntity, qrCodeTypeEntity);
case DEFAULT_QR_CODE_TYPE -> applicationContext.getBean(TextFactory.class).create(scannedQRCodeEntity, qrCodeTypeEntity);
default -> throw new IllegalArgumentException("Unsupported QR code type: " + qrCodeTypeEntity.getType());
public QRCodeModel createQRCodeInstance(QRCodeEntity scannedQRCodeEntity) {
return switch (scannedQRCodeEntity.getInfo().getType().toUpperCase()) {
case QR_CODE_TYPE_URL-> applicationContext.getBean(URLFactory.class).create(scannedQRCodeEntity);
case QR_CODE_TYPE_PHONE -> applicationContext.getBean(PhoneFactory.class).create(scannedQRCodeEntity);
case QR_CODE_TYPE_SMS -> applicationContext.getBean(SMSFactory.class).create(scannedQRCodeEntity);
case QR_CODE_TYPE_EMAIL -> applicationContext.getBean(EmailFactory.class).create(scannedQRCodeEntity);
case QR_CODE_TYPE_WIFI -> applicationContext.getBean(WifiFactory.class).create(scannedQRCodeEntity);
case DEFAULT_QR_CODE_TYPE -> applicationContext.getBean(TextFactory.class).create(scannedQRCodeEntity);
default -> throw new IllegalArgumentException("Unsupported QR code type: " + scannedQRCodeEntity.getInfo().getType());
};
}
}

View File

@@ -18,7 +18,7 @@ public class SMSFactory implements QRCodeFactory<SMSModel> {
@Override
public SMSModel create(QRCodeEntity scannedQRCodeEntity, QRCodeTypeEntity qrCodeTypeEntity) {
return new SMSModel(scannedQRCodeEntity, qrCodeTypeEntity, smsVerificationService);
public SMSModel create(QRCodeEntity scannedQRCodeEntity) {
return new SMSModel(scannedQRCodeEntity, smsVerificationService);
}
}

View File

@@ -18,7 +18,7 @@ public class TextFactory implements QRCodeFactory<TextModel> {
@Override
public TextModel create(QRCodeEntity scannedQRCodeEntity, QRCodeTypeEntity qrCodeTypeEntity) {
return new TextModel(scannedQRCodeEntity, qrCodeTypeEntity, textVerificationService);
public TextModel create(QRCodeEntity scannedQRCodeEntity) {
return new TextModel(scannedQRCodeEntity, textVerificationService);
}
}

View File

@@ -17,7 +17,7 @@ public class URLFactory implements QRCodeFactory<URLModel> {
}
@Override
public URLModel create(QRCodeEntity scannedQRCodeEntity, QRCodeTypeEntity qrCodeTypeEntity) {
return new URLModel(scannedQRCodeEntity, qrCodeTypeEntity, urlVerificationService);
public URLModel create(QRCodeEntity scannedQRCodeEntity) {
return new URLModel(scannedQRCodeEntity, urlVerificationService);
}
}

View File

@@ -18,7 +18,7 @@ public class WifiFactory implements QRCodeFactory<WifiModel> {
@Override
public WifiModel create(QRCodeEntity scannedQRCodeEntity, QRCodeTypeEntity qrCodeTypeEntity) {
return new WifiModel(scannedQRCodeEntity, qrCodeTypeEntity, wifiVerificationService);
public WifiModel create(QRCodeEntity scannedQRCodeEntity) {
return new WifiModel(scannedQRCodeEntity, wifiVerificationService);
}
}

View File

@@ -23,6 +23,7 @@ import java.security.NoSuchAlgorithmException;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
@Service
@@ -51,7 +52,7 @@ public class QRCodeTypeService {
private List<QRCodeTypeEntity> configs;
private QRCodeTypeEntity defaultQRCodeTypeEntity;
private Map<Long, String> tableMap;
private Map<Long, QRCodeTypeEntity> qrCodeTypeMap;
@PostConstruct
public void loadQRCodeTypes() {
@@ -63,8 +64,8 @@ public class QRCodeTypeService {
.findFirst()
.orElse(null);
// Construct the tableMap with key = qrCodeTypeId, value = tableName
tableMap = configs.stream().collect(Collectors.toMap(QRCodeTypeEntity::getId, QRCodeTypeEntity::getTableName));
logger.info("Table map: {}", tableMap);
qrCodeTypeMap = configs.stream().collect(Collectors.toMap(QRCodeTypeEntity::getId, Function.identity()));
logger.info("QRCodeType map: {}", qrCodeTypeMap);
}
public List<QRCodeTypeEntity> getAllTypes() {
@@ -82,7 +83,7 @@ public class QRCodeTypeService {
QRCodeEntity scannedQR = qrCodeRepository.save(QRCodeEntity.builder()
.userId(userId)
.contents(data)
.qrCodeTypeId(qrType.getId())
.info(qrType)
.createdAt(LocalDateTime.now())
.build());
@@ -96,7 +97,7 @@ public class QRCodeTypeService {
.build());
}
// Create the QR Code Instance based on the QR Code Type & insert into the respective table
QRCodeModel qrCodeModel = qrCodeFactoryProvider.createQRCodeInstance(scannedQR, qrType);
QRCodeModel qrCodeModel = qrCodeFactoryProvider.createQRCodeInstance(scannedQR);
qrCodeModel.setDetails();
return BaseScanResponse.builder().qrcode(qrCodeModel).build();
@@ -109,8 +110,8 @@ public class QRCodeTypeService {
.orElse(defaultQRCodeTypeEntity);
}
// Returns name of table given type
public String getTableMap(Long qrTypeId) {
return tableMap.get(qrTypeId);
public QRCodeTypeEntity getQRCodeMap(Long qrTypeId) {
return qrCodeTypeMap.get(qrTypeId);
}
public Mono<String> detectType(QRCodePayload payload) {