Skip to content

Commit

Permalink
Merge pull request #37 from appKom/33-create-get-and-delete-receipts-…
Browse files Browse the repository at this point in the history
…endpoint

33 create get and delete receipts endpoint
  • Loading branch information
akselsf authored Sep 17, 2024
2 parents 27b9700 + 8592d40 commit a140c7b
Show file tree
Hide file tree
Showing 15 changed files with 220 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package com.example.autobank.controller

import com.example.autobank.data.Receipt
import com.example.autobank.data.ReceiptRequestBody
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.service.ImageService
import com.example.autobank.service.ReceiptService
import org.springframework.beans.factory.annotation.Autowired
Expand All @@ -20,7 +21,7 @@ class ReceiptController() {
lateinit var receiptService: ReceiptService

@PostMapping("/create")
fun createReceipt(@RequestBody receipt: ReceiptRequestBody): ResponseEntity<Receipt> {
fun createReceipt(@RequestBody receipt: ReceiptRequestBody): ResponseEntity<ReceiptResponseBody> {
return try {
val res = receiptService.createReceipt(receipt)
ResponseEntity.ok(res)
Expand All @@ -31,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()
}
}


}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.example.autobank.data
package com.example.autobank.data.receipt

import jakarta.persistence.*
import org.jetbrains.annotations.NotNull
Expand Down
21 changes: 21 additions & 0 deletions src/main/kotlin/com/example/autobank/data/receipt/Card.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.example.autobank.data.receipt

import jakarta.persistence.*
import org.jetbrains.annotations.NotNull

@Entity
@Table(name = "card")
class Card (
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
@NotNull
val id: Int,

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

@Column(name = "card_number")
val card_number: String,

)
21 changes: 21 additions & 0 deletions src/main/kotlin/com/example/autobank/data/receipt/Payment.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.example.autobank.data.receipt

import jakarta.persistence.*
import org.jetbrains.annotations.NotNull

@Entity
@Table(name = "payment")
class Payment (
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
@NotNull
val id: Int,

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

@Column(name = "account_number")
val account_number: String,

)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.example.autobank.data
package com.example.autobank.data.receipt


import jakarta.persistence.*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.example.autobank.data.receipt

class ReceiptPaymentInformation (
val cardnumber: String?,
val accountnumber: String?,
val usedOnlineCard: Boolean,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.example.autobank.data.receipt

import com.example.autobank.data.receipt.Receipt
import jakarta.persistence.Entity


class ReceiptRequestBody {
val receipt: Receipt? = null
val receiptPaymentInformation: ReceiptPaymentInformation? = null
val attachments: Array<String> = arrayOf()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.example.autobank.data.receipt

import com.example.autobank.data.receipt.Receipt
import jakarta.persistence.Entity


class ReceiptResponseBody {
var receipt: Receipt? = null
var receiptPaymentInformation: ReceiptPaymentInformation? = null
var attachments: Array<String> = arrayOf()
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.example.autobank.repository

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

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

fun findByReceiptId(receiptId: Int): List<Attachment>
}
13 changes: 13 additions & 0 deletions src/main/kotlin/com/example/autobank/repository/CardRepository.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.example.autobank.repository

import com.example.autobank.data.receipt.Card
import org.springframework.data.jpa.repository.JpaRepository
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
@@ -0,0 +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
@@ -1,11 +1,12 @@
package com.example.autobank.repository

import com.example.autobank.data.Receipt
import com.example.autobank.data.receipt.Receipt
import org.springframework.data.jpa.repository.JpaRepository
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
@@ -1,6 +1,6 @@
package com.example.autobank.service

import com.example.autobank.data.Attachment
import com.example.autobank.data.receipt.Attachment
import com.example.autobank.repository.AttachmentRepository
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
Expand All @@ -16,4 +16,8 @@ class AttachmentService {
return attachmentRepository.save(attachment);
}

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

}
92 changes: 87 additions & 5 deletions src/main/kotlin/com/example/autobank/service/ReceiptService.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.example.autobank.service

import com.example.autobank.data.Attachment
import com.example.autobank.data.Receipt
import com.example.autobank.data.ReceiptRequestBody
import com.example.autobank.data.receipt.*
import com.example.autobank.repository.CardRepository
import com.example.autobank.repository.PaymentRepository
import com.example.autobank.repository.ReceiptRepository
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
Expand All @@ -23,22 +23,104 @@ class ReceiptService {
@Autowired
lateinit var attachmentService: AttachmentService

@Autowired
lateinit var cardRepository: CardRepository

@Autowired
lateinit var paymentRepository: PaymentRepository



fun createReceipt(receiptRequestBody: ReceiptRequestBody): Receipt {
fun createReceipt(receiptRequestBody: ReceiptRequestBody): ReceiptResponseBody {
val user = onlineUserService.getOnlineUser() ?: throw Exception("User not found")
val receiptinfo = receiptRequestBody.receipt ?: throw Exception("Receipt not sent")
receiptinfo.onlineUserId = user.id;

val storedReceipt = receiptRepository.save(receiptinfo);


/**
* Save attachments
*/
val attachments = receiptRequestBody.attachments
val attachmentsnames = mutableListOf<String>()
attachments.forEach { attachment ->
val imgname = imageService.uploadImage(attachment)
attachmentService.createAttachment(Attachment(0, storedReceipt.id, imgname))
attachmentsnames.add(imgname)
}

/**
* Save payment information
*/
if (receiptRequestBody.receiptPaymentInformation != null && receiptRequestBody.receiptPaymentInformation.usedOnlineCard && receiptRequestBody.receiptPaymentInformation.cardnumber != null && receiptRequestBody.receiptPaymentInformation.cardnumber != "") {
val card = Card(0, storedReceipt.id, receiptRequestBody.receiptPaymentInformation.cardnumber)
cardRepository.save(card)
} else if (receiptRequestBody.receiptPaymentInformation != null && !receiptRequestBody.receiptPaymentInformation.usedOnlineCard && receiptRequestBody.receiptPaymentInformation.accountnumber != null && receiptRequestBody.receiptPaymentInformation.accountnumber != "") {
val payment = Payment(0, storedReceipt.id, receiptRequestBody.receiptPaymentInformation.accountnumber)
paymentRepository.save(payment)
} else {
throw Exception("Payment information not sent")
}

storedReceipt.onlineUserId = null
return storedReceipt

val response = ReceiptResponseBody()
response.receipt = storedReceipt
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 a140c7b

Please sign in to comment.