write sms type to db

This commit is contained in:
ltiongku
2024-08-14 19:44:18 +08:00
parent 334b3867ec
commit 9adb53e7ab
5 changed files with 37 additions and 2 deletions

View File

@@ -16,4 +16,8 @@ public class GlobalExceptionHandler {
public ResponseEntity<ErrorResponse> handleResourceAlreadyExistsException(ResourceAlreadyExists e) { public ResponseEntity<ErrorResponse> handleResourceAlreadyExistsException(ResourceAlreadyExists e) {
return new ResponseEntity<>(new ErrorResponse(e.getMessage(), HttpStatus.BAD_REQUEST.value()), HttpStatus.BAD_REQUEST); return new ResponseEntity<>(new ErrorResponse(e.getMessage(), HttpStatus.BAD_REQUEST.value()), HttpStatus.BAD_REQUEST);
} }
@ExceptionHandler(InvalidFormatExceptions.class)
public ResponseEntity<ErrorResponse> handleInvalidFormatException(InvalidFormatExceptions e) {
return new ResponseEntity<>(new ErrorResponse(e.getMessage(), HttpStatus.BAD_REQUEST.value()), HttpStatus.BAD_REQUEST);
}
} }

View File

@@ -0,0 +1,7 @@
package com.safeqr.app.exceptions;
public class InvalidFormatExceptions extends RuntimeException {
public InvalidFormatExceptions(String message){
super(message);
}
}

View File

@@ -27,6 +27,8 @@ public final class SMSModel extends QRCodeModel<SMSEntity> {
@Override @Override
public void setDetails() { public void setDetails() {
details = SMSEntity.builder().qrCodeId(data.getId()).build(); details = SMSEntity.builder().qrCodeId(data.getId()).build();
smsVerificationService.parseSMSString(details, data.getContents());
// Insert into sms table // Insert into sms table
smsVerificationService.insertDB(details); smsVerificationService.insertDB(details);
} }

View File

@@ -1,5 +1,6 @@
package com.safeqr.app.qrcode.service; package com.safeqr.app.qrcode.service;
import com.safeqr.app.exceptions.InvalidFormatExceptions;
import com.safeqr.app.exceptions.ResourceNotFoundExceptions; import com.safeqr.app.exceptions.ResourceNotFoundExceptions;
import com.safeqr.app.qrcode.entity.SMSEntity; import com.safeqr.app.qrcode.entity.SMSEntity;
import com.safeqr.app.qrcode.repository.SMSRepository; import com.safeqr.app.qrcode.repository.SMSRepository;
@@ -29,4 +30,25 @@ public class SMSVerificationService {
smsRepository.save(smsEntity); smsRepository.save(smsEntity);
} }
public void parseSMSString(SMSEntity smsEntity, String smsto) throws IllegalArgumentException{
// Remove the "SMSTO:" prefix
String data = smsto.substring(6);
// Split the data into phone number and message
String[] parts = data.split(":", 2);
// If both phone number and message are available
if (parts.length == 2) {
String phone = parts[0];
String message = parts[1];
// Populate the SMSEntity object
smsEntity.setPhone(phone);
smsEntity.setMessage(message);
} else {
// Handle the case where the format is invalid
throw new InvalidFormatExceptions("Invalid SMSTO format. Expected format: SMSTO:<phone>:<message>");
}
}
} }

View File

@@ -2,7 +2,7 @@ package com.safeqr.app.user.service;
import com.safeqr.app.exceptions.ResourceAlreadyExists; import com.safeqr.app.exceptions.ResourceAlreadyExists;
import com.safeqr.app.exceptions.ResourceNotFoundExceptions; import com.safeqr.app.exceptions.ResourceNotFoundExceptions;
import com.safeqr.app.qrcode.entity.ScanBookmarkEntity;
import com.safeqr.app.qrcode.entity.ScanHistoryEntity; import com.safeqr.app.qrcode.entity.ScanHistoryEntity;
import com.safeqr.app.qrcode.repository.ScanBookmarkRepository; import com.safeqr.app.qrcode.repository.ScanBookmarkRepository;
import com.safeqr.app.qrcode.repository.ScanHistoryRepository; import com.safeqr.app.qrcode.repository.ScanHistoryRepository;
@@ -14,7 +14,7 @@ import com.safeqr.app.user.repository.UserRepository;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;