Pruduct, Purchase, Producttest

This commit is contained in:
Bossanyi Tibor
2020-11-06 07:14:41 +01:00
parent 83939110af
commit 6cf74d2738
20 changed files with 551 additions and 3 deletions
@@ -0,0 +1,26 @@
package com.aitrainer.api.controller
import com.aitrainer.api.model.Product
import com.aitrainer.api.repository.ProductRepository
import org.slf4j.LoggerFactory
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
@RestController
@RequestMapping("/api")
class ProductController(private val productRepository: ProductRepository) {
private val logger = LoggerFactory.getLogger(javaClass)
@GetMapping("/product")
fun getAllProducts(): ResponseEntity<List<Product>> {
val products = productRepository.findAll()
logger.info("Get all products")
return if(products.isNotEmpty())
ResponseEntity.ok().body(products) else
ResponseEntity.notFound().build()
}
}
@@ -0,0 +1,33 @@
package com.aitrainer.api.controller
import com.aitrainer.api.ApiApplication
import com.aitrainer.api.repository.ConfigurationRepository
import org.aspectj.lang.annotation.Aspect
import org.aspectj.lang.annotation.Before
import org.aspectj.lang.annotation.Pointcut
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Component
@Suppress("unused")
@Aspect
@Component
class ProductControllerAspect {
private val logger = LoggerFactory.getLogger(ApiApplication::class.simpleName)
@Autowired
private lateinit var configurationRepository: ConfigurationRepository
@Autowired
private lateinit var properties: ApplicationProperties
@Suppress("unused")
@Pointcut("execution(* com.aitrainer.api.controller.ProductController.*())")
fun productControllerAspect() {
}
@Before("productControllerAspect()")
fun loggingAop() {
Singleton.checkDBUpdate(configurationRepository, properties)
}
}
@@ -0,0 +1,32 @@
package com.aitrainer.api.controller
import com.aitrainer.api.model.ProductTest
import com.aitrainer.api.repository.ProductTestRepository
import org.slf4j.LoggerFactory
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.*
import javax.validation.Valid
@RestController
@RequestMapping("/api")
class ProductTestController(private val productTestRepository: ProductTestRepository) {
private val logger = LoggerFactory.getLogger(javaClass)
@GetMapping("/product_test/customer/{id}")
fun getAllByCustomerId(@PathVariable(value = "id") customerId: Long): ResponseEntity<List<ProductTest>> {
val productTestList = productTestRepository.findByCustomerId(customerId)
logger.info("Get all productTest by customerId $productTestList")
return if(productTestList.isNotEmpty())
ResponseEntity.ok().body(productTestList) else
ResponseEntity.notFound().build()
}
@PostMapping("/product_test")
fun insertProductTest(@Valid @RequestBody productTest: ProductTest): ResponseEntity<ProductTest> {
val newProductTest = productTestRepository.save(productTest)
logger.info("Create new productTest: $newProductTest")
return ResponseEntity.ok().body(newProductTest)
}
}
@@ -0,0 +1,33 @@
package com.aitrainer.api.controller
import com.aitrainer.api.ApiApplication
import com.aitrainer.api.repository.ConfigurationRepository
import org.aspectj.lang.annotation.Aspect
import org.aspectj.lang.annotation.Before
import org.aspectj.lang.annotation.Pointcut
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Component
@Suppress("unused")
@Aspect
@Component
class ProductTestControllerAspect {
private val logger = LoggerFactory.getLogger(ApiApplication::class.simpleName)
@Autowired
private lateinit var configurationRepository: ConfigurationRepository
@Autowired
private lateinit var properties: ApplicationProperties
@Suppress("unused")
@Pointcut("execution(* com.aitrainer.api.controller.ProductTestController.*())")
fun productTestControllerAspect() {
}
@Before("productTestControllerAspect()")
fun loggingAop() {
Singleton.checkDBUpdate(configurationRepository, properties)
}
}
@@ -0,0 +1,32 @@
package com.aitrainer.api.controller
import com.aitrainer.api.model.Purchase
import com.aitrainer.api.repository.PurchaseRepository
import org.slf4j.LoggerFactory
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.*
import javax.validation.Valid
@RestController
@RequestMapping("/api")
class PurchaseController(private val purchaseRepository: PurchaseRepository) {
private val logger = LoggerFactory.getLogger(javaClass)
@GetMapping("/purchase/customer/{id}")
fun getAllByCustomerId(@PathVariable(value = "id") customerId: Long): ResponseEntity<List<Purchase>> {
val purchaseList = purchaseRepository.findByCustomerId(customerId)
logger.info("Get all purchase by customerId")
return if(purchaseList.isNotEmpty())
ResponseEntity.ok().body(purchaseList) else
ResponseEntity.notFound().build()
}
@PostMapping("/purchase")
fun insertPurchase(@Valid @RequestBody purchase: Purchase): ResponseEntity<Purchase> {
val newPurchase = purchaseRepository.save(purchase)
logger.info("Create new purchase: $newPurchase")
return ResponseEntity.ok().body(newPurchase)
}
}
@@ -0,0 +1,33 @@
package com.aitrainer.api.controller
import com.aitrainer.api.ApiApplication
import com.aitrainer.api.repository.ConfigurationRepository
import org.aspectj.lang.annotation.Aspect
import org.aspectj.lang.annotation.Before
import org.aspectj.lang.annotation.Pointcut
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Component
@Suppress("unused")
@Aspect
@Component
class PurchaseControllerAspect {
private val logger = LoggerFactory.getLogger(ApiApplication::class.simpleName)
@Autowired
private lateinit var configurationRepository: ConfigurationRepository
@Autowired
private lateinit var properties: ApplicationProperties
@Suppress("unused")
@Pointcut("execution(* com.aitrainer.api.controller.PurchaseController.*())")
fun purchaseControllerAspect() {
}
@Before("purchaseControllerAspect()")
fun loggingAop() {
Singleton.checkDBUpdate(configurationRepository, properties)
}
}
@@ -0,0 +1,24 @@
package com.aitrainer.api.model
import org.springframework.lang.NonNull
import javax.persistence.Entity
import javax.persistence.GeneratedValue
import javax.persistence.GenerationType
import javax.persistence.Id
import javax.validation.constraints.NotBlank
/*enum class ProductType(val type: String) {
SUBSCRIPTION("subscription"),
IN_APP_CURR("in-app-currency")
}*/
@Entity
data class Product (
@Id @GeneratedValue(strategy = GenerationType.IDENTITY) @get: NonNull var productId: Int,
@get: NotBlank var name: String = "",
@get: NotBlank var description: String = "",
@get: NonNull var type: String? = null,
@get: NonNull var validFrom: String? = null,
@get: NonNull var validTo: String? = null
)
@@ -0,0 +1,16 @@
package com.aitrainer.api.model
import org.springframework.lang.NonNull
import javax.persistence.Entity
import javax.persistence.GeneratedValue
import javax.persistence.GenerationType
import javax.persistence.Id
@Entity
data class ProductTest (
@Id @GeneratedValue(strategy = GenerationType.IDENTITY) @get: NonNull var productTestId: Int = 0,
@get: NonNull var customerId: Long = 0,
@get: NonNull var productId: Int = 0,
@get: NonNull var dateView: String? = null,
@get: NonNull var purchaseClick: Boolean = false
)
@@ -0,0 +1,18 @@
package com.aitrainer.api.model
import org.springframework.lang.NonNull
import javax.persistence.Entity
import javax.persistence.GeneratedValue
import javax.persistence.GenerationType
import javax.persistence.Id
import javax.validation.constraints.NotBlank
@Entity
data class Purchase (
@Id @GeneratedValue(strategy = GenerationType.IDENTITY) @get: NonNull var purchaseId: Long = 0,
@get: NonNull var customerId: Long = 0,
@get: NonNull var productId: Int = 0,
@get: NonNull var purchaseSum: Double? = null,
@get: NonNull var dateAdd: String? = null,
@get: NotBlank var currency: String = ""
)
@@ -0,0 +1,9 @@
package com.aitrainer.api.repository
import com.aitrainer.api.model.Product
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.stereotype.Repository
@Repository
interface ProductRepository : JpaRepository<Product, Long> {
}
@@ -0,0 +1,10 @@
package com.aitrainer.api.repository
import com.aitrainer.api.model.ProductTest
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.stereotype.Repository
@Repository
interface ProductTestRepository : JpaRepository<ProductTest, Long> {
fun findByCustomerId(customerId: Long): List<ProductTest>
}
@@ -0,0 +1,10 @@
package com.aitrainer.api.repository
import com.aitrainer.api.model.Purchase
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.stereotype.Repository
@Repository
interface PurchaseRepository : JpaRepository<Purchase, Long> {
fun findByCustomerId(customerId: Long): List<Purchase>
}
@@ -16,6 +16,6 @@ logging.config=classpath:logback-spring.xml
logging.file=logs
# if the database structure has been changed, increment this version number
application.version=1.0.10
application.version=1.0.11
jwt.secret=aitrainer
+1 -1
View File
@@ -16,6 +16,6 @@ logging.config=classpath:logback-spring.xml
logging.file=logs
# if the database structure has been changed, increment this version number
application.version=1.0.10
application.version=1.0.11
jwt.secret=aitrainer
@@ -0,0 +1,64 @@
package com.aitrainer.api.test
import com.aitrainer.api.controller.ProductTestController
import com.aitrainer.api.repository.ProductTestRepository
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import kotlin.test.assertEquals
import kotlin.test.assertTrue
import com.aitrainer.api.model.ProductTest
import org.junit.jupiter.api.TestInstance
@SpringBootTest
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class ProductTestTest {
@Autowired
private lateinit var productTestRepository: ProductTestRepository
@Test
fun testGetProductTests() {
val list = productTestRepository.findByCustomerId(1)
print("List $list")
val controller = ProductTestController(productTestRepository)
val response = controller.getAllByCustomerId(62)
val productTest = response.body
print ("resp $response.body" )
assertTrue(productTest is List)
assertTrue(productTest.isNotEmpty())
assertEquals(productTest.size, 2)
assertEquals(productTest[0].productId, 2)
assertEquals(productTest[0].dateView, "2020-10-01 12:00:00")
}
@Test
fun testInsertProductTest() {
val productTest = ProductTest(
customerId = 2,
productId = 2,
dateView = "2020-11-01 12:00:00",
purchaseClick = false
)
val controller = ProductTestController(productTestRepository)
val response = controller.insertProductTest(productTest)
val productTestNew = response.body
assertTrue(productTestNew is ProductTest)
assertEquals(productTestNew.productId, 2)
assertEquals(productTestNew.customerId, 2)
productTestRepository.delete(productTestNew)
}
}
@@ -0,0 +1,34 @@
package com.aitrainer.api.test
import com.aitrainer.api.controller.ProductController
import com.aitrainer.api.repository.ProductRepository
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import kotlin.test.assertEquals
import kotlin.test.assertTrue
@SpringBootTest
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class ProductTesting {
@Autowired
private lateinit var productRepository: ProductRepository
@Test
fun testGetProducts() {
val controller = ProductController(productRepository)
val response = controller.getAllProducts()
val products = response.body
assertTrue(products is List)
assertTrue(products.isNotEmpty())
assertEquals(products.size, 12)
assertEquals(products[0].name, "Subscription A")
}
}
@@ -0,0 +1,57 @@
package com.aitrainer.api.test
import com.aitrainer.api.controller.PurchaseController
import com.aitrainer.api.model.Purchase
import com.aitrainer.api.repository.PurchaseRepository
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import kotlin.test.assertEquals
import kotlin.test.assertTrue
@SpringBootTest
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class PurchaseTest {
@Autowired
private lateinit var purchaseRepository: PurchaseRepository
@Test
fun testGetPurchaseByCustomerId() {
val controller = PurchaseController(purchaseRepository)
val response = controller.getAllByCustomerId(62)
val purchases = response.body
assertTrue(purchases is List)
assertTrue(purchases.isNotEmpty())
assertEquals(purchases.size, 2)
assertEquals(purchases[0].productId, 1)
}
@Test
fun testPurchaseInsert() {
val purchase = Purchase(
productId = 2,
dateAdd = "2020-11-05 12:00:00",
customerId = 1,
purchaseSum = 2000.0,
currency = "HUF"
)
val controller = PurchaseController(purchaseRepository)
val response = controller.insertPurchase(purchase)
val purchaseNew = response.body
assertEquals(purchaseNew!!.productId, 2)
assertEquals(purchaseNew.customerId, 1)
//purchaseRepository.delete(purchaseNew)
}
}