v1.2.7 memberships

This commit is contained in:
bossanyit
2023-05-08 07:58:57 +02:00
parent a8d54fa78f
commit 7a6dd693cb
9 changed files with 142 additions and 21 deletions
@@ -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)
}
}
}
@@ -10,6 +10,7 @@ data class CustomerMembership (
@Expose @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @get: NonNull var id: Long = 0,
@Expose @get: NonNull var membershipId: Long = 0,
@Expose @get: NonNull var startDate: String? = null,
@Expose @get: NonNull var endDate: String? = null,
) {
@ManyToOne(fetch = FetchType.EAGER, optional = false)
@JoinColumn(name = "customerId", nullable = false)
@@ -16,7 +16,7 @@ logging.config=classpath:logback-spring.xml
logging.file=logs
# if the database structure has been changed, increment this version number
application.version=1.2.6
application.version=1.2.7
jwt.secret=aitrainer
@@ -14,7 +14,7 @@ logging.config=classpath:logback-spring.xml
logging.file=logs
# if the database structue has been changed, increment this version number
application.version=1.2.6
application.version=1.2.7
jwt.secret=aitrainer
@@ -14,7 +14,7 @@ logging.config=classpath:logback-spring.xml
logging.file=logs
# if the database structue has been changed, increment this version number
application.version=1.2.6
application.version=1.2.7
jwt.secret=aitrainer
+1 -1
View File
@@ -17,7 +17,7 @@ logging.config=classpath:logback-spring.xml
logging.file=logs
# if the database structure has been changed, increment this version number
application.version=1.2.6
application.version=1.2.7
jwt.secret=aitrainer