Skip to content

Commit

Permalink
Merge pull request #42 from appKom/41-create-get-user-receipts-endpoint
Browse files Browse the repository at this point in the history
(#41) Add get all receipts and fix get receipt for user
  • Loading branch information
akselsf authored Oct 20, 2024
2 parents 0cf981c + 6afeac4 commit d1d4da6
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 76 deletions.
1 change: 1 addition & 0 deletions schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ SELECT r.id AS receipt_id,
MAX(r.createdat) AS receipt_created_at,
MAX(c.name) AS committee_name,
MAX(ou.fullname) AS user_fullname,
MAX(ou.id) AS user_id,

CASE
WHEN MAX(p.id) IS NOT NULL THEN 'Payment'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ 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
import org.springframework.data.repository.query.Param


@RestController
Expand All @@ -35,7 +36,7 @@ class AdminReceiptController {


@GetMapping("/all")
fun getAllReceipts( @RequestParam from: Int, @RequestParam count: Int)
fun getAllReceipts(@Param("page") from: Int, @Param("count") count: Int)
: ResponseEntity<List<ReceiptInfo>> {
if (!authenticationService.checkBankomMembership()) {
return ResponseEntity.status(403).build()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
package com.example.autobank.controller

import com.example.autobank.data.receipt.Receipt
import com.example.autobank.data.receipt.ReceiptRequestBody
import com.example.autobank.data.receipt.ReceiptResponseBody
import com.example.autobank.data.receipt.*
import com.example.autobank.service.ImageService
import com.example.autobank.service.ReceiptService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.data.repository.query.Param
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.*


@RestController
@RequestMapping("/api/receipt")
class ReceiptController() {
class ReceiptController {

@Autowired
lateinit var imageService: ImageService;
lateinit var imageService: ImageService

@Autowired
lateinit var receiptService: ReceiptService
Expand All @@ -40,17 +39,18 @@ class ReceiptController() {
/* */

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

return try {
val res = receiptService.getAllReceiptsFromUser()
val res = receiptService.getAllReceiptsFromUser(from, count)
ResponseEntity.ok(res)
} catch (e: Exception) {
ResponseEntity.badRequest().build()
}
}

@GetMapping("/get/{id}")
fun getReceipt(@PathVariable id: Int): ResponseEntity<ReceiptResponseBody> {
fun getReceipt(@PathVariable id: Int): ResponseEntity<CompleteReceipt> {
return try {
val res = receiptService.getReceipt(id)
ResponseEntity.ok(res)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ class ReceiptInfo(
@Column(name = "user_fullname")
val userFullname: String,

@Column(name = "user_id")
val userId: Int,

@Column(name = "payment_or_card")
val paymentOrCard: String,

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

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.stereotype.Repository

Expand All @@ -9,4 +11,6 @@ interface ReceiptInfoViewRepository : JpaRepository<ReceiptInfo, Int> {

fun findByReceiptId(id: Int): ReceiptInfo

fun findByUserId(pageable: Pageable, id: Int): Page<ReceiptInfo>

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ 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 org.springframework.data.domain.PageRequest


@Service
Expand All @@ -19,6 +20,9 @@ class ReceiptAdminService {
@Autowired
lateinit var receiptInfoViewRepository: ReceiptInfoViewRepository

@Autowired
lateinit var receiptService: ReceiptService

@Autowired
lateinit var paymentCardService: PaymentCardService

Expand All @@ -29,11 +33,10 @@ class ReceiptAdminService {
lateinit var attachmentService: AttachmentService

fun getAll(from: Int, count: Int): List<ReceiptInfo>? {
try {
return receiptInfoViewRepository.findAll().subList(from, from + count)
} catch (e: Exception) {
return receiptInfoViewRepository.findAll().subList(0, 5)
}

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

}

fun getReceipt(id: Int): CompleteReceipt? {
Expand All @@ -42,37 +45,7 @@ class ReceiptAdminService {
return null
}

var card: Card = Card(0, 0, "")
var payment: Payment = Payment(0, 0, "")

if (receipt.paymentOrCard == "Card") {
card = paymentCardService.getCardByReceiptId(receipt.receiptId)
} else if (receipt.paymentOrCard == "Payment") {
payment = paymentCardService.getPaymentByReceiptId(receipt.receiptId)
}
println(receipt.paymentOrCard)

// Get images
val attachments = attachmentService.getAttachmentsByReceiptId(receipt.receiptId)
val images = attachments.map { attachment -> imageService.downloadImage(attachment.name) }

return CompleteReceipt(
receipt.receiptId,
receipt.amount,
receipt.receiptName,
receipt.receiptDescription,
receipt.receiptCreatedAt,
receipt.committeeName,
receipt.userFullname,
receipt.paymentOrCard,
receipt.attachmentCount,
receipt.latestReviewStatus,
receipt.latestReviewCreatedAt,
receipt.latestReviewComment,
payment.account_number,
card.card_number,
images
)
return receiptService.getCompleteReceipt(receipt)

}

Expand Down
82 changes: 51 additions & 31 deletions src/main/kotlin/com/example/autobank/service/ReceiptService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ package com.example.autobank.service
import com.example.autobank.data.receipt.*
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 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.Pageable;

@Service

Expand All @@ -29,6 +33,12 @@ class ReceiptService {
@Autowired
lateinit var paymentRepository: PaymentRepository

@Autowired
lateinit var receiptInfoViewRepository: ReceiptInfoViewRepository

@Autowired
lateinit var paymentCardService: PaymentCardService



fun createReceipt(receiptRequestBody: ReceiptRequestBody): ReceiptResponseBody {
Expand Down Expand Up @@ -77,52 +87,62 @@ class ReceiptService {

}

fun getAllReceiptsFromUser(): List<Receipt>? {
fun getAllReceiptsFromUser(from: Int, count: Int): List<ReceiptInfo>? {
val user = onlineUserService.getOnlineUser() ?: throw Exception("User not found")
val receipts = receiptRepository.findAllByOnlineUserId(user.id)

for (receipt in receipts) {
receipt.onlineUserId = null
}
val pageable = PageRequest.of(from, count)
val page: Page<ReceiptInfo> = receiptInfoViewRepository.findByUserId(pageable, user.id)

return receipts
return page.toList()
}

fun getReceipt(id: Int): ReceiptResponseBody? {
fun getReceipt(id: Int): CompleteReceipt? {
val user = onlineUserService.getOnlineUser() ?: throw Exception("User not found")
val receipt = receiptRepository.findById(id).orElseThrow { Exception("Receipt not found") }
if (receipt.onlineUserId != user.id) {
throw Exception("Receipt not found")
val receipt = receiptInfoViewRepository.findByReceiptId(id)
if (receipt == null || receipt.userId != user.id) {
return null
}
receipt.onlineUserId = null

val receiptResponseBody = ReceiptResponseBody()
receiptResponseBody.receipt = receipt
return getCompleteReceipt(receipt)

val attachments = attachmentService.getAttachmentsByReceiptId(receipt.id)
val images = mutableListOf<String>()
attachments.forEach { attachment ->
images.add(imageService.downloadImage(attachment.name))
}

receiptResponseBody.attachments = attachments.map { it.name }.toTypedArray()

val payment = paymentRepository.findFirstByReceiptId(receipt.id)
if (payment != null) {
receiptResponseBody.receiptPaymentInformation = ReceiptPaymentInformation("", payment.account_number, false)
} else {
val card = cardRepository.findFirstByReceiptId(receipt.id)
if (card != null) {
receiptResponseBody.receiptPaymentInformation = ReceiptPaymentInformation(card.card_number, "", true)
} else {
throw Exception("Payment information not found")
}
}

return receiptResponseBody

}

fun getCompleteReceipt(receipt: ReceiptInfo): CompleteReceipt {
var card = Card(0, 0, "")
var payment = Payment(0, 0, "")

if (receipt.paymentOrCard == "Card") {
card = paymentCardService.getCardByReceiptId(receipt.receiptId)
} else if (receipt.paymentOrCard == "Payment") {
payment = paymentCardService.getPaymentByReceiptId(receipt.receiptId)
}
println(receipt.paymentOrCard)

// Get images
val attachments = attachmentService.getAttachmentsByReceiptId(receipt.receiptId)
val images = attachments.map { attachment -> imageService.downloadImage(attachment.name) }

return CompleteReceipt(
receipt.receiptId,
receipt.amount,
receipt.receiptName,
receipt.receiptDescription,
receipt.receiptCreatedAt,
receipt.committeeName,
receipt.userFullname,
receipt.paymentOrCard,
receipt.attachmentCount,
receipt.latestReviewStatus,
receipt.latestReviewCreatedAt,
receipt.latestReviewComment,
payment.account_number,
card.card_number,
images
)


}
Expand Down

0 comments on commit d1d4da6

Please sign in to comment.