| @@ -8,5 +8,5 @@ import org.springframework.stereotype.Repository | |||
| interface ItemsRepository : AbstractRepository<Items, Long> { | |||
| fun findAllByDeletedFalse(): List<Items>; | |||
| fun findByIdAndDeletedFalse(id: Long): Items; | |||
| fun findByCodeAndTypeAndDeletedFalse(code: String, type: ItemType): Items?; | |||
| fun findByCodeAndTypeAndDeletedFalse(code: String, type: String): Items?; | |||
| } | |||
| @@ -0,0 +1,36 @@ | |||
| package com.ffii.fpsms.modules.master.entity | |||
| import com.ffii.core.entity.BaseEntity | |||
| import jakarta.persistence.* | |||
| import jakarta.validation.constraints.NotNull | |||
| @Entity | |||
| @Table(name = "qc_check") | |||
| open class QcCheck: BaseEntity<Long>() { | |||
| @NotNull | |||
| @ManyToOne | |||
| @JoinColumn(name = "qcItemId") | |||
| open var qcItem: QcItem? = null | |||
| @NotNull | |||
| @Column(name = "isRange") | |||
| open var isRange: Boolean? = null | |||
| @Column(name = "description") | |||
| open var description: String? = null | |||
| @NotNull | |||
| @Column(name = "systemInput") | |||
| open var systemInput: Boolean? = null | |||
| @Column(name = "lowerLimit") | |||
| open var lowerLimit: Double? = null | |||
| @Column(name = "upperLimit") | |||
| open var upperLimit: Double? = null | |||
| @NotNull | |||
| @ManyToOne | |||
| @JoinColumn(name = "itemId") | |||
| open var item: Items? = null | |||
| } | |||
| @@ -0,0 +1,10 @@ | |||
| package com.ffii.fpsms.modules.master.entity | |||
| import com.ffii.core.support.AbstractRepository | |||
| import org.springframework.stereotype.Repository | |||
| @Repository | |||
| interface QcCheckRepository: AbstractRepository<QcCheck, Long> { | |||
| fun findAllByDeletedIsFalse(): List<QcCheck> | |||
| fun findAllByItemIdAndDeletedFalse(itemId: Long): List<QcCheck> | |||
| } | |||
| @@ -3,6 +3,7 @@ package com.ffii.fpsms.modules.master.service | |||
| import com.ffii.core.support.AbstractBaseEntityService | |||
| import com.ffii.core.support.JdbcDao | |||
| import com.ffii.fpsms.modules.master.entity.* | |||
| import com.ffii.fpsms.modules.master.web.models.ItemQc | |||
| import com.ffii.fpsms.modules.master.web.models.ItemWithQcResponse | |||
| import com.ffii.fpsms.modules.master.web.models.MessageResponse | |||
| import com.ffii.fpsms.modules.master.web.models.NewItemRequest | |||
| @@ -16,6 +17,7 @@ open class ItemsService( | |||
| private val jdbcDao: JdbcDao, | |||
| private val itemsRepository: ItemsRepository, | |||
| private val qcCheckRepository: QcCheckRepository, | |||
| private val qcItemsRepository: QcItemRepository, | |||
| ): AbstractBaseEntityService<Items, Long, ItemsRepository>(jdbcDao, itemsRepository) { | |||
| // do mapping with projection | |||
| open fun allItems(): List<Items> { | |||
| @@ -28,14 +30,27 @@ open class ItemsService( | |||
| open fun getItem(id: Long): ItemWithQcResponse { | |||
| val list = listOf(1,2) | |||
| val item = itemsRepository.findByIdAndDeletedFalse(id) | |||
| val qcChecks = qcCheckRepository.findAllByItemIdAndDeletedFalse(id).map { qcCheck -> | |||
| val qcItems = qcItemsRepository.findAllByDeletedIsFalse() | |||
| val qcCheckMap = qcCheckRepository.findAllByItemIdAndDeletedFalse(id).associateBy { it.id } | |||
| val qc = qcItems.map { | |||
| val check = qcCheckMap[it.id] // Find the corresponding qcCheck | |||
| println(check?.description) | |||
| println(check?.lowerLimit) | |||
| ItemQc( | |||
| id = it.id!!, | |||
| name = it.name!!, | |||
| code = it.code!!, | |||
| description = it.description!!, | |||
| instruction = check?.description, | |||
| lowerLimit = check?.lowerLimit, | |||
| upperLimit = check?.upperLimit, | |||
| isActive = check != null | |||
| ) // Create combined item | |||
| } | |||
| val response = ItemWithQcResponse( | |||
| item = item | |||
| item = item, | |||
| qcChecks = qc | |||
| ) | |||
| // val test = ItemWithQC(list) | |||
| // TODO: Return with QC items | |||
| return response | |||
| } | |||
| @@ -46,7 +61,7 @@ open class ItemsService( | |||
| @Throws(IOException::class) | |||
| @Transactional | |||
| open fun saveItem(request: NewItemRequest): MessageResponse { | |||
| val duplicatedItem = itemsRepository.findByCodeAndTypeAndDeletedFalse(request.code, request.type) | |||
| val duplicatedItem = itemsRepository.findByCodeAndTypeAndDeletedFalse(request.code, request.type.name) | |||
| if (duplicatedItem != null && duplicatedItem.id != request.id) { | |||
| return MessageResponse( | |||
| id = request.id, | |||
| @@ -67,7 +82,7 @@ open class ItemsService( | |||
| shelfLife = request.shelfLife | |||
| countryOfOrigin = request.countryOfOrigin | |||
| maxQty = request.maxQty | |||
| this.type = type | |||
| this.type = request.type.name | |||
| } | |||
| val savedItem = itemsRepository.saveAndFlush(item) | |||
| return MessageResponse( | |||
| @@ -0,0 +1,59 @@ | |||
| package com.ffii.fpsms.modules.master.service | |||
| import com.ffii.core.support.AbstractBaseEntityService | |||
| import com.ffii.core.support.JdbcDao | |||
| import com.ffii.fpsms.modules.master.entity.* | |||
| import com.ffii.fpsms.modules.master.web.models.MessageResponse | |||
| import com.ffii.fpsms.modules.master.web.models.NewItemRequest | |||
| import com.ffii.fpsms.modules.master.web.models.NewQcCheckRequest | |||
| import org.springframework.stereotype.Service | |||
| import org.springframework.transaction.annotation.Transactional | |||
| import java.io.IOException | |||
| @Service | |||
| open class QcCheckService( | |||
| private val jdbcDao: JdbcDao, | |||
| private val itemsRepository: ItemsRepository, | |||
| private val qcCheckRepository: QcCheckRepository, | |||
| private val qcItemRepository: QcItemRepository, | |||
| ): AbstractBaseEntityService<QcCheck, Long, QcCheckRepository>(jdbcDao, qcCheckRepository) { | |||
| @Throws(IOException::class) | |||
| @Transactional | |||
| open fun saveQcChecks(request: List<NewQcCheckRequest>): MessageResponse { | |||
| if (request.isEmpty()) { | |||
| return MessageResponse( | |||
| id = null, | |||
| name = null, | |||
| code = null, | |||
| type = null, | |||
| message = "empty list", | |||
| errorPosition = null, | |||
| ) | |||
| } | |||
| val qcChecks = qcCheckRepository.findAllByItemIdAndDeletedFalse(request[0].itemId) | |||
| val qcChecksList = request.map { req -> | |||
| val qc = qcChecks.find { it.qcItem!!.id == req.qcItemId } ?: QcCheck() | |||
| val _qcItem = qcItemRepository.findById(req.qcItemId).orElseThrow() | |||
| qc.apply { | |||
| this.qcItem = _qcItem | |||
| isRange = (req.lowerLimit !== null && req.upperLimit !== null && req.lowerLimit != req.upperLimit) | |||
| description = req.instruction | |||
| systemInput = false | |||
| lowerLimit = req.lowerLimit | |||
| upperLimit = req.upperLimit | |||
| this.item = itemsRepository.findById(req.itemId).orElseThrow() | |||
| } | |||
| } | |||
| val savedQcChecks = qcCheckRepository.saveAllAndFlush(qcChecksList) | |||
| return MessageResponse( | |||
| id = 1, | |||
| name = "total saved: ${savedQcChecks.size}", | |||
| code = null, | |||
| type = null, | |||
| message = "Qc Check Save Success", | |||
| errorPosition = null, | |||
| ) | |||
| } | |||
| } | |||
| @@ -3,6 +3,7 @@ package com.ffii.fpsms.modules.master.web | |||
| import com.ffii.core.exception.NotFoundException | |||
| import com.ffii.fpsms.modules.master.entity.Items | |||
| import com.ffii.fpsms.modules.master.service.ItemsService | |||
| import com.ffii.fpsms.modules.master.web.models.ItemWithQcResponse | |||
| import com.ffii.fpsms.modules.master.web.models.MessageResponse | |||
| import com.ffii.fpsms.modules.master.web.models.NewItemRequest | |||
| import jakarta.validation.Valid | |||
| @@ -18,11 +19,13 @@ class ItemsController( | |||
| return itemsService.allItems() | |||
| } | |||
| @GetMapping("/details/{id}") | |||
| fun getItems(@PathVariable id: Long): Items { | |||
| return itemsService.getItem(id) ?: throw NotFoundException() | |||
| fun getItems(@PathVariable id: Long): ItemWithQcResponse { | |||
| return itemsService.getItem(id) | |||
| } | |||
| @PostMapping("/new") | |||
| fun saveItem(@Valid @RequestBody newItem: NewItemRequest): MessageResponse { | |||
| println("`````TESTING`````") | |||
| return itemsService.saveItem(newItem) | |||
| } | |||
| } | |||
| @@ -0,0 +1,22 @@ | |||
| package com.ffii.fpsms.modules.master.web | |||
| import com.ffii.fpsms.modules.master.service.QcCheckService | |||
| import com.ffii.fpsms.modules.master.web.models.MessageResponse | |||
| import com.ffii.fpsms.modules.master.web.models.NewItemRequest | |||
| import com.ffii.fpsms.modules.master.web.models.NewQcCheckRequest | |||
| import jakarta.validation.Valid | |||
| 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 | |||
| @RestController | |||
| @RequestMapping("/qcCheck") | |||
| class QcCheckController( | |||
| private val qcCheckService: QcCheckService | |||
| ) { | |||
| @PostMapping("/new") | |||
| fun saveItem(@Valid @RequestBody newQcs: List<NewQcCheckRequest>): MessageResponse { | |||
| return qcCheckService.saveQcChecks(newQcs) | |||
| } | |||
| } | |||
| @@ -5,9 +5,13 @@ import com.ffii.fpsms.modules.master.entity.QcItem | |||
| data class ItemQc( | |||
| val id: Long, | |||
| val name: String, | |||
| val code: String, | |||
| val description: String?, | |||
| val instruction: String?, | |||
| val lowerLimit: Double?, | |||
| val upperLimit: Double?, | |||
| val isActive: Boolean?, | |||
| ) | |||
| data class ItemWithQcResponse( | |||
| val item: Items, | |||
| @@ -0,0 +1,9 @@ | |||
| package com.ffii.fpsms.modules.master.web.models | |||
| data class NewQcCheckRequest( | |||
| val qcItemId: Long, | |||
| val instruction: String?, | |||
| val lowerLimit: Double?, | |||
| val upperLimit: Double?, | |||
| val itemId: Long, | |||
| ) | |||