API 1.0.15 CustomerExerciseDevice

This commit is contained in:
Bossanyi Tibor
2020-11-23 18:15:46 +01:00
parent af2f5be1bf
commit 3a70bcd10f
20 changed files with 171 additions and 61 deletions
@@ -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
+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.14
application.version=1.0.15
jwt.secret=aitrainer