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) @Column(updatable = false, nullable = false)
private UUID id; private UUID id;
@JsonIgnore
@Column(name = "qr_code_type_id", nullable = false)
private Long qrCodeTypeId;
@JsonIgnore @JsonIgnore
private String userId; private String userId;
private String contents; 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) @Column(name = "created_at", insertable = false, updatable = false)
private LocalDateTime createdAt; private LocalDateTime createdAt;
} }

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -6,5 +6,5 @@ import com.safeqr.app.qrcode.model.QRCodeModel;
@FunctionalInterface @FunctionalInterface
public interface QRCodeFactory<T extends QRCodeModel> { 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; this.applicationContext = applicationContext;
} }
public QRCodeModel createQRCodeInstance(QRCodeEntity scannedQRCodeEntity, QRCodeTypeEntity qrCodeTypeEntity) { public QRCodeModel createQRCodeInstance(QRCodeEntity scannedQRCodeEntity) {
return switch (qrCodeTypeEntity.getType().toUpperCase()) { return switch (scannedQRCodeEntity.getInfo().getType().toUpperCase()) {
case QR_CODE_TYPE_URL-> applicationContext.getBean(URLFactory.class).create(scannedQRCodeEntity, qrCodeTypeEntity); case QR_CODE_TYPE_URL-> applicationContext.getBean(URLFactory.class).create(scannedQRCodeEntity);
case QR_CODE_TYPE_PHONE -> applicationContext.getBean(PhoneFactory.class).create(scannedQRCodeEntity, qrCodeTypeEntity); case QR_CODE_TYPE_PHONE -> applicationContext.getBean(PhoneFactory.class).create(scannedQRCodeEntity);
case QR_CODE_TYPE_SMS -> applicationContext.getBean(SMSFactory.class).create(scannedQRCodeEntity, qrCodeTypeEntity); case QR_CODE_TYPE_SMS -> applicationContext.getBean(SMSFactory.class).create(scannedQRCodeEntity);
case QR_CODE_TYPE_EMAIL -> applicationContext.getBean(EmailFactory.class).create(scannedQRCodeEntity, qrCodeTypeEntity); case QR_CODE_TYPE_EMAIL -> applicationContext.getBean(EmailFactory.class).create(scannedQRCodeEntity);
case QR_CODE_TYPE_WIFI -> applicationContext.getBean(WifiFactory.class).create(scannedQRCodeEntity, qrCodeTypeEntity); case QR_CODE_TYPE_WIFI -> applicationContext.getBean(WifiFactory.class).create(scannedQRCodeEntity);
case DEFAULT_QR_CODE_TYPE -> applicationContext.getBean(TextFactory.class).create(scannedQRCodeEntity, qrCodeTypeEntity); case DEFAULT_QR_CODE_TYPE -> applicationContext.getBean(TextFactory.class).create(scannedQRCodeEntity);
default -> throw new IllegalArgumentException("Unsupported QR code type: " + qrCodeTypeEntity.getType()); default -> throw new IllegalArgumentException("Unsupported QR code type: " + scannedQRCodeEntity.getInfo().getType());
}; };
} }
} }

View File

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

View File

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

View File

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

View File

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

View File

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