API 1.0.37+2 SSL, password encryption, TrainingPlanTranslation

This commit is contained in:
Bossanyi Tibor
2021-05-12 20:46:10 +02:00
parent a38c244d79
commit 7d78aa38fb
20 changed files with 540 additions and 17 deletions
@@ -1,12 +1,33 @@
package com.aitrainer.api
import org.jasypt.encryption.StringEncryptor
import org.jasypt.encryption.pbe.PooledPBEStringEncryptor
import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig
import org.slf4j.LoggerFactory
import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.builder.SpringApplicationBuilder
import org.springframework.context.annotation.Bean
@SpringBootApplication
class ApiApplication
class ApiApplication {
@Bean(name = ["jasyptStringEncryptor"])
fun stringEncryptor(): StringEncryptor? {
val encryptor = PooledPBEStringEncryptor()
val config = SimpleStringPBEConfig()
config.password = "Tibor"
config.algorithm = "PBEWithMD5AndDES"
config.setKeyObtentionIterations("1000")
config.setPoolSize("1")
config.providerName = "SunJCE"
config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator")
config.stringOutputType = "base64"
encryptor.setConfig(config)
return encryptor
}
}
private val logger = LoggerFactory.getLogger(ApiApplication::class.simpleName)
@Override
@@ -16,6 +37,10 @@ class ApiApplication
fun main(args: Array<String>) {
logger.info(" ---- Start aitrainer API")
SpringApplication.run(ApiApplication::class.java, *args)
val app = SpringApplication(ApiApplication::class.java)
app.setAdditionalProfiles("ssl")
app.run(*args)
}
@@ -18,7 +18,9 @@ class CustomerPackageController( private val customerRepository: CustomerReposit
private val purchaseRepository: PurchaseRepository,
private val customerPropertyRepository: CustomerPropertyRepository,
private val exerciseResultRepository: ExerciseResultRepository,
private val customerActivityRepository: CustomerActivityRepository) {
private val customerActivityRepository: CustomerActivityRepository,
private val customerTrainingPlanRepository: CustomerTrainingPlanRepository
) {
@GetMapping("/app_customer_package/{id}")
fun getCustomerPackageData(@PathVariable(value = "id") customerId: Long): ResponseEntity<String> {
@@ -60,6 +62,9 @@ class CustomerPackageController( private val customerRepository: CustomerReposit
val listActivityResult = customerActivityRepository.findByCustomerId(customerId)
val listActivityJson = gson.toJson(listActivityResult)
val listTrainingPlan = customerTrainingPlanRepository.findAllByCustomerId(customerId)
val listTrainingPlanJson = gson.toJson(listTrainingPlan)
val packageJson: String =
getClassRecord(Customer::class.simpleName, customerJson) +
"|||" + getClassRecord(CustomerExerciseDevice::class.simpleName, listCustomerExerciseDeviceJson) +
@@ -69,7 +74,8 @@ class CustomerPackageController( private val customerRepository: CustomerReposit
"|||" + getClassRecord(CustomerProperty::class.simpleName+"All", listCustomerPropertyAllJson) +
"|||" + getClassRecord(CustomerProperty::class.simpleName, listCustomerPropertyJson) +
"|||" + getClassRecord(ExerciseResult::class.simpleName, listExerciseResultJson+
"|||" + getClassRecord(CustomerActivity::class.simpleName, listActivityJson))
"|||" + getClassRecord(CustomerActivity::class.simpleName, listActivityJson) +
"|||" + getClassRecord(CustomerTrainingPlan::class.simpleName, listTrainingPlanJson))
return if (packageJson.isEmpty()) ResponseEntity.notFound().build() else
ResponseEntity.ok().body(packageJson)
@@ -0,0 +1,38 @@
package com.aitrainer.api.controller
import com.aitrainer.api.model.CustomerProperty
import com.aitrainer.api.model.CustomerTrainingPlan
import com.aitrainer.api.repository.CustomerPropertyRepository
import com.aitrainer.api.repository.CustomerTrainingPlanRepository
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
@RestController
@RequestMapping("/api")
class CustomerTrainingPlanController(private val customerTrainingPlanRepository: CustomerTrainingPlanRepository) {
private val logger = LoggerFactory.getLogger(javaClass)
@PostMapping("/customer_training_plan")
fun createNewCustomerTrainingPlan(@Valid @RequestBody customerTrainingPlan: CustomerTrainingPlan): ResponseEntity<CustomerTrainingPlan> {
logger.info("Create customer property: $customerTrainingPlan")
return ResponseEntity.ok().body(customerTrainingPlanRepository.save(customerTrainingPlan))
}
@Secured
@PostMapping("customer_training_plan/update/{id}")
fun updateCustomerProperty(@PathVariable(value = "id") trainingPlanId: Long,
@Valid @RequestBody customerTrainingPlanToUpdate: CustomerTrainingPlan): ResponseEntity<CustomerTrainingPlan> {
val customerTrainingPlan = customerTrainingPlanRepository.findById(trainingPlanId).orElse(null)
?: return ResponseEntity.notFound().build()
val updatedCustomerTrainingPlan = customerTrainingPlan.copy(
trainingPlanId = customerTrainingPlanToUpdate.trainingPlanId,
customerId = customerTrainingPlanToUpdate.customerId,
dateAdd = customerTrainingPlanToUpdate.dateAdd
)
return ResponseEntity.ok().body(customerTrainingPlanRepository.save(updatedCustomerTrainingPlan))
}
}
@@ -0,0 +1,16 @@
package com.aitrainer.api.model
import com.google.gson.annotations.Expose
import org.springframework.lang.NonNull
import javax.persistence.Entity
import javax.persistence.GeneratedValue
import javax.persistence.GenerationType
import javax.persistence.Id
@Entity
data class CustomerTrainingPlan (
@Expose @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @get: NonNull var customerTrainingPlanId: Long = 0,
@Expose @get: NonNull var customerId: Long = 0,
@Expose @get: NonNull var trainingPlanId: Long = 0,
@Expose @get: NonNull var dateAdd: String? = null
)
@@ -11,10 +11,15 @@ import javax.validation.constraints.NotBlank
data class TrainingPlan (
@Expose @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @get: NonNull var trainingPlanId: Long = 0,
@Expose @get: NotBlank var name: String = "",
@Expose var description: String?,
@Expose @get: NotBlank var type: String = "",
) {
@OneToMany(cascade = [(CascadeType.ALL)], fetch = FetchType.EAGER, mappedBy = "trainingPlan")
@Fetch(value = FetchMode.SUBSELECT)
@Expose val details: List<TrainingPlanDetail> = mutableListOf<TrainingPlanDetail>().toList()
@OneToMany(cascade = [(CascadeType.ALL)], fetch = FetchType.EAGER, mappedBy = "trainingPlan")
@Fetch(value = FetchMode.SUBSELECT)
@Expose val details: List<TrainingPlanDetail> = mutableListOf<TrainingPlanDetail>().toList()
@OneToMany(fetch = FetchType.EAGER, mappedBy = "trainingPlan")
@Fetch(value = FetchMode.SUBSELECT)
@Expose val translations: List<TrainingPlanTranslation> = mutableListOf<TrainingPlanTranslation>()
}
@@ -11,11 +11,11 @@ data class TrainingPlanDetail (
@Expose @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @get: NonNull var trainingPlanDetailId: Long = 0,
@Expose @get: NotBlank var exerciseTypeId: Int = 0,
@Expose @get: NotBlank var sort: Int = 0,
@Expose @get: NotBlank var set: Int = 0,
@Expose @get: NotBlank var repeats: Int = 0,
@Expose @get: NotBlank var weight: Double = 0.0,
@Expose @get: NotBlank var restingTime: Int = 0,
@Expose @get: NotBlank var parallel: Boolean = false,
@Expose var set: Int = 0,
@Expose var repeats: Int? = 0,
@Expose var weight: Double? = 0.0,
@Expose var restingTime: Int? = 0,
@Expose var parallel: Boolean? = false,
@Expose var day: String? = null,
) {
@@ -24,7 +24,4 @@ data class TrainingPlanDetail (
@JoinColumn(name = "trainingPlanId", nullable = false)
@JsonIgnore
val trainingPlan: TrainingPlan? = null
}
@@ -0,0 +1,23 @@
package com.aitrainer.api.model
import com.fasterxml.jackson.annotation.JsonIgnore
import com.google.gson.annotations.Expose
import javax.persistence.*
import javax.validation.constraints.NotBlank
@Entity
data class TrainingPlanTranslation (
@Expose @Id @GeneratedValue(strategy = GenerationType.IDENTITY) val translationId: Long = 0,
@Expose @get: NotBlank var languageCode: String?,
@Expose @get: NotBlank var nameTranslation: String = "",
@Expose @get: NotBlank var descriptionTranslation: String = "",
) {
@ManyToOne(fetch = FetchType.EAGER, optional = false)
@JoinColumn(name = "trainingPlanId", nullable = false)
@JsonIgnore
val trainingPlan: TrainingPlan? = null
}
@@ -0,0 +1,10 @@
package com.aitrainer.api.repository
import com.aitrainer.api.model.CustomerTrainingPlan
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.stereotype.Repository
@Repository
interface CustomerTrainingPlanRepository: JpaRepository<CustomerTrainingPlan, Long> {
fun findAllByCustomerId(customerId: Long): List<CustomerTrainingPlan>
}
@@ -0,0 +1,9 @@
package com.aitrainer.api.service
import org.springframework.context.annotation.Configuration
import org.springframework.context.annotation.PropertySource
@Configuration
@PropertySource("classpath:application.ssl.properties")
class AppConfigForJasyptStarter {
}
@@ -0,0 +1,24 @@
package com.aitrainer.api.service
import org.springframework.beans.factory.annotation.Value
import org.springframework.core.env.Environment
import org.springframework.stereotype.Service
@Service
class PropertyServiceForJasyptStarter {
@Value("\${trust.store.password}")
val storePassword: String? = null
@Value("\${server.ssl.key-store-password}")
val keyStorePassword: String? = null
fun getPasswordUsingEnvironment(environment: Environment): String? {
return environment.getProperty("trust.store.password")
}
fun getKeyStorePasswordUsingEnvironment(environment: Environment): String? {
return environment.getProperty("server.ssl.key-store-password")
}
}