API 1.0.15b CustomerExerciseDevice standalone
This commit is contained in:
parent
3a70bcd10f
commit
11c9a6d112
@ -2,7 +2,6 @@ package com.aitrainer.api.controller
|
||||
|
||||
import com.aitrainer.api.model.Customer
|
||||
import com.aitrainer.api.model.User
|
||||
import com.aitrainer.api.repository.CustomerExerciseDeviceRepository
|
||||
import com.aitrainer.api.service.ServiceBeans
|
||||
import com.aitrainer.api.repository.CustomerRepository
|
||||
import org.slf4j.LoggerFactory
|
||||
@ -16,8 +15,7 @@ import javax.validation.Valid
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api")
|
||||
class CustomerController ( private val customerRepository: CustomerRepository,
|
||||
private val customerExerciseDeviceRepository: CustomerExerciseDeviceRepository ) {
|
||||
class CustomerController ( private val customerRepository: CustomerRepository) {
|
||||
private val logger = LoggerFactory.getLogger(javaClass)
|
||||
|
||||
@Autowired
|
||||
@ -37,10 +35,6 @@ class CustomerController ( private val customerRepository: CustomerRepository,
|
||||
@GetMapping("/customers/{id}")
|
||||
fun getCustomerById(@PathVariable(value = "id") customerId: Long, @RequestHeader headers: HttpHeaders): ResponseEntity<Customer> {
|
||||
val customer: Customer? = customerRepository.findById(customerId).orElse(null)
|
||||
if ( customer != null) {
|
||||
val list = customerExerciseDeviceRepository.findByCustomerId(customer.customerId)
|
||||
customer.exerciseDevices = list
|
||||
}
|
||||
return if (customer == null) ResponseEntity.notFound().build() else ResponseEntity.ok().body(customer)
|
||||
}
|
||||
|
||||
@ -48,10 +42,7 @@ class CustomerController ( private val customerRepository: CustomerRepository,
|
||||
@GetMapping("/customers/find_by_firebaseuid/{uid}")
|
||||
fun getCustomerByFirebaseUid(@PathVariable(value = "uid") firebaseUid: String): ResponseEntity<Customer> {
|
||||
val customer: Customer? = customerRepository.findByFirebaseUid(firebaseUid)
|
||||
if ( customer != null) {
|
||||
val list = customerExerciseDeviceRepository.findByCustomerId(customer.customerId)
|
||||
customer.exerciseDevices = list
|
||||
}
|
||||
|
||||
return if (customer == null) ResponseEntity.notFound().build() else ResponseEntity.ok().body(customer)
|
||||
}
|
||||
|
||||
@ -59,10 +50,6 @@ class CustomerController ( private val customerRepository: CustomerRepository,
|
||||
@GetMapping("/customers/find_by_email/{email}")
|
||||
fun getCustomerByEmail(@PathVariable(value = "email") email: String): ResponseEntity<Customer> {
|
||||
val customer: Customer? = customerRepository.findByEmail(email)
|
||||
if ( customer != null) {
|
||||
val list = customerExerciseDeviceRepository.findByCustomerId(customer.customerId)
|
||||
customer.exerciseDevices = list
|
||||
}
|
||||
return if (customer == null) ResponseEntity.notFound().build() else ResponseEntity.ok().body(customer)
|
||||
}
|
||||
|
||||
@ -89,7 +76,7 @@ class CustomerController ( private val customerRepository: CustomerRepository,
|
||||
val returnCustomer: Customer = customerRepository.findById(customerId).orElse(null)
|
||||
?: return ResponseEntity.notFound().build()
|
||||
|
||||
returnCustomer.firebaseUid = firebaseUid;
|
||||
returnCustomer.firebaseUid = firebaseUid
|
||||
return ResponseEntity.ok().body(customerRepository.save(returnCustomer))
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,32 @@
|
||||
package com.aitrainer.api.controller
|
||||
|
||||
import com.aitrainer.api.model.CustomerExerciseDevice
|
||||
import com.aitrainer.api.repository.CustomerExerciseDeviceRepository
|
||||
import org.slf4j.LoggerFactory
|
||||
import org.springframework.http.ResponseEntity
|
||||
import org.springframework.web.bind.annotation.*
|
||||
import javax.validation.Valid
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api")
|
||||
class CustomerExerciseDeviceController(private val customerExerciseDeviceRepository: CustomerExerciseDeviceRepository) {
|
||||
private val logger = LoggerFactory.getLogger(javaClass)
|
||||
|
||||
@GetMapping("/customer_exercise_device/customer/{id}")
|
||||
fun getAllByCustomerId(@PathVariable(value = "id") customerId: Long): ResponseEntity<List<CustomerExerciseDevice>> {
|
||||
|
||||
val deviceList = customerExerciseDeviceRepository.findByCustomerId(customerId)
|
||||
logger.info("Get all devices by customerId $deviceList")
|
||||
|
||||
return if(deviceList.isNotEmpty())
|
||||
ResponseEntity.ok().body(deviceList) else
|
||||
ResponseEntity.notFound().build()
|
||||
}
|
||||
|
||||
@PostMapping("/customer_exercise_device")
|
||||
fun insertDevice(@Valid @RequestBody customerExerciseDevice: CustomerExerciseDevice): ResponseEntity<CustomerExerciseDevice> {
|
||||
val newCustomerExerciseDevice = customerExerciseDeviceRepository.save(customerExerciseDevice)
|
||||
logger.info("Create new device: $newCustomerExerciseDevice")
|
||||
return ResponseEntity.ok().body(newCustomerExerciseDevice)
|
||||
}
|
||||
}
|
@ -1,7 +1,5 @@
|
||||
package com.aitrainer.api.model
|
||||
|
||||
import org.hibernate.annotations.Fetch
|
||||
import org.hibernate.annotations.FetchMode
|
||||
import javax.persistence.*
|
||||
|
||||
@Entity
|
||||
@ -20,7 +18,6 @@ data class Customer (
|
||||
var trainerId: Long = 0,
|
||||
var password: String? = "",
|
||||
var birthYear:Int = 0,
|
||||
// var weight: Int = 0,
|
||||
var goal: String? = null,
|
||||
var fitnessLevel: String = "beginner",
|
||||
var bodyType: String? = null,
|
||||
@ -28,8 +25,4 @@ data class Customer (
|
||||
|
||||
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
var customerId: Long = 0
|
||||
) {
|
||||
@OneToMany(cascade = [(CascadeType.ALL)], fetch = FetchType.EAGER, mappedBy = "customer")
|
||||
@Fetch(value = FetchMode.SUBSELECT)
|
||||
var exerciseDevices: List<CustomerExerciseDevice> = mutableListOf<CustomerExerciseDevice>()
|
||||
}
|
||||
)
|
||||
|
@ -12,14 +12,9 @@ data class CustomerExerciseDevice (
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
val customerExerciseDeviceId: Long = 0,
|
||||
@get: NotNull var exerciseDeviceId: Long?,
|
||||
//@get: NotNull var customerId: Long?,
|
||||
@get: NotNull var customerId: Long?,
|
||||
@get: NotNull var favourite: Boolean?,
|
||||
@get: NotNull var dateAdd: String? = null
|
||||
|
||||
) {
|
||||
@ManyToOne(fetch = FetchType.EAGER, optional = false)
|
||||
@JoinColumn(name = "customerId", nullable = false)
|
||||
@JsonIgnore
|
||||
val customer: Customer? = null
|
||||
}
|
||||
)
|
||||
|
||||
|
@ -8,7 +8,7 @@ import org.springframework.stereotype.Repository
|
||||
@Repository
|
||||
interface CustomerExerciseDeviceRepository: JpaRepository<CustomerExerciseDevice, Long> {
|
||||
@Query(" FROM CustomerExerciseDevice " +
|
||||
" WHERE customer.customerId = :customerId"
|
||||
" WHERE customerId = :customerId"
|
||||
)
|
||||
fun findByCustomerId(customerId: Long): List<CustomerExerciseDevice>
|
||||
}
|
@ -0,0 +1,59 @@
|
||||
package com.aitrainer.api.test
|
||||
|
||||
import com.aitrainer.api.controller.CustomerExerciseDeviceController
|
||||
import com.aitrainer.api.controller.PurchaseController
|
||||
import com.aitrainer.api.model.Customer
|
||||
import com.aitrainer.api.model.CustomerExerciseDevice
|
||||
import com.aitrainer.api.repository.CustomerExerciseDeviceRepository
|
||||
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 CustomerExerciseDeviceTest {
|
||||
|
||||
@Autowired
|
||||
private lateinit var customerExerciseDeviceRepository: CustomerExerciseDeviceRepository
|
||||
|
||||
@Test
|
||||
fun testGetPurchaseByCustomerId() {
|
||||
val controller = CustomerExerciseDeviceController(customerExerciseDeviceRepository)
|
||||
val response = controller.getAllByCustomerId(90)
|
||||
|
||||
val devices = response.body
|
||||
|
||||
assertTrue(devices is List)
|
||||
assertTrue(devices.isNotEmpty())
|
||||
assertEquals(devices.size, 2)
|
||||
assertEquals(devices[0].exerciseDeviceId, 1)
|
||||
assertEquals(devices[0].customerId, 90)
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
fun testDeviceInsert() {
|
||||
val customerExerciseDevice = CustomerExerciseDevice(
|
||||
exerciseDeviceId = 3,
|
||||
favourite = false,
|
||||
dateAdd = "2020-11-23 04:32:00",
|
||||
customerId = 62
|
||||
)
|
||||
|
||||
|
||||
val controller = CustomerExerciseDeviceController(customerExerciseDeviceRepository)
|
||||
val response = controller.insertDevice(customerExerciseDevice)
|
||||
|
||||
val customerDeviceNew = response.body
|
||||
|
||||
assertEquals(customerDeviceNew!!.customerId, 62)
|
||||
assertEquals(customerDeviceNew.exerciseDeviceId, 3)
|
||||
|
||||
customerExerciseDeviceRepository.delete(customerDeviceNew)
|
||||
}
|
||||
|
||||
}
|
@ -3,7 +3,6 @@ package com.aitrainer.api.test
|
||||
import com.aitrainer.api.controller.CustomerController
|
||||
import com.aitrainer.api.model.Customer
|
||||
import com.aitrainer.api.model.User
|
||||
import com.aitrainer.api.repository.CustomerExerciseDeviceRepository
|
||||
import com.aitrainer.api.repository.CustomerRepository
|
||||
import com.aitrainer.api.service.ServiceBeans
|
||||
import org.junit.jupiter.api.BeforeAll
|
||||
@ -32,8 +31,6 @@ class CustomerTests {
|
||||
@Autowired
|
||||
private lateinit var customerRepository: CustomerRepository
|
||||
|
||||
@Autowired
|
||||
private lateinit var customerExerciseDeviceRepository: CustomerExerciseDeviceRepository
|
||||
|
||||
private var insertedId: Long? = null
|
||||
|
||||
@ -44,15 +41,13 @@ class CustomerTests {
|
||||
assertEquals( customer.name, "Átlag 18 éves fiú")
|
||||
|
||||
val id2: Long = 90
|
||||
val controller = CustomerController(customerRepository, customerExerciseDeviceRepository)
|
||||
val controller = CustomerController(customerRepository)
|
||||
val response = controller.getCustomerById(id2, HttpHeaders.EMPTY)
|
||||
|
||||
val customer2: Customer = response.body as Customer
|
||||
assertNotNull (customer2)
|
||||
|
||||
assertEquals(customer2.email, "sw@andio.biz")
|
||||
assertEquals(customer2.exerciseDevices.size, 2)
|
||||
assertEquals(customer2.exerciseDevices[0].exerciseDeviceId, 1)
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -61,7 +56,6 @@ class CustomerTests {
|
||||
val customers: List<Customer> = customerRepository.findByTrainerId( id )
|
||||
assertEquals(customers.count(), 3)
|
||||
assertEquals( customers[0].name, "Átlag 13 éves fiú")
|
||||
assertEquals(customers[0].exerciseDevices.size, 0)
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -134,7 +128,7 @@ class CustomerTests {
|
||||
//customer.weight = 79
|
||||
customer.birthYear = 1972
|
||||
|
||||
val customerController = CustomerController(customerRepository, customerExerciseDeviceRepository)
|
||||
val customerController = CustomerController(customerRepository)
|
||||
var response: ResponseEntity<*> = customerController.updateCustomerById(id, customer, HttpHeaders.readOnlyHttpHeaders(HttpHeaders.EMPTY) )
|
||||
print ("body " + response.body)
|
||||
var newCustomer: Customer? = response.body as Customer
|
||||
@ -174,7 +168,7 @@ class CustomerTests {
|
||||
password = user.password
|
||||
firebaseUid = user.firebaseUid
|
||||
}
|
||||
val customerController = CustomerController(customerRepository,customerExerciseDeviceRepository)
|
||||
val customerController = CustomerController(customerRepository)
|
||||
customerController.serviceBeans = serviceBean
|
||||
var response: ResponseEntity<*> = customerController.registration(json)
|
||||
print("body " + response.body)
|
||||
@ -197,7 +191,7 @@ class CustomerTests {
|
||||
|
||||
insertedId = savedCustomer.customerId
|
||||
|
||||
val customerController = CustomerController(customerRepository,customerExerciseDeviceRepository)
|
||||
val customerController = CustomerController(customerRepository)
|
||||
val response: ResponseEntity<*> = customerController.updateCustomerFirebaseUidById(insertedId!!, "3FirebusaeId4")
|
||||
val newCustomer2: Customer = response.body as Customer
|
||||
assertEquals(response.statusCode, HttpStatus.OK)
|
||||
@ -212,7 +206,7 @@ class CustomerTests {
|
||||
@Test
|
||||
fun testGetCustomerByFirebaseUid() {
|
||||
val uid = "3FirebaseU1d"
|
||||
val customerController = CustomerController(customerRepository,customerExerciseDeviceRepository)
|
||||
val customerController = CustomerController(customerRepository)
|
||||
val response: ResponseEntity<*> = customerController.getCustomerByFirebaseUid(uid)
|
||||
assertEquals(response.statusCode, HttpStatus.OK)
|
||||
val newCustomer: Customer = response.body as Customer
|
||||
@ -224,7 +218,7 @@ class CustomerTests {
|
||||
@Test
|
||||
fun testGetCustomerByEmail() {
|
||||
val email = "sw2@andio.biz"
|
||||
val customerController = CustomerController(customerRepository,customerExerciseDeviceRepository)
|
||||
val customerController = CustomerController(customerRepository)
|
||||
val response: ResponseEntity<*> = customerController.getCustomerByEmail(email)
|
||||
assertEquals(response.statusCode, HttpStatus.OK)
|
||||
|
||||
@ -233,10 +227,35 @@ class CustomerTests {
|
||||
assertEquals(newCustomer.name, "Bos")
|
||||
assertEquals(newCustomer.email, "sw2@andio.biz")
|
||||
assertEquals(newCustomer.firebaseUid, "3FirebaseU1d")
|
||||
assertEquals(newCustomer.exerciseDevices.size, 0)
|
||||
|
||||
}
|
||||
|
||||
/*@Test
|
||||
fun testAddCustomerExerciseDevice() {
|
||||
val customer: Customer = customerRepository.findById(103) .orElse(null)
|
||||
val customerExerciseDevice = CustomerExerciseDevice(
|
||||
exerciseDeviceId = 1,
|
||||
favourite = false,
|
||||
dateAdd = "2020-11-23 20:00"
|
||||
)
|
||||
customerExerciseDevice.customer = customer
|
||||
val listDevice : List<CustomerExerciseDevice> = listOf(customerExerciseDevice)
|
||||
|
||||
customer.exerciseDevices = listDevice
|
||||
val newCustomer: Customer = customerRepository.save(customer)
|
||||
|
||||
assertEquals(newCustomer.email, "sw2@andio.biz")
|
||||
assertEquals(newCustomer.exerciseDevices.size, 1)
|
||||
assertEquals(newCustomer.exerciseDevices[0].exerciseDeviceId, 1)
|
||||
|
||||
val deletedList: List<CustomerExerciseDevice> = listOf()
|
||||
customer.exerciseDevices = deletedList
|
||||
val newCustomer2 = customerRepository.save(customer)
|
||||
assertEquals(newCustomer2.email, "sw2@andio.biz")
|
||||
assertEquals(newCustomer2.exerciseDevices.size, 0)
|
||||
|
||||
}
|
||||
*/
|
||||
/*@Test fun _testLogin() {
|
||||
val json = "{\"username\":\"bosi2@example.com\",\"password\":\"94333385\"}"
|
||||
val user: User = User().fromJson(json)
|
||||
|
Loading…
Reference in New Issue
Block a user