Skip to content

Commit

Permalink
Add filter parameters to /getall user-receipts (#53) (#54)
Browse files Browse the repository at this point in the history
Co-authored-by: akselsf <akselsf>
  • Loading branch information
akselsf authored Feb 16, 2025
1 parent 6be4abf commit 26e00a7
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,34 +33,36 @@ class AdminReceiptController {


@GetMapping("/all")
fun getAllReceipts(@Param("page") from: Int, @Param("count") count: Int, @Param("status") status: String?, @Param("committee") committee: String?, @Param("search") search: String?, @Param("sortOrder") sortOrder: String?, @Param("sortField") sortField: String?): ResponseEntity<ReceiptListResponseBody> {
if (!authenticationService.checkBankomMembership()) {
return ResponseEntity.status(403).build()
fun getAllReceipts(@Param("page") from: Int = 0, @Param("count") count: Int = 10, @Param("status") status: String?, @Param("committee") committee: String?, @Param("search") search: String?, @Param("sortOrder") sortOrder: String?, @Param("sortField") sortField: String?): ResponseEntity<ReceiptListResponseBody> {
if (authenticationService.checkBankomMembership()) {
return ResponseEntity.ok(receiptAdminService.getAll(from, count, status, committee, search, sortField, sortOrder))
}
return ResponseEntity.status(403).build()

return ResponseEntity.ok(receiptAdminService.getAll(from, count, status, committee, search, sortField, sortOrder))
}

@GetMapping("/get/{id}")
fun getReceipt(@PathVariable id: Int): ResponseEntity<CompleteReceipt> {
if (!authenticationService.checkBankomMembership()) {
return ResponseEntity.status(403).build()
if (authenticationService.checkBankomMembership()) {
return ResponseEntity.ok(receiptAdminService.getReceipt(id))

}
return ResponseEntity.status(403).build()


return ResponseEntity.ok(receiptAdminService.getReceipt(id))
}

@PostMapping("/review")
fun reviewReceipt(@RequestBody reviewBody: ReceiptReviewRequestBody): ResponseEntity<ReceiptReview> {
if (!authenticationService.checkBankomMembership()) {
return ResponseEntity.status(403).build()
}
try {
return ResponseEntity.ok(receiptReviewService.createReceiptReview(reviewBody));
} catch (e: Exception) {
println(e)
return ResponseEntity.badRequest().build()
if (authenticationService.checkBankomMembership()) {
try {
return ResponseEntity.ok(receiptReviewService.createReceiptReview(reviewBody));
} catch (e: Exception) {
println(e)
return ResponseEntity.badRequest().build()
}
}
return ResponseEntity.status(403).build()

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,9 @@ class ReceiptController {
/* */

@GetMapping("/getall")
fun getAllReceipts(@Param("page") from: Int, @Param("count") count: Int): ResponseEntity<List<ReceiptInfo>> {

fun getAllReceipts(@Param("page") from: Int = 0, @Param("count") count: Int = 10, @Param("status") status: String?, @Param("committee") committee: String?, @Param("search") search: String?, @Param("sortOrder") sortOrder: String?, @Param("sortField") sortField: String?): ResponseEntity<ReceiptListResponseBody> {
return try {
val res = receiptService.getAllReceiptsFromUser(from, count)
val res = receiptService.getAllReceiptsFromUser(from, count, status, committee, search, sortField, sortOrder)
ResponseEntity.ok(res)
} catch (e: Exception) {
ResponseEntity.badRequest().build()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import org.springframework.data.jpa.domain.Specification
import jakarta.persistence.criteria.Predicate

class ReceiptInfoSpecification(
private val userId: Int?,
private val status: String?,
private val committeeName: String?,
private val search: String?
Expand All @@ -16,6 +17,11 @@ class ReceiptInfoSpecification(
): Predicate? {
val predicates = mutableListOf<Predicate>()

if (userId != null) {
predicates.add(criteriaBuilder.equal(root.get<Int>("userId"), userId))
}



if (status == "NONE") {
predicates.add(criteriaBuilder.isNull(root.get<String>("latestReviewStatus")))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ class AuthenticationService {
}

fun checkBankomMembership(): Boolean {
if (environment != "prod") {
if (environment == "dev") {
return true
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class ReceiptAdminService {

val pageable = PageRequest.of(from, count, sort)

val specification = ReceiptInfoSpecification(status, committeeName, search)
val specification = ReceiptInfoSpecification(null, status, committeeName, search)

val receipts: List<ReceiptInfo> = receiptInfoViewRepository.findAll(specification, pageable).toList()

Expand Down
22 changes: 18 additions & 4 deletions src/main/kotlin/com/example/autobank/service/ReceiptService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ import com.example.autobank.repository.receipt.CardRepository
import com.example.autobank.repository.receipt.PaymentRepository
import com.example.autobank.repository.receipt.ReceiptInfoViewRepository
import com.example.autobank.repository.receipt.ReceiptRepository
import com.example.autobank.repository.receipt.specification.ReceiptInfoSpecification
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort

@Service

Expand Down Expand Up @@ -90,13 +92,25 @@ class ReceiptService {

}

fun getAllReceiptsFromUser(from: Int, count: Int): List<ReceiptInfo>? {
fun getAllReceiptsFromUser(from: Int, count: Int, status: String?, committeeName: String?, search: String?, sortField: String?, sortOrder: String?): ReceiptListResponseBody? {


val user = onlineUserService.getOnlineUser() ?: throw Exception("User not found")

val pageable = PageRequest.of(from, count)
val page: Page<ReceiptInfo> = receiptInfoViewRepository.findByUserId(pageable, user.id)

return page.toList()
val sort = if (!sortField.isNullOrEmpty()) {
Sort.by(Sort.Direction.fromString(sortOrder ?: "ASC"), sortField)
} else {
Sort.by(Sort.Direction.DESC, "receiptCreatedAt")
}
val pageable = PageRequest.of(from, count, sort)

val specification = ReceiptInfoSpecification(user.id, status, committeeName, search)

val page: List<ReceiptInfo> = receiptInfoViewRepository.findAll(specification, pageable).toList()
val total: Long = receiptInfoViewRepository.count(specification)

return ReceiptListResponseBody(page.toTypedArray(), total)
}

fun getReceipt(id: Int): CompleteReceipt? {
Expand Down

0 comments on commit 26e00a7

Please sign in to comment.