Extracted EpcQrCodeService

This commit is contained in:
dankito 2023-04-10 20:27:13 +02:00
parent 7d69fc9d2d
commit be296eaad9
2 changed files with 42 additions and 23 deletions

View File

@ -1,9 +1,8 @@
package net.codinux.banking.epcqrcode.rest
import net.codinux.banking.epcqrcode.EpcQrCodeConfig
import net.codinux.banking.epcqrcode.EpcQrCodeGenerator
import net.codinux.banking.epcqrcode.rest.dto.GenerateEpcQrCodeRequest
import net.codinux.banking.epcqrcode.rest.dto.GenerateEpcQrCodeResponse
import net.codinux.banking.epcqrcode.rest.service.EpcQrCodeService
import org.eclipse.microprofile.openapi.annotations.Operation
import org.eclipse.microprofile.openapi.annotations.media.Content
import org.eclipse.microprofile.openapi.annotations.media.Schema
@ -12,6 +11,7 @@ import org.eclipse.microprofile.openapi.annotations.responses.APIResponse
import org.eclipse.microprofile.openapi.annotations.tags.Tag
import org.slf4j.LoggerFactory
import java.util.*
import javax.inject.Inject
import javax.ws.rs.*
import javax.ws.rs.core.MediaType
import javax.ws.rs.core.Response
@ -20,11 +20,10 @@ import javax.ws.rs.core.Response
@Path("/epcqrcode/v1")
@Produces(MediaType.APPLICATION_JSON)
@Tag(name = "EPC QR Code")
class EpcQrCodeResource {
protected val epcQrCodeGenerator = EpcQrCodeGenerator()
private val base64Encoder = Base64.getEncoder()
class EpcQrCodeResource(
@Inject
private val service: EpcQrCodeService
) {
private val log = LoggerFactory.getLogger(EpcQrCodeResource::class.java)
@ -52,31 +51,19 @@ class EpcQrCodeResource {
}
private fun handleJsonRequest(request: GenerateEpcQrCodeRequest): Response {
try {
val epcQrCode = epcQrCodeGenerator.generateEpcQrCode(mapToEpcQrCodeConfig(request), request.imageHeightAndWidth)
val qrCodeBytes = base64Encoder.encode(epcQrCode.bytes)
return try {
val qrCodeBytes = service.generateEpcQrCodeBase64Encoded(request)
return Response.ok(GenerateEpcQrCodeResponse(qrCodeBytes))
Response.ok(GenerateEpcQrCodeResponse(qrCodeBytes))
.header("Access-Control-Allow-Origin", "*")
.build()
} catch (e: Exception) {
log.error("Could not create QR code for $request", e)
return Response.status(Response.Status.INTERNAL_SERVER_ERROR.statusCode, e.localizedMessage) // TODO: it's almost always a bad idea to pass exception message to the outside
Response.status(Response.Status.INTERNAL_SERVER_ERROR.statusCode, e.localizedMessage) // TODO: it's almost always a bad idea to pass exception message to the outside
.header("Access-Control-Allow-Origin", "*")
.build()
}
}
private fun mapToEpcQrCodeConfig(dto: GenerateEpcQrCodeRequest): EpcQrCodeConfig {
return EpcQrCodeConfig(
dto.receiverName,
dto.iban,
dto.bic,
dto.amount,
dto.reference,
dto.noteToUser
)
}
}

View File

@ -0,0 +1,32 @@
package net.codinux.banking.epcqrcode.rest.service
import net.codinux.banking.epcqrcode.EpcQrCodeConfig
import net.codinux.banking.epcqrcode.EpcQrCodeGenerator
import net.codinux.banking.epcqrcode.rest.dto.GenerateEpcQrCodeRequest
import java.util.*
import javax.enterprise.context.ApplicationScoped
@ApplicationScoped
class EpcQrCodeService {
private val epcQrCodeGenerator = EpcQrCodeGenerator()
private val base64Encoder = Base64.getEncoder()
fun generateEpcQrCodeBase64Encoded(request: GenerateEpcQrCodeRequest): ByteArray {
val epcQrCode = epcQrCodeGenerator.generateEpcQrCode(mapToEpcQrCodeConfig(request), request.imageHeightAndWidth)
return base64Encoder.encode(epcQrCode.bytes)
}
private fun mapToEpcQrCodeConfig(dto: GenerateEpcQrCodeRequest): EpcQrCodeConfig {
return EpcQrCodeConfig(
dto.receiverName,
dto.iban,
dto.bic,
dto.amount,
dto.reference,
dto.noteToUser
)
}
}