API 1.1 WT Club changes

This commit is contained in:
Tibor Bossanyi
2023-02-04 18:41:55 +01:00
parent b17ea93836
commit 564e749bc2
100 changed files with 1085 additions and 230 deletions
@@ -1,18 +1,24 @@
package com.aitrainer.api.controller
import com.aitrainer.api.model.Customer
import com.aitrainer.api.model.User
import com.aitrainer.api.model.*
import com.aitrainer.api.service.ServiceBeans
import com.aitrainer.api.repository.CustomerRepository
import com.aitrainer.api.service.Email
import com.aitrainer.api.service.EmailTemplateService
import com.aitrainer.api.service.Firebase
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
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.io.ByteArrayOutputStream
import java.nio.charset.StandardCharsets.UTF_8
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
import java.util.*
import java.util.zip.GZIPInputStream
import java.util.zip.GZIPOutputStream
import javax.validation.Valid
@@ -24,6 +30,9 @@ class CustomerController ( private val customerRepository: CustomerRepository) {
@Autowired
var serviceBeans: ServiceBeans? = null
@Autowired
private var emailTemplateService: EmailTemplateService? = null
@Secured
@GetMapping("/customers")
fun getAllCustomers(@RequestHeader headers: HttpHeaders): List<Customer> =
@@ -147,6 +156,98 @@ class CustomerController ( private val customerRepository: CustomerRepository) {
return ResponseEntity.ok().body(customerRepository.save(updatedCustomer))
}
@PostMapping("/club_registration")
fun clubRegistration(@Valid @RequestBody json: String): ResponseEntity<*> {
fun gzip(content: String): String {
val bos = ByteArrayOutputStream()
GZIPOutputStream(bos).bufferedWriter(UTF_8).use { it.write(content) }
return bos.toByteArray().toString()
}
fun unzip(content: ByteArray): String =
GZIPInputStream(content.inputStream()).bufferedReader(UTF_8).use { it.readText() }
val newUser: ClubUser = ClubUser().fromJson(json)
if ( newUser.email.isEmpty()) {
return ResponseEntity.badRequest().body("No Email")
}
val existingCustomer: Customer? = customerRepository.findByEmailAndActive(newUser.email, "Y")
if (existingCustomer != null ) {
return ResponseEntity.badRequest().body("Customer exists")
}
val current = LocalDateTime.now()
val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS")
val nowFormatted = current.format(formatter)
val stringCharacters = ('0'..'z').toList().toTypedArray()
val genPassword = (1..10).map { stringCharacters.random() }.joinToString("")
val firebase = Firebase()
val signupResponse = firebase.signUp(newUser.email, genPassword)
?: return ResponseEntity.badRequest().body("Firebase exception ${firebase.error}")
val saving_customer = Customer()
if ( serviceBeans == null ) {
serviceBeans = ServiceBeans()
}
with (saving_customer) {
email = newUser.email
password = serviceBeans!!.passwordEncoder().encode(genPassword)
idToken = signupResponse.idToken
firebaseUid = signupResponse.localId
dateAdd = nowFormatted
firstname = newUser.firstname
name = ""
goal = newUser.goal
fitnessLevel = newUser.fitnessLevel
}
val newCustomer = customerRepository.save(saving_customer)
if ( newUser.weight != 0.0 ) {
val property = CustomerPropertyProperty()
with (property) {
propertyId = 1
propertyValue = newUser.weight
dateAdd= nowFormatted
goal = false
customer = newCustomer
}
newCustomer.properties.add(property)
}
if ( newUser.height != 0.0 ) {
val property = CustomerPropertyProperty()
with (property) {
propertyId = 2
propertyValue = newUser.height
dateAdd= nowFormatted
goal = false
customer = newCustomer
}
newCustomer.properties.add(property)
}
customerRepository.save(newCustomer)
// create email link
val activationLink = "https://club.workouttest.com/welcome/id=${signupResponse.idToken}"
if ( emailTemplateService == null ) {
emailTemplateService = EmailTemplateService()
}
val html = emailTemplateService!!.getEmailBody(newUser.firstname, activationLink)
val subject = emailTemplateService!!.getSubject()
// send email
val email = Email()
email.send(newUser.email, html, subject)
return ResponseEntity.ok().body(newCustomer)
}
@PostMapping("/registration")
fun registration(@Valid @RequestBody json: String): ResponseEntity<*> {
@@ -180,9 +281,10 @@ class CustomerController ( private val customerRepository: CustomerRepository) {
} else {
ResponseEntity.badRequest().body("Customer exists")
}
}
@PostMapping("/login")
fun login(@Valid @RequestBody json: String): ResponseEntity<*> {
val customer = Customer()
@@ -0,0 +1,47 @@
package com.aitrainer.api.controller
import com.aitrainer.api.model.CustomerMembership
import com.aitrainer.api.repository.CustomerMembershipRepository
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 CustomerMembershipController(private val customerMembershipRepository: CustomerMembershipRepository) {
private val logger = LoggerFactory.getLogger(javaClass)
@PostMapping("/customer_membership")
fun createNewCustomerMembership(@Valid @RequestBody customerMembership: CustomerMembership): ResponseEntity<CustomerMembership> {
logger.info("Create customer membership: $customerMembership")
return ResponseEntity.ok().body(customerMembershipRepository.save(customerMembership))
}
@Secured
@PostMapping("customer_membership/update/{id}")
fun updateCustomerMembership(@PathVariable(value = "id") membershipId: Long,
@Valid @RequestBody membershipToUpdate: CustomerMembership): ResponseEntity<CustomerMembership> {
val customerMembership = customerMembershipRepository.findById(membershipId).orElse(null)
?: return ResponseEntity.notFound().build()
val updatedCustomerMembership = customerMembership.copy(
trainingPlanId = membershipToUpdate.trainingPlanId,
days = membershipToUpdate.days,
)
return ResponseEntity.ok().body(customerMembershipRepository.save(updatedCustomerMembership))
}
@GetMapping("/customer_membership/{customer_id}")
fun getAllByCustomerId(@PathVariable(value = "customer_id") customerId: Long): ResponseEntity<List<CustomerMembership>> {
val membershipList: List<CustomerMembership> = customerMembershipRepository.findAllByCustomerId(customerId)
logger.info("Get all customer_membership by by customerId")
return if(membershipList.isNotEmpty())
ResponseEntity.ok().body(membershipList) else
ResponseEntity.notFound().build()
}
}
@@ -1,6 +1,7 @@
package com.aitrainer.api.controller
import com.aitrainer.api.model.*
import com.aitrainer.api.model.CustomerMembership
import com.aitrainer.api.repository.*
import com.google.gson.GsonBuilder
import org.springframework.http.ResponseEntity
@@ -19,8 +20,48 @@ class CustomerPackageController( private val customerRepository: CustomerReposit
private val exerciseResultRepository: ExerciseResultRepository,
private val customerActivityRepository: CustomerActivityRepository,
private val customerTrainingPlanRepository: CustomerTrainingPlanRepository,
private val customerMembership: CustomerMembershipRepository,
) {
@GetMapping("/club_customer_package/{id}")
fun getCustomerClubPackageData(@PathVariable(value = "id") customerId: Long): ResponseEntity<String> {
if (customerId <= 0) {
return ResponseEntity.notFound().build()
}
val gson = GsonBuilder()
.excludeFieldsWithoutExposeAnnotation()
.setPrettyPrinting()
.create()
val customer: Customer = customerRepository.findByCustomerIdAndActive(customerId, "Y")
?: return ResponseEntity.notFound().build()
val customerJson: String = gson.toJson(customer)
val listCustomerPropertyAll = customerPropertyRepository.findAllByCustomerId(customerId)
val listCustomerPropertyAllJson = gson.toJson(listCustomerPropertyAll)
val listCustomerProperty = customerPropertyRepository.findLastPropertiesByCustomerId(customerId)
val listCustomerPropertyJson = gson.toJson(listCustomerProperty)
val listTrainingPlan = customerTrainingPlanRepository.findAllByCustomerId(customerId)
val listTrainingPlanJson = gson.toJson(listTrainingPlan)
val listMembership = customerMembership.findAllByCustomerId(customerId)
val listMembershipJson = gson.toJson(listMembership)
val packageJson: String =
getClassRecord(Customer::class.simpleName, customerJson) +
"|||" + getClassRecord(CustomerProperty::class.simpleName+"All", listCustomerPropertyAllJson) +
"|||" + getClassRecord(CustomerProperty::class.simpleName, listCustomerPropertyJson) +
"|||" + getClassRecord(CustomerTrainingPlan::class.simpleName, listTrainingPlanJson +
"|||" + getClassRecord(CustomerMembership::class.simpleName, listMembershipJson)
)
return if (packageJson.isEmpty()) ResponseEntity.notFound().build() else
ResponseEntity.ok().body(packageJson)
}
@GetMapping("/app_customer_package/{id}")
fun getCustomerPackageData(@PathVariable(value = "id") customerId: Long): ResponseEntity<String> {
@@ -19,6 +19,12 @@ class CustomerPropertyController(private val customerPropertyRepository: Custome
return ResponseEntity.ok().body(customerPropertyRepository.save(customerProperty))
}
@PostMapping("/customer_goal")
fun createCustomerGoal(@Valid @RequestBody customerProperty: CustomerProperty): ResponseEntity<CustomerProperty> {
logger.info("Create customer goal: $customerProperty")
return ResponseEntity.ok().body(customerPropertyRepository.save(customerProperty))
}
@Secured
@PostMapping("customer_property/update/{id}")
fun updateCustomerProperty(@PathVariable(value = "id") propertyId: Long,
@@ -2,7 +2,6 @@ package com.aitrainer.api.controller
import com.aitrainer.api.model.*
import com.aitrainer.api.repository.*
import org.slf4j.LoggerFactory
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
@@ -29,9 +28,48 @@ class PackageController(private val exerciseAbilityRepository: ExerciseAbilityRe
private val splitTestsRepository: SplitTestsRepository,
private val trainingPlanDayRepository: TrainingPlanDayRepository,
private val appTextRepository: AppTextRepository,
private val trainingProgramRepository: TrainingProgramRepository
private val trainingProgramRepository: TrainingProgramRepository,
private val membershipRepository: MembershipRepository
) {
private val logger = LoggerFactory.getLogger(javaClass)
@GetMapping("/club_package")
fun getClubPackageData(): ResponseEntity<String> {
val gson = GsonBuilder()
.excludeFieldsWithoutExposeAnnotation()
.setPrettyPrinting()
.create()
val listProperty:List<Property> = propertyRepository.getProperties()
val listPropertyJson: String = gson.toJson(listProperty)
val listExerciseType = exerciseTypeRepository.findAll()
val listExerciseTypeJson = gson.toJson(listExerciseType)
val listTrainingPlan = trainingPlanRepository.findAll()
val listTrainingPlanJson = gson.toJson(listTrainingPlan)
val listTrainingPlanDay = trainingPlanDayRepository.findAll()
val listTrainingPlanDayJson = gson.toJson(listTrainingPlanDay)
val listTrainingProgram = trainingProgramRepository.findAll()
val listTrainingProgramJson = gson.toJson(listTrainingProgram)
val listMembership = membershipRepository.findAll()
val listMembershipJson = gson.toJson(listMembership)
val packageJson: String =
getClassRecord(Property::class.simpleName, listPropertyJson) +
"|||" + getClassRecord(ExerciseType::class.simpleName, listExerciseTypeJson) +
"|||" + getClassRecord(TrainingPlan::class.simpleName, listTrainingPlanJson) +
"|||" + getClassRecord(TrainingPlanDay::class.simpleName, listTrainingPlanDayJson) +
"|||" + getClassRecord(TrainingProgram::class.simpleName, listTrainingProgramJson) +
"|||" + getClassRecord(Membership::class.simpleName, listMembershipJson)
return if (packageJson.isEmpty()) ResponseEntity.notFound().build() else
ResponseEntity.ok().body(packageJson)
}
@GetMapping("/app_package")
fun getPackageData(): ResponseEntity<String> {