From 59d09ed115f1604a45a20c5380b1282b893ae8e7 Mon Sep 17 00:00:00 2001 From: Bossanyi Tibor Date: Mon, 15 Feb 2021 18:04:22 +0100 Subject: [PATCH] API 1.0.25 update CustomerProperty --- build.gradle.kts | 2 +- data/db/install.sql | 4 +- data/db/update_1_0_25.sql | 10 +++++ .../controller/CustomerPropertyController.kt | 17 +++++++- .../resources/application-prod.properties | 2 +- src/main/resources/application.properties | 2 +- .../api/test/AppCustomerPackageTest.kt | 4 +- .../api/test/CustomerPropertyTest.kt | 39 +++++++++++++++++-- .../com/aitrainer/api/test/PropertyTest.kt | 2 +- 9 files changed, 69 insertions(+), 13 deletions(-) create mode 100644 data/db/update_1_0_25.sql diff --git a/build.gradle.kts b/build.gradle.kts index dae94ee..1c540fa 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -11,7 +11,7 @@ plugins { } group = "com.aitrainer" -version = "1.0.24" +version = "1.0.25" java.sourceCompatibility = JavaVersion.VERSION_1_8 repositories { diff --git a/data/db/install.sql b/data/db/install.sql index 895a03b..2a00794 100644 --- a/data/db/install.sql +++ b/data/db/install.sql @@ -266,7 +266,9 @@ CREATE TABLE IF NOT EXISTS `customer_property` ( `property_id` int(10) unsigned NOT NULL DEFAULT 0, `property_value` double unsigned NOT NULL DEFAULT 0, `date_add` datetime DEFAULT NULL, - PRIMARY KEY (`customer_property_id`) USING BTREE + PRIMARY KEY (`customer_property_id`) USING BTREE, + INDEX `property_id` (`property_id`) USING BTREE, + INDEX `customer_id` (`customer_id`) USING BTREE ) ENGINE=InnoDB AUTO_INCREMENT=65 DEFAULT CHARSET=utf8 COLLATE=utf8_hungarian_ci; -- Tábla adatainak mentése aitrainer2.customer_property: ~7 rows (hozzávetőleg) diff --git a/data/db/update_1_0_25.sql b/data/db/update_1_0_25.sql new file mode 100644 index 0000000..1bb5f10 --- /dev/null +++ b/data/db/update_1_0_25.sql @@ -0,0 +1,10 @@ +START TRANSACTION; +ALTER TABLE `customer_property` + ADD INDEX `property_id` (`property_id`), + ADD INDEX `customer_id` (`customer_id`); + + +UPDATE configuration set config_value = "1.0.25", date_change=CURRENT_DATE WHERE config_key = "db_version"; + +COMMIT; + diff --git a/src/main/kotlin/com/aitrainer/api/controller/CustomerPropertyController.kt b/src/main/kotlin/com/aitrainer/api/controller/CustomerPropertyController.kt index 040b218..21a6849 100644 --- a/src/main/kotlin/com/aitrainer/api/controller/CustomerPropertyController.kt +++ b/src/main/kotlin/com/aitrainer/api/controller/CustomerPropertyController.kt @@ -1,11 +1,10 @@ package com.aitrainer.api.controller import com.aitrainer.api.model.CustomerProperty -import com.aitrainer.api.model.ExercisePlanDetail -import com.aitrainer.api.model.Purchase import com.aitrainer.api.repository.CustomerPropertyRepository import org.slf4j.LoggerFactory import org.springframework.http.ResponseEntity +import org.springframework.security.access.annotation.Secured import org.springframework.web.bind.annotation.* import javax.validation.Valid @@ -20,6 +19,20 @@ class CustomerPropertyController(private val customerPropertyRepository: Custome return ResponseEntity.ok().body(customerPropertyRepository.save(customerProperty)) } + @Secured + @PostMapping("customer_property/update/{id}") + fun updateCustomerProperty(@PathVariable(value = "id") propertyId: Long, + @Valid @RequestBody propertyToUpdate: CustomerProperty): ResponseEntity { + val customerProperty = customerPropertyRepository.findById(propertyId).orElse(null) + ?: return ResponseEntity.notFound().build() + + val updatedCustomerProperty = customerProperty.copy( + propertyValue = propertyToUpdate.propertyValue, + dateAdd = propertyToUpdate.dateAdd + ) + return ResponseEntity.ok().body(customerPropertyRepository.save(updatedCustomerProperty)) + } + @GetMapping("/customer_property/{id}") fun getAllByCustomerId(@PathVariable(value = "id") customerId: Long): ResponseEntity> { diff --git a/src/main/resources/application-prod.properties b/src/main/resources/application-prod.properties index 6be0371..7f99ae2 100644 --- a/src/main/resources/application-prod.properties +++ b/src/main/resources/application-prod.properties @@ -17,6 +17,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.24 +application.version=1.0.25 jwt.secret=aitrainer \ No newline at end of file diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index e2cfadd..9e584f4 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -17,6 +17,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.24 +application.version=1.0.25 jwt.secret=aitrainer \ No newline at end of file diff --git a/src/test/kotlin/com/aitrainer/api/test/AppCustomerPackageTest.kt b/src/test/kotlin/com/aitrainer/api/test/AppCustomerPackageTest.kt index 50fccc6..e37ef6e 100644 --- a/src/test/kotlin/com/aitrainer/api/test/AppCustomerPackageTest.kt +++ b/src/test/kotlin/com/aitrainer/api/test/AppCustomerPackageTest.kt @@ -92,8 +92,8 @@ class AppCustomerPackageTest { val type = object : TypeToken?>() {}.type val propertyList: List = gson.fromJson(propertyJson, type) assertTrue(propertyList.isNotEmpty()) - assertEquals(propertyList[1].propertyId,4) - assertEquals(propertyList[1].propertyValue,37.0) + assertEquals(propertyList[2].propertyId,4) + assertEquals(propertyList[2].propertyValue,37.0) } else if (record[0] == CustomerProperty::class.simpleName+"All") { val propertyJson: String = record[1] val type = object : TypeToken?>() {}.type diff --git a/src/test/kotlin/com/aitrainer/api/test/CustomerPropertyTest.kt b/src/test/kotlin/com/aitrainer/api/test/CustomerPropertyTest.kt index 9c1e7df..537e2a1 100644 --- a/src/test/kotlin/com/aitrainer/api/test/CustomerPropertyTest.kt +++ b/src/test/kotlin/com/aitrainer/api/test/CustomerPropertyTest.kt @@ -8,6 +8,7 @@ import org.junit.jupiter.api.TestFactory import org.junit.jupiter.api.TestInstance import org.springframework.beans.factory.annotation.Autowired import org.springframework.boot.test.context.SpringBootTest +import org.springframework.http.HttpStatus import kotlin.test.assertEquals import kotlin.test.assertTrue @@ -28,7 +29,7 @@ class CustomerPropertyTest { assertTrue(properties is List) assertTrue(properties.isNotEmpty()) - assertEquals(properties.size, 5) + assertEquals(properties.size, 8) assertEquals(properties[0].propertyValue, 81.0) assertEquals(properties[1].propertyValue, 82.0) assertEquals(properties[3].propertyId, 4) @@ -42,9 +43,9 @@ class CustomerPropertyTest { val properties = response.body assertTrue(properties is List) assertTrue(properties.isNotEmpty()) - assertEquals(properties.size, 3) - assertEquals(properties[2].propertyValue, 79.0) - assertEquals(properties[0].propertyValue, 172.0) + assertEquals(properties.size, 6) + assertEquals(properties[2].propertyValue, 37.0) + assertEquals(properties[0].propertyValue, 81.0) } @Test @@ -67,4 +68,34 @@ class CustomerPropertyTest { customerPropertyRepository.delete(customerProperty) } + @Test + fun testUpdate() { + var customerProperty = CustomerProperty( + customerPropertyId = 91, + dateAdd = "2021-02-15", + propertyValue = 42.0 + ) + val controller = CustomerPropertyController(customerPropertyRepository) + + var response = controller.updateCustomerProperty(customerProperty.customerPropertyId, customerProperty) + + assertTrue(response.body is CustomerProperty) + val updated = response.body + assertEquals(updated!!.propertyValue, 42.0) + updated.propertyValue = 81.0 + controller.updateCustomerProperty(customerProperty.customerPropertyId, customerProperty) + + //notFound + customerProperty = CustomerProperty( + customerPropertyId = 1000, + dateAdd = "2021-02-15", + propertyValue = 83.0 + ) + response = controller.updateCustomerProperty(customerProperty.customerPropertyId, customerProperty) + assertEquals(response.statusCode, HttpStatus.NOT_FOUND) + + + + } + } \ No newline at end of file diff --git a/src/test/kotlin/com/aitrainer/api/test/PropertyTest.kt b/src/test/kotlin/com/aitrainer/api/test/PropertyTest.kt index 2938454..205d3ff 100644 --- a/src/test/kotlin/com/aitrainer/api/test/PropertyTest.kt +++ b/src/test/kotlin/com/aitrainer/api/test/PropertyTest.kt @@ -26,7 +26,7 @@ class PropertyTest { assertTrue(properties is List) assertTrue(properties.isNotEmpty()) - assertEquals(properties.size, 4) + assertEquals(properties.size, 7) assertEquals(properties[0].propertyName, "Weight") assertEquals(properties[0].translations[0].propertyName, "Tömeg") assertEquals(properties[1].propertyName, "Height")