API 1.0.15 CustomerExerciseDevice
This commit is contained in:
@@ -103,4 +103,10 @@ class ControllerAspect {
|
||||
Singleton.checkDBUpdate(configurationRepository, properties)
|
||||
}
|
||||
|
||||
@Before("execution(* com.aitrainer.api.controller.ExerciseDeviceController.*(..))")
|
||||
fun exerciseDeviceControllerAspect(joinPoint: JoinPoint) {
|
||||
println("exerciseDevice controller")
|
||||
Singleton.checkDBUpdate(configurationRepository, properties)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -2,6 +2,7 @@ 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
|
||||
@@ -10,13 +11,13 @@ import org.springframework.http.HttpHeaders
|
||||
import org.springframework.http.ResponseEntity
|
||||
import org.springframework.security.access.annotation.Secured
|
||||
import org.springframework.web.bind.annotation.*
|
||||
import java.util.*
|
||||
import javax.validation.Valid
|
||||
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api")
|
||||
class CustomerController ( private val customerRepository: CustomerRepository ) {
|
||||
class CustomerController ( private val customerRepository: CustomerRepository,
|
||||
private val customerExerciseDeviceRepository: CustomerExerciseDeviceRepository ) {
|
||||
private val logger = LoggerFactory.getLogger(javaClass)
|
||||
|
||||
@Autowired
|
||||
@@ -35,15 +36,22 @@ class CustomerController ( private val customerRepository: CustomerRepository )
|
||||
@Secured
|
||||
@GetMapping("/customers/{id}")
|
||||
fun getCustomerById(@PathVariable(value = "id") customerId: Long, @RequestHeader headers: HttpHeaders): ResponseEntity<Customer> {
|
||||
return customerRepository.findById(customerId).map { customer ->
|
||||
ResponseEntity.ok(customer)
|
||||
}.orElse(ResponseEntity.notFound().build())
|
||||
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)
|
||||
}
|
||||
|
||||
@Secured
|
||||
@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)
|
||||
}
|
||||
|
||||
@@ -51,6 +59,10 @@ 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)
|
||||
}
|
||||
|
||||
@@ -64,9 +76,9 @@ class CustomerController ( private val customerRepository: CustomerRepository )
|
||||
@GetMapping("/customers/trainees/{id}")
|
||||
fun getCustomerListByTrainerId(@PathVariable(value = "id") trainerId: Long, @RequestHeader headers: HttpHeaders):
|
||||
ResponseEntity<List<Customer>> {
|
||||
val list = customerRepository.findByTrainerId(trainerId)
|
||||
val customerList: List<Customer> = customerRepository.findByTrainerId(trainerId)
|
||||
|
||||
return if (list.isEmpty()) ResponseEntity.notFound().build() else ResponseEntity.ok().body(list)
|
||||
return if (customerList.isEmpty()) ResponseEntity.notFound().build() else ResponseEntity.ok().body(customerList)
|
||||
}
|
||||
|
||||
@Secured
|
||||
@@ -101,8 +113,8 @@ class CustomerController ( private val customerRepository: CustomerRepository )
|
||||
birthYear = newCustomer.birthYear,
|
||||
fitnessLevel = newCustomer.fitnessLevel,
|
||||
bodyType = newCustomer.bodyType,
|
||||
goal = newCustomer.goal,
|
||||
weight = newCustomer.weight
|
||||
goal = newCustomer.goal
|
||||
//weight = newCustomer.weight
|
||||
)
|
||||
} else {
|
||||
updatedCustomer =
|
||||
@@ -114,8 +126,8 @@ class CustomerController ( private val customerRepository: CustomerRepository )
|
||||
birthYear = newCustomer.birthYear,
|
||||
fitnessLevel = newCustomer.fitnessLevel,
|
||||
bodyType = newCustomer.bodyType,
|
||||
goal = newCustomer.goal,
|
||||
weight = newCustomer.weight
|
||||
goal = newCustomer.goal
|
||||
//weight = newCustomer.weight
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
package com.aitrainer.api.model
|
||||
|
||||
import javax.persistence.Entity
|
||||
import javax.persistence.GeneratedValue
|
||||
import javax.persistence.GenerationType
|
||||
import javax.persistence.Id
|
||||
import org.hibernate.annotations.Fetch
|
||||
import org.hibernate.annotations.FetchMode
|
||||
import javax.persistence.*
|
||||
|
||||
@Entity
|
||||
data class Customer (
|
||||
@@ -21,7 +20,7 @@ data class Customer (
|
||||
var trainerId: Long = 0,
|
||||
var password: String? = "",
|
||||
var birthYear:Int = 0,
|
||||
var weight: Int = 0,
|
||||
// var weight: Int = 0,
|
||||
var goal: String? = null,
|
||||
var fitnessLevel: String = "beginner",
|
||||
var bodyType: String? = null,
|
||||
@@ -29,4 +28,8 @@ 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>()
|
||||
}
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.aitrainer.api.model
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore
|
||||
import org.hibernate.annotations.Fetch
|
||||
import org.hibernate.annotations.FetchMode
|
||||
import javax.persistence.*
|
||||
import javax.validation.constraints.NotNull
|
||||
|
||||
@Entity
|
||||
data class CustomerExerciseDevice (
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
val customerExerciseDeviceId: Long = 0,
|
||||
@get: NotNull var exerciseDeviceId: 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
|
||||
}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package com.aitrainer.api.model
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore
|
||||
import org.hibernate.annotations.Fetch
|
||||
import org.hibernate.annotations.FetchMode
|
||||
import javax.persistence.*
|
||||
@@ -10,7 +9,7 @@ import javax.validation.constraints.NotBlank
|
||||
data class ExerciseDevice (
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
val deviceId: Long = 0,
|
||||
val exerciseDeviceId: Long = 0,
|
||||
|
||||
@get: NotBlank var name: String = "",
|
||||
@get: NotBlank var description: String = "",
|
||||
|
||||
@@ -16,7 +16,7 @@ data class ExerciseDeviceTranslation (
|
||||
|
||||
) {
|
||||
@ManyToOne(fetch = FetchType.EAGER, optional = false)
|
||||
@JoinColumn(name = "deviceId", nullable = false)
|
||||
@JoinColumn(name = "exerciseDeviceId", nullable = false)
|
||||
@JsonIgnore
|
||||
val exerciseDevice: ExerciseDevice? = null
|
||||
|
||||
|
||||
@@ -32,8 +32,4 @@ data class ExerciseType(
|
||||
@Fetch(value = FetchMode.SUBSELECT)
|
||||
val translations: List<ExerciseTypeTranslation> = mutableListOf<ExerciseTypeTranslation>()
|
||||
|
||||
@OneToMany(cascade = [(CascadeType.ALL)], fetch = FetchType.EAGER, mappedBy = "exerciseType")
|
||||
@Fetch(value = FetchMode.SUBSELECT)
|
||||
val exerciseDevices: List<ExerciseTypeDevice> = mutableListOf<ExerciseTypeDevice>()
|
||||
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package com.aitrainer.api.model
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore
|
||||
import org.springframework.lang.NonNull
|
||||
import javax.persistence.*
|
||||
import javax.validation.constraints.NotBlank
|
||||
|
||||
@@ -11,7 +10,6 @@ data class ExerciseTypeTranslation (
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
val translationId: Long = 0,
|
||||
|
||||
//@get: NonNull var exerciseTypeId: Integer?,
|
||||
@get: NotBlank var languageCode: String?,
|
||||
@get: NotBlank var name: String = "",
|
||||
@get: NotBlank var description: String = ""
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.aitrainer.api.repository
|
||||
|
||||
import com.aitrainer.api.model.CustomerExerciseDevice
|
||||
import org.springframework.data.jpa.repository.JpaRepository
|
||||
import org.springframework.data.jpa.repository.Query
|
||||
import org.springframework.stereotype.Repository
|
||||
|
||||
@Repository
|
||||
interface CustomerExerciseDeviceRepository: JpaRepository<CustomerExerciseDevice, Long> {
|
||||
@Query(" FROM CustomerExerciseDevice " +
|
||||
" WHERE customer.customerId = :customerId"
|
||||
)
|
||||
fun findByCustomerId(customerId: Long): List<CustomerExerciseDevice>
|
||||
}
|
||||
@@ -2,6 +2,7 @@ package com.aitrainer.api.repository
|
||||
|
||||
import com.aitrainer.api.model.Customer
|
||||
import org.springframework.data.jpa.repository.JpaRepository
|
||||
import org.springframework.data.jpa.repository.Query
|
||||
import org.springframework.stereotype.Repository
|
||||
|
||||
@Repository
|
||||
|
||||
@@ -8,7 +8,7 @@ import org.springframework.stereotype.Repository
|
||||
@Repository
|
||||
interface ExerciseDeviceRepository: JpaRepository<ExerciseDevice, Long> {
|
||||
@Query("FROM ExerciseDevice as e " +
|
||||
"LEFT JOIN ExerciseDeviceTranslation as t ON e.deviceId = t.exerciseDevice AND t.languageCode = 'hu' " +
|
||||
"ORDER BY e.deviceId ")
|
||||
"LEFT JOIN ExerciseDeviceTranslation as t ON e.exerciseDeviceId = t.exerciseDevice AND t.languageCode = 'hu' " +
|
||||
"ORDER BY e.exerciseDeviceId ")
|
||||
fun getExerciseDevices(): List<ExerciseDevice>
|
||||
}
|
||||
@@ -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.14
|
||||
application.version=1.0.15
|
||||
|
||||
jwt.secret=aitrainer
|
||||
@@ -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.14
|
||||
application.version=1.0.15
|
||||
|
||||
jwt.secret=aitrainer
|
||||
@@ -3,6 +3,7 @@ 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
|
||||
@@ -30,6 +31,10 @@ class CustomerTests {
|
||||
|
||||
@Autowired
|
||||
private lateinit var customerRepository: CustomerRepository
|
||||
|
||||
@Autowired
|
||||
private lateinit var customerExerciseDeviceRepository: CustomerExerciseDeviceRepository
|
||||
|
||||
private var insertedId: Long? = null
|
||||
|
||||
@Test
|
||||
@@ -37,6 +42,17 @@ class CustomerTests {
|
||||
val id: Long = 7
|
||||
val customer: Customer = customerRepository.findById( id ).orElse(null)
|
||||
assertEquals( customer.name, "Átlag 18 éves fiú")
|
||||
|
||||
val id2: Long = 90
|
||||
val controller = CustomerController(customerRepository, customerExerciseDeviceRepository)
|
||||
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
|
||||
@@ -45,6 +61,7 @@ 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
|
||||
@@ -62,7 +79,7 @@ class CustomerTests {
|
||||
}
|
||||
|
||||
fun testUpdate( customerId: Long ) {
|
||||
val id: Long? = customerId
|
||||
val id: Long = customerId
|
||||
assertNotNull(id)
|
||||
|
||||
val updatedCustomer: Customer = customerRepository.findById( id ).orElse(null)
|
||||
@@ -114,15 +131,15 @@ class CustomerTests {
|
||||
customer.admin = 1
|
||||
customer.sex = "m"
|
||||
customer.fitnessLevel = "intermediate"
|
||||
customer.weight = 79
|
||||
//customer.weight = 79
|
||||
customer.birthYear = 1972
|
||||
|
||||
val customerController = CustomerController(customerRepository)
|
||||
val customerController = CustomerController(customerRepository, customerExerciseDeviceRepository)
|
||||
var response: ResponseEntity<*> = customerController.updateCustomerById(id, customer, HttpHeaders.readOnlyHttpHeaders(HttpHeaders.EMPTY) )
|
||||
print ("body " + response.body)
|
||||
var newCustomer: Customer? = response.body as Customer
|
||||
assertEquals(response.statusCode, HttpStatus.OK)
|
||||
assertEquals(newCustomer?.weight, 79)
|
||||
//assertEquals(newCustomer?.weight, 79)
|
||||
assertEquals(newCustomer?.password, "123456789")
|
||||
assertEquals(newCustomer?.firstname, "Tib")
|
||||
|
||||
@@ -157,21 +174,19 @@ class CustomerTests {
|
||||
password = user.password
|
||||
firebaseUid = user.firebaseUid
|
||||
}
|
||||
val customerController = CustomerController(customerRepository)
|
||||
val customerController = CustomerController(customerRepository,customerExerciseDeviceRepository)
|
||||
customerController.serviceBeans = serviceBean
|
||||
var response: ResponseEntity<*> = customerController.registration(json)
|
||||
print("body " + response.body)
|
||||
val newCustomer: Customer? = response.body as Customer
|
||||
val newCustomer: Customer = response.body as Customer
|
||||
assertEquals(response.statusCode, HttpStatus.OK)
|
||||
assertEquals(newCustomer?.firebaseUid, "3Firebase8Uid")
|
||||
assertEquals(newCustomer.firebaseUid, "3Firebase8Uid")
|
||||
|
||||
val json2 = "{\"username\":\"bosi2@example.com\",\"password\":\"934345\",\"firebaseUid\":\"3Firebase8Uid\"}"
|
||||
response = customerController.registration(json2)
|
||||
assertEquals(response.statusCode, HttpStatus.BAD_REQUEST)
|
||||
|
||||
if ( newCustomer != null) {
|
||||
customerRepository.delete(newCustomer)
|
||||
}
|
||||
customerRepository.delete(newCustomer)
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -182,11 +197,11 @@ class CustomerTests {
|
||||
|
||||
insertedId = savedCustomer.customerId
|
||||
|
||||
val customerController = CustomerController(customerRepository)
|
||||
val customerController = CustomerController(customerRepository,customerExerciseDeviceRepository)
|
||||
val response: ResponseEntity<*> = customerController.updateCustomerFirebaseUidById(insertedId!!, "3FirebusaeId4")
|
||||
val newCustomer2: Customer? = response.body as Customer
|
||||
val newCustomer2: Customer = response.body as Customer
|
||||
assertEquals(response.statusCode, HttpStatus.OK)
|
||||
assertEquals(newCustomer2!!.firebaseUid, "3FirebusaeId4")
|
||||
assertEquals(newCustomer2.firebaseUid, "3FirebusaeId4")
|
||||
assertEquals(newCustomer2.firstname, "Tibor")
|
||||
assertEquals(newCustomer2.name, "Bossanyi2")
|
||||
assertEquals(newCustomer2.age, 48)
|
||||
@@ -197,11 +212,11 @@ class CustomerTests {
|
||||
@Test
|
||||
fun testGetCustomerByFirebaseUid() {
|
||||
val uid = "3FirebaseU1d"
|
||||
val customerController = CustomerController(customerRepository)
|
||||
val customerController = CustomerController(customerRepository,customerExerciseDeviceRepository)
|
||||
val response: ResponseEntity<*> = customerController.getCustomerByFirebaseUid(uid)
|
||||
assertEquals(response.statusCode, HttpStatus.OK)
|
||||
val newCustomer: Customer? = response.body as Customer
|
||||
assertEquals(newCustomer!!.name, "Bos")
|
||||
val newCustomer: Customer = response.body as Customer
|
||||
assertEquals(newCustomer.name, "Bos")
|
||||
assertEquals(newCustomer.email, "sw2@andio.biz")
|
||||
|
||||
}
|
||||
@@ -209,13 +224,16 @@ class CustomerTests {
|
||||
@Test
|
||||
fun testGetCustomerByEmail() {
|
||||
val email = "sw2@andio.biz"
|
||||
val customerController = CustomerController(customerRepository)
|
||||
val customerController = CustomerController(customerRepository,customerExerciseDeviceRepository)
|
||||
val response: ResponseEntity<*> = customerController.getCustomerByEmail(email)
|
||||
assertEquals(response.statusCode, HttpStatus.OK)
|
||||
val newCustomer: Customer? = response.body as Customer
|
||||
assertEquals(newCustomer!!.name, "Bos")
|
||||
|
||||
val newCustomer: Customer = response.body as Customer
|
||||
|
||||
assertEquals(newCustomer.name, "Bos")
|
||||
assertEquals(newCustomer.email, "sw2@andio.biz")
|
||||
assertEquals(newCustomer.firebaseUid, "3FirebaseU1d")
|
||||
assertEquals(newCustomer.exerciseDevices.size, 0)
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -18,7 +18,7 @@ class ExerciseDeviceTest {
|
||||
|
||||
|
||||
@Test
|
||||
fun testGetProperties() {
|
||||
fun testGetExerciseDevices() {
|
||||
val controller = ExerciseDeviceController(exerciseDeviceRepository )
|
||||
val response = controller.getDevicesWithTranslation()
|
||||
|
||||
|
||||
@@ -73,7 +73,6 @@ class ExerciseTypeTest {
|
||||
assertEquals(exerciseType.name, "Chest Press")
|
||||
assertEquals(exerciseType.images[0].url, "images/2.2.1.1.chestpress.png")
|
||||
assertEquals(responseEntity.body!![2].translations[0].name, "Tricepsz")
|
||||
assertEquals(exerciseType.exerciseDevices[0].exerciseDeviceId, 1)
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user