|
|
@@ -0,0 +1,76 @@ |
|
|
|
package com.ffii.fpsms.modules.master.web |
|
|
|
|
|
|
|
import com.ffii.fpsms.modules.master.entity.QcItem |
|
|
|
import com.ffii.fpsms.modules.master.entity.QcItemRepository |
|
|
|
import com.ffii.fpsms.modules.master.service.QcItemService |
|
|
|
import com.ffii.fpsms.modules.master.web.models.SaveQcItemRequest |
|
|
|
import com.ffii.fpsms.modules.master.web.models.SaveQcItemResponse |
|
|
|
import jakarta.validation.Valid |
|
|
|
import org.springframework.web.bind.annotation.GetMapping |
|
|
|
import org.springframework.web.bind.annotation.PostMapping |
|
|
|
import org.springframework.web.bind.annotation.RequestBody |
|
|
|
import org.springframework.web.bind.annotation.RequestMapping |
|
|
|
import org.springframework.web.bind.annotation.RestController |
|
|
|
import kotlin.reflect.KClass |
|
|
|
import kotlin.reflect.KProperty |
|
|
|
import kotlin.reflect.KParameter |
|
|
|
import kotlin.reflect.KTypeParameter |
|
|
|
import kotlin.reflect.typeOf |
|
|
|
|
|
|
|
@RestController |
|
|
|
@RequestMapping("/qcItems") |
|
|
|
class QcItemController( |
|
|
|
private val qcItemService: QcItemService, |
|
|
|
private val qcItemRepository: QcItemRepository, |
|
|
|
) { |
|
|
|
@GetMapping |
|
|
|
fun allQcItems(): List<QcItem> { |
|
|
|
return qcItemService.allQcItems() |
|
|
|
} |
|
|
|
|
|
|
|
@PostMapping("/save") |
|
|
|
fun saveQcItem(@Valid @RequestBody request: SaveQcItemRequest): SaveQcItemResponse { |
|
|
|
|
|
|
|
// val qcItemProperties = QcItem::class.members.filterIsInstance<KProperty<QcItem>>() |
|
|
|
val errors = mutableMapOf<String, String>() |
|
|
|
val id = request.id |
|
|
|
val qcItem = if (id != null) qcItemRepository.findById(id).orElseThrow() else QcItem() |
|
|
|
|
|
|
|
// check duplicated code |
|
|
|
val duplicateQcItem = qcItemService.findQcItemByCode(request.code) |
|
|
|
if (duplicateQcItem != null && duplicateQcItem.id != qcItem.id) { |
|
|
|
errors["code"] = "Code is duplicated" |
|
|
|
} |
|
|
|
|
|
|
|
if (errors.isNotEmpty()) { |
|
|
|
request.let { |
|
|
|
SaveQcItemResponse( |
|
|
|
id = it.id, |
|
|
|
code = it.code, |
|
|
|
name = it.name, |
|
|
|
description = it.description, |
|
|
|
errors = errors |
|
|
|
) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// Save Qc Item |
|
|
|
qcItem.apply { |
|
|
|
code = request.code |
|
|
|
name = request.name |
|
|
|
description = request.description |
|
|
|
} |
|
|
|
|
|
|
|
val savedQcItem = qcItemRepository.save(qcItem) |
|
|
|
|
|
|
|
return savedQcItem.let { |
|
|
|
SaveQcItemResponse( |
|
|
|
id = it.id, |
|
|
|
code = it.code, |
|
|
|
name = it.name, |
|
|
|
description = it.description, |
|
|
|
errors = null |
|
|
|
) |
|
|
|
} |
|
|
|
} |
|
|
|
} |