| @@ -9,6 +9,7 @@ import org.springframework.stereotype.Repository | |||||
| @Repository | @Repository | ||||
| interface QcCategoryRepository : AbstractRepository<QcCategory, Long> { | interface QcCategoryRepository : AbstractRepository<QcCategory, Long> { | ||||
| fun findAllByDeletedIsFalse(): List<QcCategory>; | fun findAllByDeletedIsFalse(): List<QcCategory>; | ||||
| fun findByIdAndDeletedIsFalse(id: Long): QcCategory?; | |||||
| fun findQcCategoryComboByDeletedIsFalse(): List<QcCategoryCombo>; | fun findQcCategoryComboByDeletedIsFalse(): List<QcCategoryCombo>; | ||||
| @@ -3,9 +3,14 @@ package com.ffii.fpsms.modules.master.service | |||||
| import com.ffii.core.support.AbstractBaseEntityService | import com.ffii.core.support.AbstractBaseEntityService | ||||
| import com.ffii.fpsms.modules.master.entity.QcCategory | import com.ffii.fpsms.modules.master.entity.QcCategory | ||||
| import com.ffii.fpsms.modules.master.entity.QcCategoryRepository | import com.ffii.fpsms.modules.master.entity.QcCategoryRepository | ||||
| import com.ffii.fpsms.modules.master.entity.QcItem | |||||
| import com.ffii.fpsms.modules.master.entity.projections.QcCategoryCombo | import com.ffii.fpsms.modules.master.entity.projections.QcCategoryCombo | ||||
| import com.ffii.fpsms.modules.master.web.models.SaveQcCategoryRequest | |||||
| import com.ffii.fpsms.modules.master.web.models.SaveQcCategoryResponse | |||||
| import com.ffii.fpsms.modules.qc.entity.projection.QcCategoryInfo | import com.ffii.fpsms.modules.qc.entity.projection.QcCategoryInfo | ||||
| import jakarta.validation.Valid | |||||
| import org.springframework.stereotype.Service | import org.springframework.stereotype.Service | ||||
| import org.springframework.web.bind.annotation.RequestBody | |||||
| @Service | @Service | ||||
| open class QcCategoryService( | open class QcCategoryService( | ||||
| @@ -16,6 +21,10 @@ open class QcCategoryService( | |||||
| return qcCategoryRepository.findAllByDeletedIsFalse() | return qcCategoryRepository.findAllByDeletedIsFalse() | ||||
| } | } | ||||
| open fun findQcCategoryById(id: Long): QcCategory? { | |||||
| return qcCategoryRepository.findByIdAndDeletedIsFalse(id) | |||||
| } | |||||
| open fun getQcCategoryCombo(): List<QcCategoryCombo> { | open fun getQcCategoryCombo(): List<QcCategoryCombo> { | ||||
| return qcCategoryRepository.findQcCategoryComboByDeletedIsFalse(); | return qcCategoryRepository.findQcCategoryComboByDeletedIsFalse(); | ||||
| } | } | ||||
| @@ -40,4 +49,57 @@ open class QcCategoryService( | |||||
| } | } | ||||
| return result; | return result; | ||||
| } | } | ||||
| open fun markDeleted(id: Long): List<QcCategory> { | |||||
| val qcItem = qcCategoryRepository.findById(id).orElseThrow().apply { | |||||
| deleted = true | |||||
| } | |||||
| qcCategoryRepository.save(qcItem) | |||||
| return allQcCategories() | |||||
| } | |||||
| open fun saveQcCategory(@Valid @RequestBody request: SaveQcCategoryRequest): SaveQcCategoryResponse { | |||||
| val errors = mutableMapOf<String, String>() | |||||
| val id = request.id | |||||
| val qcCategory = if (id != null) qcCategoryRepository.findById(id).orElseThrow() else QcCategory() | |||||
| // check duplicated code | |||||
| // val duplicateQcCategory = findQcCategoryByCode(request.code) | |||||
| // if (duplicateQcCategory != null && duplicateQcCategory.id != qcCategory.id) { | |||||
| // errors["code"] = "Code is duplicated" | |||||
| // } | |||||
| if (errors.isNotEmpty()) { | |||||
| request.let { | |||||
| SaveQcCategoryResponse( | |||||
| id = it.id, | |||||
| code = it.code, | |||||
| name = it.name, | |||||
| description = it.description, | |||||
| errors = errors | |||||
| ) | |||||
| } | |||||
| } | |||||
| // Save Qc Item | |||||
| qcCategory.apply { | |||||
| code = request.code | |||||
| name = request.name | |||||
| description = request.description | |||||
| } | |||||
| val savedQcCategory = qcCategoryRepository.save(qcCategory) | |||||
| return savedQcCategory.let { | |||||
| SaveQcCategoryResponse( | |||||
| id = it.id, | |||||
| code = it.code, | |||||
| name = it.name, | |||||
| description = it.description, | |||||
| errors = null | |||||
| ) | |||||
| } | |||||
| } | |||||
| } | } | ||||
| @@ -54,10 +54,10 @@ open class QcItemService( | |||||
| val qcItem = if (id != null) qcItemRepository.findById(id).orElseThrow() else QcItem() | val qcItem = if (id != null) qcItemRepository.findById(id).orElseThrow() else QcItem() | ||||
| // check duplicated code | // check duplicated code | ||||
| val duplicateQcItem = findQcItemByCode(request.code) | |||||
| if (duplicateQcItem != null && duplicateQcItem.id != qcItem.id) { | |||||
| errors["code"] = "Code is duplicated" | |||||
| } | |||||
| // val duplicateQcItem = findQcItemByCode(request.code) | |||||
| // if (duplicateQcItem != null && duplicateQcItem.id != qcItem.id) { | |||||
| // errors["code"] = "Code is duplicated" | |||||
| // } | |||||
| if (errors.isNotEmpty()) { | if (errors.isNotEmpty()) { | ||||
| request.let { | request.let { | ||||
| @@ -2,9 +2,13 @@ package com.ffii.fpsms.modules.master.web | |||||
| import com.ffii.core.exception.NotFoundException | import com.ffii.core.exception.NotFoundException | ||||
| import com.ffii.fpsms.modules.master.entity.QcCategory | import com.ffii.fpsms.modules.master.entity.QcCategory | ||||
| import com.ffii.fpsms.modules.master.entity.QcItem | |||||
| import com.ffii.fpsms.modules.master.entity.projections.QcCategoryCombo | import com.ffii.fpsms.modules.master.entity.projections.QcCategoryCombo | ||||
| import com.ffii.fpsms.modules.master.service.QcCategoryService | import com.ffii.fpsms.modules.master.service.QcCategoryService | ||||
| import com.ffii.fpsms.modules.master.web.models.SaveQcCategoryRequest | |||||
| import com.ffii.fpsms.modules.master.web.models.SaveQcCategoryResponse | |||||
| import com.ffii.fpsms.modules.qc.entity.projection.QcCategoryInfo | import com.ffii.fpsms.modules.qc.entity.projection.QcCategoryInfo | ||||
| import jakarta.validation.Valid | |||||
| import org.springframework.web.bind.annotation.* | import org.springframework.web.bind.annotation.* | ||||
| @RestController | @RestController | ||||
| @@ -17,6 +21,21 @@ class QcCategoryController( | |||||
| return qcCategoryService.allQcCategories() | return qcCategoryService.allQcCategories() | ||||
| } | } | ||||
| @DeleteMapping("/{id}") | |||||
| fun deleteQcItem(@PathVariable id: Long): List<QcCategory> { | |||||
| return qcCategoryService.markDeleted(id) | |||||
| } | |||||
| @GetMapping("/details/{id}") | |||||
| fun qcCategoryDetails(@PathVariable id: Long): QcCategory { | |||||
| return qcCategoryService.findQcCategoryById(id) ?: throw NotFoundException() | |||||
| } | |||||
| @PostMapping("/save") | |||||
| fun saveQcCategory(@Valid @RequestBody request: SaveQcCategoryRequest): SaveQcCategoryResponse { | |||||
| return qcCategoryService.saveQcCategory(request) | |||||
| } | |||||
| @GetMapping("/combo") | @GetMapping("/combo") | ||||
| fun getQcCategoryCombo(): List<QcCategoryCombo> { | fun getQcCategoryCombo(): List<QcCategoryCombo> { | ||||
| return qcCategoryService.getQcCategoryCombo(); | return qcCategoryService.getQcCategoryCombo(); | ||||
| @@ -0,0 +1,12 @@ | |||||
| package com.ffii.fpsms.modules.master.web.models | |||||
| import jakarta.validation.constraints.NotBlank | |||||
| data class SaveQcCategoryRequest( | |||||
| val id: Long?, | |||||
| @field:NotBlank(message = "Code cannot be empty") | |||||
| val code: String, | |||||
| @field:NotBlank(message = "Name cannot be empty") | |||||
| val name: String, | |||||
| val description: String?, | |||||
| ) | |||||
| @@ -0,0 +1,9 @@ | |||||
| package com.ffii.fpsms.modules.master.web.models | |||||
| data class SaveQcCategoryResponse( | |||||
| val id: Long?, | |||||
| val code: String?, | |||||
| val name: String?, | |||||
| val description: String?, | |||||
| val errors: MutableMap<String, String>? | |||||
| ) | |||||