API 1.0.15b CustomerExerciseDevice standalone
This commit is contained in:
@@ -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>
|
||||
}
|
||||
Reference in New Issue
Block a user