Skip to content

Commit

Permalink
Add filtering to receipts
Browse files Browse the repository at this point in the history
  • Loading branch information
akselsf committed Dec 23, 2024
1 parent 4fc78fc commit 9c688b2
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
package com.example.autobank.controller;

import com.example.autobank.data.receipt.CompleteReceipt
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RestController;
import com.example.autobank.data.receipt.Receipt;
import com.example.autobank.service.ReceiptAdminService;
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.PathVariable
import com.example.autobank.data.receipt.ReceiptInfo
import com.example.autobank.service.AuthenticationService
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestParam
import com.example.autobank.data.ReceiptReviewRequestBody
import com.example.autobank.data.receipt.ReceiptReview
import com.example.autobank.service.ReceiptReviewService
Expand All @@ -36,13 +33,12 @@ class AdminReceiptController {


@GetMapping("/all")
fun getAllReceipts(@Param("page") from: Int, @Param("count") count: Int)
: ResponseEntity<List<ReceiptInfo>> {
fun getAllReceipts(@Param("page") from: Int, @Param("count") count: Int, @Param("status") status: String?, @Param("committee") committee: String?, @Param("sortOrder") sortOrder: String?, @Param("sortField") sortField: String?): ResponseEntity<List<ReceiptInfo>> {
if (!authenticationService.checkBankomMembership()) {
return ResponseEntity.status(403).build()
}

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

@GetMapping("/get/{id}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import com.example.autobank.data.receipt.ReceiptInfo
import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.jpa.repository.JpaSpecificationExecutor
import org.springframework.stereotype.Repository

@Repository
interface ReceiptInfoViewRepository : JpaRepository<ReceiptInfo, Int> {
interface ReceiptInfoViewRepository : JpaRepository<ReceiptInfo, Int>, JpaSpecificationExecutor<ReceiptInfo> {

fun findByReceiptId(id: Int): ReceiptInfo

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.example.autobank.repository.receipt.specification

import com.example.autobank.data.receipt.ReceiptInfo
import org.springframework.data.jpa.domain.Specification
import jakarta.persistence.criteria.Predicate

class ReceiptInfoSpecification(
private val status: String?,
private val committeeName: String?
) : Specification<ReceiptInfo> {
override fun toPredicate(
root: jakarta.persistence.criteria.Root<ReceiptInfo>,
query: jakarta.persistence.criteria.CriteriaQuery<*>,
criteriaBuilder: jakarta.persistence.criteria.CriteriaBuilder
): Predicate? {
val predicates = mutableListOf<Predicate>()


if (status == "NONE") {
predicates.add(criteriaBuilder.isNull(root.get<String>("latestReviewStatus")))
} else if (status != null) {
predicates.add(criteriaBuilder.equal(root.get<String>("latestReviewStatus"), status))
}
println(committeeName)
if (committeeName != null) {
predicates.add(criteriaBuilder.equal(root.get<String>("committeeName"), committeeName))
}


return criteriaBuilder.and(*predicates.toTypedArray())
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package com.example.autobank.service

import org.springframework.stereotype.Service
import com.example.autobank.data.receipt.Receipt
import com.example.autobank.repository.receipt.PaymentRepository
import com.example.autobank.repository.receipt.ReceiptRepository
import org.springframework.beans.factory.annotation.Autowired
import com.example.autobank.repository.receipt.ReceiptInfoViewRepository
import com.example.autobank.data.receipt.*
import com.example.autobank.data.ReceiptReviewRequestBody
import com.example.autobank.repository.receipt.specification.ReceiptInfoSpecification
import org.springframework.data.domain.PageRequest
import org.springframework.data.domain.Sort


@Service
Expand All @@ -32,10 +32,24 @@ class ReceiptAdminService {
@Autowired
lateinit var attachmentService: AttachmentService

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

val pageable = PageRequest.of(from, count)
return receiptInfoViewRepository.findAll(pageable).toList()

val sort = if (!sortField.isNullOrEmpty()) {
Sort.by(Sort.Direction.fromString(sortOrder ?: "ASC"), sortField)
} else {
Sort.unsorted()
}

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

return if (status != null || committeeName != null) {
receiptInfoViewRepository.findAll(
ReceiptInfoSpecification(status, committeeName), pageable
).toList()
} else {
receiptInfoViewRepository.findAll(pageable).toList()
}

}

Expand All @@ -49,7 +63,5 @@ class ReceiptAdminService {

}

fun createReceiptReview(reviewBody: ReceiptReviewRequestBody) {

}
}

0 comments on commit 9c688b2

Please sign in to comment.