Merge branch 'tibor' into 'master'

API 1.0.15 CustomerExerciseDevice

See merge request bossanyit/aitrainer_server!24
This commit is contained in:
Bossányi Tibor 2020-11-23 17:17:18 +00:00
commit 868bd97840
20 changed files with 171 additions and 61 deletions

View File

@ -11,7 +11,7 @@ plugins {
}
group = "com.aitrainer"
version = "1.0.14"
version = "1.0.15"
java.sourceCompatibility = JavaVersion.VERSION_1_8
repositories {

View File

@ -381,26 +381,27 @@ ENGINE=InnoDB
;
CREATE TABLE `exercise_device` (
`device_id` INT(11) NOT NULL AUTO_INCREMENT,
`exercise_device_id` INT(11) NOT NULL AUTO_INCREMENT,
`name` CHAR(50) NULL DEFAULT NULL COLLATE 'utf8_hungarian_ci',
`description` VARCHAR(200) NULL DEFAULT NULL COLLATE 'utf8_hungarian_ci',
`image_url` CHAR(100) NULL DEFAULT NULL COLLATE 'utf8_hungarian_ci',
PRIMARY KEY (`device_id`) USING BTREE
PRIMARY KEY (`exercise_device_id`) USING BTREE
)
COLLATE='utf8_hungarian_ci'
ENGINE=InnoDB
;
INSERT INTO `exercise_device` (`device_id`, `name`, `description`, `image_url`) VALUES (1, 'Weight', NULL, NULL);
INSERT INTO `exercise_device` (`device_id`, `name`, `description`, `image_url`) VALUES (2, 'Own Weight', NULL, NULL);
INSERT INTO `exercise_device` (`exercise_device_id`, `name`, `description`, `image_url`) VALUES (1, 'Weight', NULL, NULL);
INSERT INTO `exercise_device` (`exercise_device_id`, `name`, `description`, `image_url`) VALUES (2, 'Own Weight', NULL, NULL);
CREATE TABLE `exercise_device_translation` (
`translation_id` INT(11) NOT NULL AUTO_INCREMENT,
`device_id` INT(11) NULL DEFAULT NULL,
`exercise_device_id` INT(11) NULL DEFAULT NULL,
`language_code` CHAR(2) NULL DEFAULT NULL COLLATE 'utf8_hungarian_ci',
`name` CHAR(100) NULL DEFAULT NULL COLLATE 'utf8_hungarian_ci',
PRIMARY KEY (`translation_id`) USING BTREE
PRIMARY KEY (`translation_id`) USING BTREE,
INDEX `exercise_device_id` (`exercise_device_id`) USING BTREE
)
COLLATE='utf8_hungarian_ci'
ENGINE=InnoDB
@ -414,10 +415,10 @@ INSERT INTO `exercise_device_translation` (`translation_id`, `device_id`, `langu
CREATE TABLE `exercise_type_device` (
`exercise_type_device_id` INT(11) NOT NULL AUTO_INCREMENT,
`exercise_type_id` INT(11) NOT NULL DEFAULT '0',
`device_id` INT(11) NOT NULL DEFAULT '0',
`exercise_device_id` INT(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`exercise_type_device_id`) USING BTREE,
INDEX `exercise_type_id` (`exercise_type_id`) USING BTREE,
INDEX `exercise_device_id` (`device_id`) USING BTREE
INDEX `exercise_device_id` (`exercise_device_id`) USING BTREE
)
COLLATE='utf8_hungarian_ci'
ENGINE=InnoDB
@ -425,6 +426,17 @@ ENGINE=InnoDB
INSERT INTO `exercise_type_device` (`exercise_type_device_id`, `exercise_type_id`, `exercise_device_id`) VALUES (1, 37, 1);
CREATE TABLE `customer_exercise_device` (
`customer_exercise_device_id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`customer_id` INT(11) NOT NULL DEFAULT '0',
`exercise_device_id` INT(11) NOT NULL DEFAULT '0',
`date_add` DATETIME NULL DEFAULT NULL,
`favourite` TINYINT(4) NULL DEFAULT NULL,
PRIMARY KEY (`customer_exercise_device_id`) USING BTREE
)
COLLATE='utf8_hungarian_ci'
ENGINE=InnoDB
;
/*!40000 ALTER TABLE `exercise_type` ENABLE KEYS */;

View File

@ -35,7 +35,7 @@ CREATE TABLE `exercise_type_device` (
`exercise_device_id` INT(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`exercise_type_device_id`) USING BTREE,
INDEX `exercise_type_id` (`exercise_type_id`) USING BTREE,
INDEX `device_id` (`device_id`) USING BTREE
INDEX `exercise_device_id` (`exercise_device_id`) USING BTREE
)
COLLATE='utf8_hungarian_ci'
ENGINE=InnoDB

27
data/db/update_1_0_15.sql Normal file
View File

@ -0,0 +1,27 @@
CREATE TABLE `customer_exercise_device` (
`customer_exercise_device_id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`customer_id` INT(11) NOT NULL DEFAULT '0',
`exercise_device_id` INT(11) NOT NULL DEFAULT '0',
`date_add` DATETIME NULL DEFAULT NULL,
`favourite` TINYINT(4) NULL DEFAULT NULL,
PRIMARY KEY (`customer_exercise_device_id`) USING BTREE
)
COLLATE='utf8_hungarian_ci'
ENGINE=InnoDB
;
INSERT INTO `customer_exercise_device` (`customer_exercise_device_id`, `customer_id`, `exercise_device_id`, `date_add`, `favourite`) VALUES (1, 90, 1, NULL, NULL);
INSERT INTO `customer_exercise_device` (`customer_exercise_device_id`, `customer_id`, `exercise_device_id`, `date_add`, `favourite`) VALUES (2, 90, 2, NULL, NULL);
ALTER TABLE `exercise_device`
CHANGE COLUMN `device_id` `exercise_device_id` INT(11) NOT NULL AUTO_INCREMENT FIRST,
DROP PRIMARY KEY,
ADD PRIMARY KEY (`exercise_device_id`) USING BTREE;
ALTER TABLE `exercise_device_translation`
CHANGE COLUMN `device_id` `exercise_device_id` INT(11) NULL DEFAULT NULL AFTER `translation_id`;
ALTER TABLE `exercise_device_translation`
ADD INDEX `exercise_device_id` (`exercise_device_id`);
UPDATE configuration set config_value = "1.0.15", date_change=CURRENT_DATE WHERE config_key = "db_version";

View File

@ -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)
}
}

View File

@ -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
)
}

View File

@ -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>()
}

View File

@ -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
}

View File

@ -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 = "",

View File

@ -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

View File

@ -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>()
}

View File

@ -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 = ""

View File

@ -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>
}

View File

@ -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

View File

@ -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>
}

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

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

View File

@ -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)
}

View File

@ -18,7 +18,7 @@ class ExerciseDeviceTest {
@Test
fun testGetProperties() {
fun testGetExerciseDevices() {
val controller = ExerciseDeviceController(exerciseDeviceRepository )
val response = controller.getDevicesWithTranslation()

View File

@ -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)
}
}