Skip to content

Commit

Permalink
Get all and single receipt endpoint for users (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
akselsf committed Sep 17, 2024
1 parent a1db698 commit 8592d40
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,31 @@ class ReceiptController() {
}

/* For testing. temporary */
@GetMapping("/get/{id}")
@GetMapping("/getimage/{id}")
fun getReceipt(@PathVariable id: String): String {
return imageService.downloadImage(id)
}
/* */

@GetMapping("/getall")
fun getAllReceipts(): ResponseEntity<List<Receipt>> {
return try {
val res = receiptService.getAllReceiptsFromUser()
ResponseEntity.ok(res)
} catch (e: Exception) {
ResponseEntity.badRequest().build()
}
}

@GetMapping("/get/{id}")
fun getReceipt(@PathVariable id: Int): ResponseEntity<ReceiptResponseBody> {
return try {
val res = receiptService.getReceipt(id)
ResponseEntity.ok(res)
} catch (e: Exception) {
ResponseEntity.badRequest().build()
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Payment (
val id: Int,

@Column(name = "receipt_id")
val receipt_id: Int,
val receiptId: Int,

@Column(name = "account_number")
val account_number: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ import jakarta.persistence.Entity
class ReceiptResponseBody {
var receipt: Receipt? = null
var receiptPaymentInformation: ReceiptPaymentInformation? = null
var attachmentnames: Array<String> = arrayOf()
var attachments: Array<String> = arrayOf()
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ import org.springframework.stereotype.Repository

@Repository
interface AttachmentRepository : JpaRepository<Attachment, Int> {

fun findByReceiptId(receiptId: Int): List<Attachment>
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import org.springframework.stereotype.Repository

@Repository
interface CardRepository : JpaRepository<Card, Int> {
fun findFirstByReceiptId(id: Int): Card?



}
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.example.autobank.repository

import com.example.autobank.data.receipt.Card
import com.example.autobank.data.receipt.Payment
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.stereotype.Repository

@Repository
interface PaymentRepository : JpaRepository<Payment, Int> {
fun findFirstByReceiptId(id: Int): Payment?


}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import org.springframework.stereotype.Repository

@Repository
interface ReceiptRepository : JpaRepository<Receipt, Int> {
fun findAllByOnlineUserId(onlineUserId: Int): List<Receipt>


}
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,8 @@ class AttachmentService {
return attachmentRepository.save(attachment);
}

fun getAttachmentsByReceiptId(id: Int): List<Attachment> {
return attachmentRepository.findByReceiptId(id);
}

}
52 changes: 51 additions & 1 deletion src/main/kotlin/com/example/autobank/service/ReceiptService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,61 @@ class ReceiptService {

val response = ReceiptResponseBody()
response.receipt = storedReceipt
response.attachmentnames = attachmentsnames.toTypedArray()
response.attachments = attachmentsnames.toTypedArray()
response.receiptPaymentInformation = receiptRequestBody.receiptPaymentInformation

return response

}

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

for (receipt in receipts) {
receipt.onlineUserId = null
}

return receipts
}

fun getReceipt(id: Int): ReceiptResponseBody? {
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")
}
receipt.onlineUserId = null

val receiptResponseBody = ReceiptResponseBody()
receiptResponseBody.receipt = 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





}


Expand Down

0 comments on commit 8592d40

Please sign in to comment.