Customer.firebaseUid

This commit is contained in:
Bossanyi Tibor
2020-10-25 12:13:30 +01:00
parent e807db3faf
commit c04f0cbbc4
10 changed files with 97 additions and 27 deletions
@@ -4,6 +4,7 @@ import com.aitrainer.api.model.Customer
import com.aitrainer.api.model.User
import com.aitrainer.api.service.ServiceBeans
import com.aitrainer.api.repository.CustomerRepository
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.http.HttpHeaders
import org.springframework.http.ResponseEntity
@@ -16,6 +17,7 @@ import javax.validation.Valid
@RestController
@RequestMapping("/api")
class CustomerController ( private val customerRepository: CustomerRepository ) {
private val logger = LoggerFactory.getLogger(javaClass)
@Autowired
var serviceBeans: ServiceBeans? = null
@@ -38,6 +40,13 @@ class CustomerController ( private val customerRepository: CustomerRepository )
}.orElse(ResponseEntity.notFound().build())
}
@Secured
@GetMapping("/customers/firebase/{uid}")
fun getCustomerByFirebaseUid(@PathVariable(value = "uid") firebaseUid: String): ResponseEntity<Customer> {
val customer: Customer? = customerRepository.findByFirebaseUid(firebaseUid)
return if (customer == null) ResponseEntity.notFound().build() else ResponseEntity.ok().body(customer)
}
@Secured
@GetMapping("/customers/real")
@@ -53,6 +62,18 @@ class CustomerController ( private val customerRepository: CustomerRepository )
return if (list.isEmpty()) ResponseEntity.notFound().build() else ResponseEntity.ok().body(list)
}
@Secured
@PostMapping("customers/update_firebase_uid/{id}")
fun updateCustomerFirebaseUidById(@PathVariable(value = "id") customerId: Long, @Valid @RequestBody firebaseUid: String)
: ResponseEntity<Customer> {
logger.info("Get customer id: $customerId uid: $firebaseUid")
val returnCustomer: Customer = customerRepository.findById(customerId).orElse(null)
?: return ResponseEntity.notFound().build()
returnCustomer.firebaseUid = firebaseUid;
return ResponseEntity.ok().body(customerRepository.save(returnCustomer))
}
@Secured
@PostMapping("/customers/{id}")
fun updateCustomerById(@PathVariable(value = "id") customerId: Long,
@@ -103,6 +124,7 @@ class CustomerController ( private val customerRepository: CustomerRepository )
with (customer) {
email = newUser.username
password = serviceBeans!!.passwordEncoder().encode(newUser.password)
firebaseUid = newUser.firebaseUid
}
val returnCustomer: Customer? = customerRepository.findByEmail(newUser.username).let {
@@ -25,6 +25,7 @@ data class Customer (
var goal: String? = null,
var fitnessLevel: String = "beginner",
var bodyType: String? = null,
var firebaseUid: String? = null,
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
var customerId: Long = 0
@@ -6,7 +6,8 @@ import kotlinx.serialization.json.*
@Serializable
data class User (
var username: String = "",
var password: String = ""
var password: String = "",
var firebaseUid: String = ""
) {
@OptIn(UnstableDefault::class)
fun fromJson(json: String): User {
@@ -11,4 +11,6 @@ interface CustomerRepository : JpaRepository<Customer, Long> {
fun findByEmail(email: String?): Customer?
fun findByTrainerId( trainerId: Long ): List<Customer>
fun findByFirebaseUid(firebaseUid: String?): Customer?
}
@@ -16,6 +16,6 @@ logging.config=classpath:logback-spring.xml
logging.file=logs
# if the database structure has been changed, increment this version number
application.version=1.0.9
application.version=1.0.10
jwt.secret=aitrainer
+1 -1
View File
@@ -16,6 +16,6 @@ logging.config=classpath:logback-spring.xml
logging.file=logs
# if the database structure has been changed, increment this version number
application.version=1.0.9
application.version=1.0.10
jwt.secret=aitrainer
@@ -42,7 +42,7 @@ class CustomerTests {
@Test
fun testTrainer() {
val id: Long = 137
val customers: List<Customer> = customerRepository.findByTrainerId( id );
val customers: List<Customer> = customerRepository.findByTrainerId( id )
assertEquals(customers.count(), 3)
assertEquals( customers[0].name, "Átlag 13 éves fiú")
}
@@ -119,6 +119,7 @@ class CustomerTests {
val customerController = CustomerController(customerRepository)
var response: ResponseEntity<*> = customerController.updateCustomerById(id, customer, HttpHeaders.readOnlyHttpHeaders(HttpHeaders.EMPTY) )
print ("body " + response.body)
var newCustomer: Customer? = response.body as Customer
assertEquals(response.statusCode, HttpStatus.OK)
assertEquals(newCustomer?.weight, 79)
@@ -147,21 +148,24 @@ class CustomerTests {
@Test
fun testRegistration() {
val json = "{\"username\":\"bosi@example.com\",\"password\":\"94385\"}"
val json = "{\"username\":\"bosi2@example.com\",\"password\":\"94385\",\"firebaseUid\":\"3Firebase8Uid\"}"
val user: User = User().fromJson(json)
assertEquals(user.username, "bosi@example.com")
assertEquals(user.username, "bosi2@example.com")
val customer = Customer()
with(customer) {
email = user.username
password = user.password
firebaseUid = user.firebaseUid
}
val customerController = CustomerController(customerRepository)
customerController.serviceBeans = serviceBean
var response: ResponseEntity<*> = customerController.registration(json)
print("body " + response.body)
val newCustomer: Customer? = response.body as Customer
assertEquals(response.statusCode, HttpStatus.OK)
assertEquals(newCustomer?.firebaseUid, "3Firebase8Uid")
val json2 = "{\"username\":\"bosi@example.com\",\"password\":\"934345\"}"
val json2 = "{\"username\":\"bosi2@example.com\",\"password\":\"934345\",\"firebaseUid\":\"3Firebase8Uid\"}"
response = customerController.registration(json2)
assertEquals(response.statusCode, HttpStatus.BAD_REQUEST)
@@ -170,7 +174,39 @@ class CustomerTests {
}
}
@Test fun testLogin() {
@Test
fun testUpdateFirebaseUid() {
val newCustomer = Customer("Bossanyi2", "Tibor", "", 48, "m")
val savedCustomer: Customer = customerRepository.save(newCustomer)
assertEquals(savedCustomer.age, 48)
insertedId = savedCustomer.customerId
val customerController = CustomerController(customerRepository)
val response: ResponseEntity<*> = customerController.updateCustomerFirebaseUidById(insertedId!!, "3FirebusaeId4")
val newCustomer2: Customer? = response.body as Customer
assertEquals(response.statusCode, HttpStatus.OK)
assertEquals(newCustomer2!!.firebaseUid, "3FirebusaeId4")
assertEquals(newCustomer2.firstname, "Tibor")
assertEquals(newCustomer2.name, "Bossanyi2")
assertEquals(newCustomer2.age, 48)
customerRepository.delete(newCustomer)
}
@Test
fun testGetCustomerByFirebaseUid() {
val uid = "3FirebaseU1d"
val customerController = CustomerController(customerRepository)
val response: ResponseEntity<*> = customerController.getCustomerByFirebaseUid(uid)
assertEquals(response.statusCode, HttpStatus.OK)
val newCustomer: Customer? = response.body as Customer
assertEquals(newCustomer!!.name, "Bos")
assertEquals(newCustomer.email, "sw2@andio.biz")
}
/*@Test fun _testLogin() {
val json = "{\"username\":\"bosi2@example.com\",\"password\":\"94333385\"}"
val user: User = User().fromJson(json)
val customer = Customer()
@@ -196,5 +232,5 @@ class CustomerTests {
if ( newCustomer != null) {
customerRepository.delete(newCustomer)
}
}
}*/
}