|
|
|
@@ -3,12 +3,14 @@ package com.aitrainer.api.controller
|
|
|
|
|
import com.aitrainer.api.model.*
|
|
|
|
|
import com.aitrainer.api.service.ServiceBeans
|
|
|
|
|
import com.aitrainer.api.repository.CustomerRepository
|
|
|
|
|
import com.aitrainer.api.repository.MembershipRepository
|
|
|
|
|
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.beans.factory.annotation.Value
|
|
|
|
|
import org.springframework.data.repository.findByIdOrNull
|
|
|
|
|
import org.springframework.http.HttpHeaders
|
|
|
|
|
import org.springframework.http.ResponseEntity
|
|
|
|
|
import org.springframework.security.access.annotation.Secured
|
|
|
|
@@ -22,7 +24,7 @@ import java.util.Base64
|
|
|
|
|
|
|
|
|
|
@RestController
|
|
|
|
|
@RequestMapping("/api")
|
|
|
|
|
class CustomerController ( private val customerRepository: CustomerRepository) {
|
|
|
|
|
class CustomerController (private val customerRepository: CustomerRepository, private val membershipRepository: MembershipRepository,) {
|
|
|
|
|
private val logger = LoggerFactory.getLogger(javaClass)
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
@@ -325,4 +327,60 @@ class CustomerController ( private val customerRepository: CustomerRepository) {
|
|
|
|
|
ResponseEntity.badRequest().body("Customer does not exist or the password is wrong")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@PostMapping("/membership/{id}")
|
|
|
|
|
fun newMembership(@PathVariable(value = "id") membershipId: Long, @Valid @RequestBody customer: Customer): ResponseEntity<Customer> {
|
|
|
|
|
val returnCustomer: Customer = customerRepository.findById(customer.customerId).orElse(null)
|
|
|
|
|
?: return ResponseEntity.notFound().build()
|
|
|
|
|
println("found customer ${returnCustomer.customerId}")
|
|
|
|
|
|
|
|
|
|
val membership: Membership = membershipRepository.findByIdOrNull(membershipId)
|
|
|
|
|
?: return ResponseEntity.notFound().build()
|
|
|
|
|
|
|
|
|
|
println("found membership $membershipId")
|
|
|
|
|
|
|
|
|
|
val customerMembership = CustomerMembership()
|
|
|
|
|
customerMembership.membershipId = membershipId
|
|
|
|
|
customerMembership.customer = returnCustomer
|
|
|
|
|
val now = LocalDateTime.now()
|
|
|
|
|
val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
|
|
|
|
|
customerMembership.startDate = now.format(formatter)
|
|
|
|
|
returnCustomer.memberships.add(customerMembership)
|
|
|
|
|
customerRepository.save(returnCustomer)
|
|
|
|
|
|
|
|
|
|
val savedCustomer: Customer = customerRepository.findById(customer.customerId).orElse(null)
|
|
|
|
|
?: return ResponseEntity.notFound().build()
|
|
|
|
|
|
|
|
|
|
println("saved ${savedCustomer.customerId}")
|
|
|
|
|
return ResponseEntity.ok(savedCustomer)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@PostMapping("/membership/cancel/{id}")
|
|
|
|
|
fun cancelMembership(@PathVariable(value = "id") membershipId: Long, @Valid @RequestBody customer: Customer): ResponseEntity<Customer> {
|
|
|
|
|
val returnCustomer: Customer = customerRepository.findById(customer.customerId).orElse(null)
|
|
|
|
|
?: return ResponseEntity.notFound().build()
|
|
|
|
|
println("found customer ${returnCustomer.customerId}")
|
|
|
|
|
|
|
|
|
|
val membership: Membership = membershipRepository.findByIdOrNull(membershipId)
|
|
|
|
|
?: return ResponseEntity.notFound().build()
|
|
|
|
|
println("found membership $membershipId")
|
|
|
|
|
|
|
|
|
|
var found = false
|
|
|
|
|
var savedCustomer: Customer? = null
|
|
|
|
|
for ( customerMembership in returnCustomer.memberships ) {
|
|
|
|
|
if ( customerMembership.membershipId == membershipId) {
|
|
|
|
|
val now = LocalDateTime.now()
|
|
|
|
|
val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd")
|
|
|
|
|
customerMembership.endDate = now.format(formatter)
|
|
|
|
|
savedCustomer = customerRepository.save(returnCustomer)
|
|
|
|
|
found = true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return if ( ! found ) {
|
|
|
|
|
println("not found customer membership to cancel $membershipId")
|
|
|
|
|
ResponseEntity.notFound().build()
|
|
|
|
|
} else {
|
|
|
|
|
ResponseEntity.ok(savedCustomer)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|