@@ -17,7 +17,7 @@ public class RecordsRes<T> { | |||
this.records = records; | |||
} | |||
public RecordsRes(List<T> records, int total) { | |||
public RecordsRes(List<T> records, Integer total) { | |||
this.records = records; | |||
this.total = total; | |||
} | |||
@@ -0,0 +1,38 @@ | |||
package com.ffii.core.utils; | |||
import java.util.Collections; | |||
import java.util.List; | |||
/** | |||
* MapUtils | |||
* | |||
* @author Jason Lam | |||
*/ | |||
public class PagingUtils { | |||
/** | |||
* Create target sub list from a full list | |||
* | |||
* @param fullList | |||
* List to be split | |||
* @param pageSize | |||
* targeted record per page | |||
* @param pageNum | |||
* targeted page to be display | |||
* | |||
* @return List | |||
*/ | |||
public static <T> List<T> getPaginatedList(List<T> fullList, int pageSize, int pageNum) { | |||
// Calculate the start and end indices for subList | |||
int startIndex = (pageNum - 1) * pageSize; | |||
int endIndex = Math.min(startIndex + pageSize, fullList.size()); | |||
// Ensure we don't go out of bounds | |||
if (startIndex < fullList.size()) { | |||
return fullList.subList(startIndex, endIndex); | |||
} else { | |||
return Collections.emptyList(); // Return empty list if start index is out of bounds | |||
} | |||
} | |||
} |
@@ -2,6 +2,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.dto.ItemFilterRequestDTO | |||
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 | |||
@@ -10,7 +11,6 @@ import com.ffii.fpsms.modules.master.web.models.NewItemRequest | |||
import org.springframework.stereotype.Service | |||
import org.springframework.transaction.annotation.Transactional | |||
import java.io.IOException | |||
import kotlin.jvm.optionals.getOrNull | |||
@Service | |||
open class ItemsService( | |||
@@ -25,7 +25,30 @@ open class ItemsService( | |||
val items = itemsRepository.findAll() | |||
return items | |||
} | |||
open fun getItemsByPage(args: Map<String, Any>): List<Map<String, Any>> { | |||
// Extract parameters from the DTO | |||
val sql = StringBuilder( | |||
"SELECT " + | |||
"i.id, " + | |||
"i.code, " + | |||
"i.name, " + | |||
"i.description " + | |||
"FROM items i " + | |||
"WHERE i.deleted = FALSE" | |||
); | |||
if (args.containsKey("name")){ | |||
sql.append(" AND i.name like :name "); | |||
} | |||
if (args.containsKey("code")){ | |||
sql.append(" AND i.code like :code "); | |||
} | |||
return jdbcDao.queryForList(sql.toString(), args); | |||
} | |||
// QcCheck included item | |||
open fun getItem(id: Long): ItemWithQcResponse { | |||
val list = listOf(1,2) | |||
@@ -1,13 +1,19 @@ | |||
package com.ffii.fpsms.modules.master.web | |||
import com.ffii.core.exception.NotFoundException | |||
import com.ffii.core.response.RecordsRes | |||
import com.ffii.core.utils.CriteriaArgsBuilder | |||
import com.ffii.core.utils.PagingUtils | |||
import com.ffii.fpsms.modules.master.dto.ItemFilterRequestDTO | |||
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.servlet.http.HttpServletRequest | |||
import jakarta.validation.Valid | |||
import org.springframework.web.bind.annotation.* | |||
import java.util.Collections.emptyList | |||
@RestController | |||
@RequestMapping("/items") | |||
@@ -18,6 +24,40 @@ class ItemsController( | |||
fun allItems(): List<Items> { | |||
return itemsService.allItems() | |||
} | |||
// @GetMapping("/getRecordByPage") | |||
// fun getAllItemsByPage(@RequestBody filterRequest: HttpServletRequest): RecordsRes<Map<String, Any>> { | |||
// val pageSize = filterRequest.getParameter("pageSize").toString().toInt(); // Default to 10 if not provided | |||
// val pageNumber = filterRequest.getParameter("pageNum").toString().toInt(); // Default to 0 if not provided | |||
// val criteriaArgs = CriteriaArgsBuilder.withRequest(filterRequest) | |||
// .addStringLike("name") | |||
// .build(); | |||
// | |||
// val fullList = itemsService.getItemsByPage(criteriaArgs)?.subList(pageSize*pageNumber+1, pageSize*pageNumber+pageSize) | |||
// val outputList = fullList?.subList(pageSize*pageNumber+1, pageSize*pageNumber+pageSize) | |||
// | |||
// return RecordsRes(outputList as List<Map<String, Any>>?, fullList?.size) | |||
// } | |||
@GetMapping("/getRecordByPage") | |||
fun getAllItemsByPage( | |||
request: HttpServletRequest | |||
): RecordsRes<Map<String, Any>> { | |||
val criteriaArgs = CriteriaArgsBuilder.withRequest(request) | |||
.addStringLike("name") | |||
.addStringLike("code") | |||
.build() | |||
val pageSize = request.getParameter("pageSize")?.toIntOrNull() ?: 10 // Default to 10 if not provided | |||
val pageNum = request.getParameter("pageNum")?.toIntOrNull() ?: 1 // Default to 1 if not provided | |||
val fullList = itemsService.getItemsByPage(criteriaArgs) ?: emptyList() | |||
val paginatedList = PagingUtils.getPaginatedList(fullList,pageSize, pageNum) | |||
return RecordsRes(paginatedList as List<Map<String, Any>>, fullList.size) | |||
} | |||
@GetMapping("/details/{id}") | |||
fun getItems(@PathVariable id: Long): ItemWithQcResponse { | |||
return itemsService.getItem(id) | |||