API 1.1 WT Club, add customer_membership
This commit is contained in:
@@ -190,24 +190,26 @@ class CustomerController ( private val customerRepository: CustomerRepository) {
|
||||
val signupResponse = firebase.signUp(newUser.email, genPassword)
|
||||
?: return ResponseEntity.badRequest().body("Firebase exception ${firebase.error}")
|
||||
|
||||
val saving_customer = Customer()
|
||||
val savingCustomer = Customer()
|
||||
if ( serviceBeans == null ) {
|
||||
serviceBeans = ServiceBeans()
|
||||
}
|
||||
|
||||
with (saving_customer) {
|
||||
with (savingCustomer) {
|
||||
email = newUser.email
|
||||
password = serviceBeans!!.passwordEncoder().encode(genPassword)
|
||||
idToken = signupResponse.idToken
|
||||
firebaseRegToken = signupResponse.idToken
|
||||
firebaseUid = signupResponse.localId
|
||||
dateAdd = nowFormatted
|
||||
firstname = newUser.firstname
|
||||
name = ""
|
||||
goal = newUser.goal
|
||||
fitnessLevel = newUser.fitnessLevel
|
||||
birthYear = newUser.birthYear
|
||||
sex = newUser.sex
|
||||
}
|
||||
|
||||
val newCustomer = customerRepository.save(saving_customer)
|
||||
val newCustomer = customerRepository.save(savingCustomer)
|
||||
|
||||
if ( newUser.weight != 0.0 ) {
|
||||
val property = CustomerPropertyProperty()
|
||||
@@ -225,13 +227,24 @@ class CustomerController ( private val customerRepository: CustomerRepository) {
|
||||
with (property) {
|
||||
propertyId = 2
|
||||
propertyValue = newUser.height
|
||||
dateAdd= nowFormatted
|
||||
dateAdd = nowFormatted
|
||||
goal = false
|
||||
customer = newCustomer
|
||||
}
|
||||
newCustomer.properties.add(property)
|
||||
}
|
||||
|
||||
val newMembershipId = newUser.membershipId
|
||||
if ( newMembershipId != 0L) {
|
||||
val membership = CustomerMembership()
|
||||
with(membership) {
|
||||
customer = newCustomer
|
||||
membershipId = newMembershipId
|
||||
startDate = nowFormatted
|
||||
}
|
||||
newCustomer.memberships.add(membership)
|
||||
}
|
||||
|
||||
customerRepository.save(newCustomer)
|
||||
|
||||
// create email link
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.aitrainer.api.controller
|
||||
|
||||
import com.aitrainer.api.model.CustomerMembership
|
||||
import com.aitrainer.api.repository.CustomerMembershipRepository
|
||||
import com.aitrainer.api.repository.CustomerRepository
|
||||
import org.slf4j.LoggerFactory
|
||||
import org.springframework.http.ResponseEntity
|
||||
import org.springframework.security.access.annotation.Secured
|
||||
@@ -10,7 +11,7 @@ import javax.validation.Valid
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api")
|
||||
class CustomerMembershipController(private val customerMembershipRepository: CustomerMembershipRepository) {
|
||||
class CustomerMembershipController(private val customerMembershipRepository: CustomerMembershipRepository, private val customerRepository: CustomerRepository) {
|
||||
private val logger = LoggerFactory.getLogger(javaClass)
|
||||
|
||||
@PostMapping("/customer_membership")
|
||||
@@ -19,25 +20,12 @@ class CustomerMembershipController(private val customerMembershipRepository: Cus
|
||||
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 customer = customerRepository.findByCustomerIdAndActive(customerId, "Y")
|
||||
?: return ResponseEntity.notFound().build()
|
||||
|
||||
val membershipList: List<CustomerMembership> = customerMembershipRepository.findAllByCustomerId(customerId)
|
||||
val membershipList: List<CustomerMembership> = customerMembershipRepository.findAllByCustomer(customer)
|
||||
logger.info("Get all customer_membership by by customerId")
|
||||
|
||||
return if(membershipList.isNotEmpty())
|
||||
|
||||
@@ -47,7 +47,7 @@ class CustomerPackageController( private val customerRepository: CustomerReposit
|
||||
val listTrainingPlan = customerTrainingPlanRepository.findAllByCustomerId(customerId)
|
||||
val listTrainingPlanJson = gson.toJson(listTrainingPlan)
|
||||
|
||||
val listMembership = customerMembership.findAllByCustomerId(customerId)
|
||||
val listMembership = customerMembership.findAllByCustomer(customer)
|
||||
val listMembershipJson = gson.toJson(listMembership)
|
||||
|
||||
val packageJson: String =
|
||||
|
||||
@@ -7,10 +7,13 @@ import kotlinx.serialization.json.*
|
||||
data class ClubUser (
|
||||
var email: String = "",
|
||||
var firstname: String = "",
|
||||
var sex: String = "m",
|
||||
var birthYear: Int = 0,
|
||||
var weight: Double = 0.0,
|
||||
var height: Double = 0.0,
|
||||
var goal: String = "",
|
||||
var fitnessLevel: String = "",
|
||||
var membershipId: Long = 0,
|
||||
) {
|
||||
fun fromJson(json: String): ClubUser {
|
||||
return Json.decodeFromString(serializer(), json)
|
||||
|
||||
@@ -34,11 +34,14 @@ data class Customer (
|
||||
@Expose var lang: String? = null,
|
||||
@Expose var phone: String? = null,
|
||||
@Expose var lifeLong: Int? = null,
|
||||
@Expose var idToken: String? = null,
|
||||
|
||||
@Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Expose var customerId: Long = 0,
|
||||
) {
|
||||
@OneToMany(cascade = [(CascadeType.ALL)], fetch = FetchType.EAGER, mappedBy = "customer")
|
||||
@Fetch(value = FetchMode.SUBSELECT)
|
||||
@Expose val properties: MutableList<CustomerPropertyProperty> = mutableListOf()
|
||||
|
||||
@OneToMany(cascade = [(CascadeType.ALL)], fetch = FetchType.EAGER, mappedBy = "customer")
|
||||
@Fetch(value = FetchMode.SUBSELECT)
|
||||
@Expose val memberships: MutableList<CustomerMembership> = mutableListOf()
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.aitrainer.api.model
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore
|
||||
import com.google.gson.annotations.Expose
|
||||
import org.springframework.lang.NonNull
|
||||
import jakarta.persistence.*
|
||||
@@ -7,9 +8,10 @@ import jakarta.persistence.*
|
||||
@Entity
|
||||
data class CustomerMembership (
|
||||
@Expose @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @get: NonNull var id: Long = 0,
|
||||
@Expose @get: NonNull var customerId: Long = 0,
|
||||
@Expose @get: NonNull var membershipId: Long = 0,
|
||||
@Expose @get: NonNull var startDate: String? = null,
|
||||
@Expose @get: NonNull var trainingPlanId: Long = 0,
|
||||
@Expose @get: NonNull var days: String? = null
|
||||
)
|
||||
) {
|
||||
@ManyToOne(fetch = FetchType.EAGER, optional = false)
|
||||
@JoinColumn(name = "customerId", nullable = false)
|
||||
@JsonIgnore var customer: Customer? = null
|
||||
}
|
||||
@@ -12,4 +12,6 @@ data class Membership (
|
||||
@Expose @get: NonNull var duration: Int? = null,
|
||||
@Expose @get: NonNull var durationType: String? = null,
|
||||
@Expose @get: NonNull var durationUnit: String? = null,
|
||||
@Expose @get: NonNull var trainingPlanId: Long = 0,
|
||||
@Expose @get: NonNull var trainingPlanDayIds: String? = null
|
||||
)
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.aitrainer.api.repository
|
||||
|
||||
import com.aitrainer.api.model.Customer
|
||||
import com.aitrainer.api.model.CustomerMembership
|
||||
import org.springframework.data.jpa.repository.JpaRepository
|
||||
import org.springframework.data.jpa.repository.Query
|
||||
@@ -8,7 +9,7 @@ import org.springframework.stereotype.Repository
|
||||
@Repository
|
||||
interface CustomerMembershipRepository : JpaRepository<CustomerMembership, Long> {
|
||||
@Query(" FROM CustomerMembership " +
|
||||
" WHERE customerId = :customerId"
|
||||
" WHERE customer = :customer"
|
||||
)
|
||||
fun findAllByCustomerId(customerId: Long): List<CustomerMembership>
|
||||
fun findAllByCustomer(customer: Customer): List<CustomerMembership>
|
||||
}
|
||||
|
||||
@@ -59,8 +59,6 @@ class Firebase {
|
||||
error = JSONObject(response.body()).getJSONObject("error").getString("message").toString()
|
||||
return error
|
||||
}
|
||||
|
||||
print("Firebase $command response $response")
|
||||
error = ""
|
||||
return response.body()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user