Skip to content

Commit

Permalink
Fixed paymentinformation for receipt
Browse files Browse the repository at this point in the history
  • Loading branch information
akselsf committed Sep 17, 2024
1 parent 27b9700 commit a1db698
Show file tree
Hide file tree
Showing 15 changed files with 139 additions and 19 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 Down

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 receipt_id: 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 attachmentnames: Array<String> = arrayOf()
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
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

Expand Down
11 changes: 11 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,11 @@
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> {


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

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> {


}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
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

Expand Down
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 Down
42 changes: 37 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,54 @@ 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.attachmentnames = attachmentsnames.toTypedArray()
response.receiptPaymentInformation = receiptRequestBody.receiptPaymentInformation

return response

}

Expand Down

0 comments on commit a1db698

Please sign in to comment.