Ver código fonte

Supporting function: Printer Handle

master
B.E.N.S.O.N 2 semanas atrás
pai
commit
3527bc0560
4 arquivos alterados com 164 adições e 2 exclusões
  1. +0
    -1
      src/main/java/com/ffii/fpsms/modules/master/entity/Printer.kt
  2. +4
    -0
      src/main/java/com/ffii/fpsms/modules/master/entity/PrinterRepository.kt
  3. +104
    -0
      src/main/java/com/ffii/fpsms/modules/master/service/PrinterService.kt
  4. +56
    -1
      src/main/java/com/ffii/fpsms/modules/master/web/PrinterController.kt

+ 0
- 1
src/main/java/com/ffii/fpsms/modules/master/entity/Printer.kt Ver arquivo

@@ -30,7 +30,6 @@ open class Printer : BaseEntity<Long>() {
@Column(name = "ip", length = 30)
open var ip: String? = null

@Size(max = 10)
@Column(name = "port", length = 10)
open var port: Int? = null



+ 4
- 0
src/main/java/com/ffii/fpsms/modules/master/entity/PrinterRepository.kt Ver arquivo

@@ -2,6 +2,7 @@ package com.ffii.fpsms.modules.master.entity

import com.ffii.core.support.AbstractRepository
import com.ffii.fpsms.modules.master.entity.projections.PrinterCombo
import org.springframework.data.jpa.repository.Query
import org.springframework.stereotype.Repository
import java.io.Serializable

@@ -12,4 +13,7 @@ interface PrinterRepository : AbstractRepository<Printer, Long> {
fun findByIdAndDeletedFalse(id: Serializable): Printer?;
fun findAllByDeletedIsFalse(): List<Printer>;
@Query("SELECT DISTINCT p.description FROM Printer p WHERE p.deleted = false AND p.description IS NOT NULL AND p.description != '' ORDER BY p.description")
fun findDistinctDescriptionsByDeletedIsFalse(): List<String>;
}

+ 104
- 0
src/main/java/com/ffii/fpsms/modules/master/service/PrinterService.kt Ver arquivo

@@ -3,12 +3,20 @@ package com.ffii.fpsms.modules.master.service
import com.ffii.fpsms.modules.master.entity.Printer
import com.ffii.fpsms.modules.master.entity.PrinterRepository
import com.ffii.fpsms.modules.master.entity.projections.PrinterCombo
import jakarta.persistence.OptimisticLockException
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.http.HttpStatus
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import org.springframework.web.server.ResponseStatusException

@Service
open class PrinterService(
val printerRepository: PrinterRepository
) {
private val logger: Logger = LoggerFactory.getLogger(PrinterService::class.java)
open fun getPrinters(): List<Printer> {
return printerRepository.findAllByDeletedIsFalse();
}
@@ -20,4 +28,100 @@ open class PrinterService(
open fun findById(id: Long): Printer? {
return printerRepository.findByIdAndDeletedFalse(id)
}
open fun getDistinctDescriptions(): List<String> {
return printerRepository.findDistinctDescriptionsByDeletedIsFalse()
}
@Transactional
open fun createPrinter(request: com.ffii.fpsms.modules.master.web.PrinterCreateRequest): Printer {
try {
val printer = Printer().apply {
name = request.name
code = request.code
type = request.type
description = request.description
ip = request.ip
port = request.port
dpi = request.dpi
}

return printerRepository.save(printer)
} catch (e: OptimisticLockException) {
logger.error("Optimistic lock exception when creating printer", e)
throw ResponseStatusException(
HttpStatus.CONFLICT,
"Printer was modified by another user. Please refresh and try again."
)
} catch (e: ResponseStatusException) {
throw e
} catch (e: Exception) {
logger.error("Error creating printer", e)
throw ResponseStatusException(
HttpStatus.INTERNAL_SERVER_ERROR,
"Failed to create printer: ${e.message ?: "Unknown error"}"
)
}
}

@Transactional
open fun updatePrinter(id: Long, request: com.ffii.fpsms.modules.master.web.PrinterUpdateRequest): Printer {
try {
val printer = printerRepository.findById(id).orElseThrow {
ResponseStatusException(HttpStatus.NOT_FOUND, "Printer with id $id not found")
}
request.name?.let { printer.name = it }
request.code?.let { printer.code = it }
request.type?.let { printer.type = it }
request.description?.let { printer.description = it }
request.ip?.let { printer.ip = it }
request.port?.let { printer.port = it }
request.dpi?.let { printer.dpi = it }
return printerRepository.save(printer)
} catch (e: OptimisticLockException) {
logger.error("Optimistic lock exception when updating printer with id $id", e)
throw ResponseStatusException(
HttpStatus.CONFLICT,
"Printer was modified by another user. Please refresh and try again."
)
} catch (e: ResponseStatusException) {
throw e
} catch (e: Exception) {
logger.error("Error updating printer with id $id", e)
throw ResponseStatusException(
HttpStatus.INTERNAL_SERVER_ERROR,
"Failed to update printer: ${e.message ?: "Unknown error"}"
)
}
}
@Transactional
open fun markDeleted(id: Long): List<Printer> {
try {
val printer = printerRepository.findById(id).orElseThrow {
ResponseStatusException(HttpStatus.NOT_FOUND, "Printer with id $id not found")
}
printer.deleted = true
printerRepository.save(printer)
return getPrinters()
} catch (e: OptimisticLockException) {
logger.error("Optimistic lock exception when deleting printer with id $id", e)
throw ResponseStatusException(
HttpStatus.CONFLICT,
"Printer was modified by another user. Please refresh and try again."
)
} catch (e: ResponseStatusException) {
throw e
} catch (e: Exception) {
logger.error("Error deleting printer with id $id", e)
throw ResponseStatusException(
HttpStatus.INTERNAL_SERVER_ERROR,
"Failed to delete printer: ${e.message ?: "Unknown error"}"
)
}
}
}

+ 56
- 1
src/main/java/com/ffii/fpsms/modules/master/web/PrinterController.kt Ver arquivo

@@ -3,9 +3,15 @@ package com.ffii.fpsms.modules.master.web
import com.ffii.fpsms.modules.master.entity.Printer
import com.ffii.fpsms.modules.master.entity.projections.PrinterCombo
import com.ffii.fpsms.modules.master.service.PrinterService
import org.springframework.web.bind.annotation.DeleteMapping
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.PutMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import jakarta.validation.Valid

@RequestMapping("printers")
@RestController
@@ -17,8 +23,57 @@ class PrinterController(
return printerService.getPrinters();
}
@GetMapping("/{id}")
fun getPrinter(@PathVariable id: Long): Printer {
return printerService.findById(id)
?: throw org.springframework.web.server.ResponseStatusException(
org.springframework.http.HttpStatus.NOT_FOUND,
"Printer with id $id not found"
)
}
@GetMapping("/combo")
fun findCombo(): List<PrinterCombo> {
return printerService.findCombo();
}
}
@GetMapping("/descriptions")
fun getDescriptions(): List<String> {
return printerService.getDistinctDescriptions();
}
@PostMapping
fun createPrinter(@Valid @RequestBody request: PrinterCreateRequest): Printer {
return printerService.createPrinter(request)
}

@PutMapping("/{id}")
fun updatePrinter(@PathVariable id: Long, @Valid @RequestBody request: PrinterUpdateRequest): Printer {
return printerService.updatePrinter(id, request);
}
@DeleteMapping("/{id}")
fun deletePrinter(@PathVariable id: Long): List<Printer> {
return printerService.markDeleted(id);
}
}

data class PrinterCreateRequest(
val name: String? = null,
val code: String? = null,
val type: String? = null,
val description: String? = null,
val ip: String? = null,
val port: Int? = null,
val dpi: Int? = null
)

data class PrinterUpdateRequest(
val name: String? = null,
val code: String? = null,
val type: String? = null,
val description: String? = null,
val ip: String? = null,
val port: Int? = null,
val dpi: Int? = null
)

Carregando…
Cancelar
Salvar