edit scanned response
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
package com.safeqr.app.qrcode.dto.response;
|
package com.safeqr.app.qrcode.dto.response;
|
||||||
|
|
||||||
|
import com.safeqr.app.qrcode.entity.QRCode;
|
||||||
import com.safeqr.app.qrcode.entity.QRCodeType;
|
import com.safeqr.app.qrcode.entity.QRCodeType;
|
||||||
import lombok.Builder;
|
import lombok.Builder;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
@@ -7,6 +8,6 @@ import lombok.Data;
|
|||||||
@Data
|
@Data
|
||||||
@Builder
|
@Builder
|
||||||
public class ScanResponse {
|
public class ScanResponse {
|
||||||
private String contents;
|
private QRCode scannedQRCode;
|
||||||
private String qrType;
|
private QRCodeType qrCodeType;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,30 +1,38 @@
|
|||||||
|
|
||||||
package com.safeqr.app.qrcode.entity;
|
package com.safeqr.app.qrcode.entity;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||||
import jakarta.persistence.*;
|
import jakarta.persistence.*;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Builder;
|
import lombok.Builder;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
import org.hibernate.annotations.GenericGenerator;
|
import org.hibernate.annotations.GenericGenerator;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
@Table(name = "qr_code", schema = "safeqr")
|
@Table(name = "qr_code", schema = "safeqr")
|
||||||
@Data
|
@Data
|
||||||
@Builder
|
@Builder
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
public class QRCode {
|
public class QRCode {
|
||||||
|
|
||||||
@Id
|
@Id
|
||||||
|
@JsonIgnore
|
||||||
@GeneratedValue(generator = "UUID")
|
@GeneratedValue(generator = "UUID")
|
||||||
@GenericGenerator(name = "UUID", strategy = "org.hibernate.id.UUIDGenerator")
|
@GenericGenerator(name = "UUID", strategy = "org.hibernate.id.UUIDGenerator")
|
||||||
@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)
|
@Column(name = "qr_code_type_id", nullable = false)
|
||||||
private Long qrCodeTypeId;
|
private Long qrCodeTypeId;
|
||||||
private String userId;
|
private String userId;
|
||||||
private String contents;
|
private String contents;
|
||||||
|
|
||||||
@Column(name = "created_at", insertable = false, updatable = false)
|
@Column(name = "created_at", insertable = false, updatable = false)
|
||||||
private String createdAt;
|
private LocalDateTime createdAt;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
|
|
||||||
package com.safeqr.app.qrcode.entity;
|
package com.safeqr.app.qrcode.entity;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||||
import jakarta.persistence.*;
|
import jakarta.persistence.*;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
@@ -10,10 +11,15 @@ import lombok.Data;
|
|||||||
public class QRCodeType {
|
public class QRCodeType {
|
||||||
|
|
||||||
@Id
|
@Id
|
||||||
|
@JsonIgnore
|
||||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
private Long id;
|
private Long id;
|
||||||
private String type;
|
private String type;
|
||||||
private String description;
|
private String description;
|
||||||
|
|
||||||
|
@JsonIgnore
|
||||||
private String prefix;
|
private String prefix;
|
||||||
|
|
||||||
|
@JsonIgnore
|
||||||
private String tableName;
|
private String tableName;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,8 +17,13 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import reactor.core.publisher.Mono;
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
|
import java.net.MalformedURLException;
|
||||||
import java.security.NoSuchAlgorithmException;
|
import java.security.NoSuchAlgorithmException;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.net.URL;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class QRCodeTypeService {
|
public class QRCodeTypeService {
|
||||||
@@ -64,8 +69,11 @@ public class QRCodeTypeService {
|
|||||||
.userId(userId)
|
.userId(userId)
|
||||||
.contents(data)
|
.contents(data)
|
||||||
.qrCodeTypeId(qrType.getId())
|
.qrCodeTypeId(qrType.getId())
|
||||||
|
.createdAt(LocalDateTime.now())
|
||||||
.build());
|
.build());
|
||||||
|
|
||||||
// Insert qrcode into respective table based on type
|
// Insert qrcode into respective table based on type
|
||||||
|
insertIntoRespectiveTable(scannedQR);
|
||||||
|
|
||||||
// Insert into Scan History table if userId is not null
|
// Insert into Scan History table if userId is not null
|
||||||
logger.info("scanQRCode: scannedQR new ID={}", scannedQR.getId());
|
logger.info("scanQRCode: scannedQR new ID={}", scannedQR.getId());
|
||||||
@@ -78,8 +86,8 @@ public class QRCodeTypeService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return ScanResponse.builder()
|
return ScanResponse.builder()
|
||||||
.contents(data)
|
.scannedQRCode(scannedQR)
|
||||||
.qrType(qrType.getType())
|
.qrCodeType(qrType)
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
private QRCodeType getQRCodeType(String data) {
|
private QRCodeType getQRCodeType(String data) {
|
||||||
@@ -88,6 +96,57 @@ public class QRCodeTypeService {
|
|||||||
.findFirst()
|
.findFirst()
|
||||||
.orElse(defaultQRCodeType);
|
.orElse(defaultQRCodeType);
|
||||||
}
|
}
|
||||||
|
private void insertIntoRespectiveTable(QRCode qrCode) {
|
||||||
|
try {
|
||||||
|
String url = qrCode.getContents();
|
||||||
|
Map<String, Object> breakdown = breakdownURL(url);
|
||||||
|
breakdown.forEach((key, value) -> logger.info("{}: {}", key, value));
|
||||||
|
} catch (MalformedURLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
// Function to breakdown URL into subdomain, domain, topLevelDomain, query params, fragment
|
||||||
|
public Map<String, Object> breakdownURL(String urlString) throws MalformedURLException {
|
||||||
|
URL url = new URL(urlString);
|
||||||
|
Map<String, Object> breakdown = new HashMap<>();
|
||||||
|
|
||||||
|
String host = url.getHost();
|
||||||
|
String[] hostParts = host.split("\\.");
|
||||||
|
|
||||||
|
String subdomain = "";
|
||||||
|
String domain = "";
|
||||||
|
String topLevelDomain = "";
|
||||||
|
|
||||||
|
if (hostParts.length >= 2) {
|
||||||
|
topLevelDomain = hostParts[hostParts.length - 1];
|
||||||
|
domain = hostParts[hostParts.length - 2];
|
||||||
|
if (hostParts.length > 2) {
|
||||||
|
subdomain = String.join(".", java.util.Arrays.copyOfRange(hostParts, 0, hostParts.length - 2));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
breakdown.put("Subdomain", subdomain.isEmpty() ? "None" : subdomain);
|
||||||
|
breakdown.put("Domain", domain);
|
||||||
|
breakdown.put("Top Level Domain", topLevelDomain);
|
||||||
|
|
||||||
|
String query = url.getQuery();
|
||||||
|
if (query != null) {
|
||||||
|
Map<String, String> queryParams = new HashMap<>();
|
||||||
|
for (String param : query.split("&")) {
|
||||||
|
String[] pair = param.split("=");
|
||||||
|
queryParams.put(pair[0], pair.length > 1 ? pair[1] : "");
|
||||||
|
}
|
||||||
|
breakdown.put("Query Parameters", queryParams);
|
||||||
|
} else {
|
||||||
|
breakdown.put("Query Parameters", "None");
|
||||||
|
}
|
||||||
|
|
||||||
|
String fragment = url.getRef();
|
||||||
|
breakdown.put("Fragment", fragment != null ? fragment : "None");
|
||||||
|
|
||||||
|
return breakdown;
|
||||||
|
}
|
||||||
|
|
||||||
public Mono<String> detectType(QRCodePayload payload) {
|
public Mono<String> detectType(QRCodePayload payload) {
|
||||||
String data = payload.getData();
|
String data = payload.getData();
|
||||||
|
|||||||
Reference in New Issue
Block a user