authenticaction, registration, login
This commit is contained in:
@@ -6,7 +6,7 @@ import org.springframework.web.bind.annotation.RequestMapping
|
||||
import org.springframework.web.bind.annotation.RestController
|
||||
|
||||
@RestController
|
||||
@RequestMapping()
|
||||
@RequestMapping
|
||||
class ApplicationProperties {
|
||||
|
||||
@Value("\${application.version}")
|
||||
|
||||
@@ -1,39 +1,53 @@
|
||||
package com.aitrainer.api.controller
|
||||
|
||||
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.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 javax.validation.Valid
|
||||
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api")
|
||||
class CustomerController ( private val customerRepository: CustomerRepository ) {
|
||||
|
||||
|
||||
@Autowired
|
||||
var serviceBeans: ServiceBeans? = null
|
||||
|
||||
@Secured
|
||||
@GetMapping("/customers")
|
||||
fun getAllCustomers(): List<Customer> =
|
||||
fun getAllCustomers(@RequestHeader headers: HttpHeaders): List<Customer> =
|
||||
customerRepository.findAll()
|
||||
|
||||
@Secured
|
||||
@PostMapping("/customers")
|
||||
fun createNewCustomer(@Valid @RequestBody customer: Customer): Customer =
|
||||
fun createNewCustomer(@Valid @RequestBody customer: Customer, @RequestHeader headers: HttpHeaders): Customer =
|
||||
customerRepository.save(customer)
|
||||
|
||||
@Secured
|
||||
@GetMapping("/customers/{id}")
|
||||
fun getCustomerById(@PathVariable(value = "id") customerId: Long): ResponseEntity<Customer> {
|
||||
fun getCustomerById(@PathVariable(value = "id") customerId: Long, @RequestHeader headers: HttpHeaders): ResponseEntity<Customer> {
|
||||
return customerRepository.findById(customerId).map { customer ->
|
||||
ResponseEntity.ok(customer)
|
||||
}.orElse(ResponseEntity.notFound().build())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Secured
|
||||
@GetMapping("/customers/real")
|
||||
fun getRealCustomers(active: String): List<Customer> =
|
||||
fun getRealCustomers(active: String, @RequestHeader headers: HttpHeaders): List<Customer> =
|
||||
customerRepository.findByActive(active)
|
||||
|
||||
|
||||
@Secured
|
||||
@PutMapping("/customers/{id}")
|
||||
fun updateCustomerById(@PathVariable(value = "id") customerId: Long,
|
||||
@Valid @RequestBody newCustomer: Customer): ResponseEntity<Customer> {
|
||||
@Valid @RequestBody newCustomer: Customer,
|
||||
@RequestHeader headers: HttpHeaders): ResponseEntity<Customer> {
|
||||
|
||||
return customerRepository.findById(customerId).map { existingCustomer ->
|
||||
val updatedCustomer: Customer = existingCustomer
|
||||
@@ -43,6 +57,60 @@ class CustomerController ( private val customerRepository: CustomerRepository )
|
||||
age = newCustomer.age)
|
||||
ResponseEntity.ok().body(customerRepository.save(updatedCustomer))
|
||||
}.orElse(ResponseEntity.notFound().build())
|
||||
}
|
||||
|
||||
|
||||
@PostMapping("/registration")
|
||||
fun registration(@Valid @RequestBody json: String): ResponseEntity<*> {
|
||||
val customer = Customer()
|
||||
|
||||
val newUser: User = User().fromJson(json)
|
||||
with (customer) {
|
||||
email = newUser.username
|
||||
password = serviceBeans!!.passwordEncoder().encode(newUser.password)
|
||||
}
|
||||
|
||||
val returnCustomer: Customer? = customerRepository.findByEmail(newUser.username).let {
|
||||
if ( it == null) {
|
||||
customerRepository.save(customer)
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
return if ( returnCustomer != null ) {
|
||||
ResponseEntity.ok().body(returnCustomer)
|
||||
} else {
|
||||
ResponseEntity.badRequest().body("Customer exists")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@GetMapping("/login")
|
||||
fun login(@Valid @RequestBody json: String): ResponseEntity<*> {
|
||||
val customer = Customer()
|
||||
|
||||
val newUser: User = User().fromJson(json)
|
||||
with (customer) {
|
||||
email = newUser.username
|
||||
password = newUser.password
|
||||
}
|
||||
val returnCustomer: Customer? = customerRepository.findByEmail(newUser.username).let {
|
||||
if ( it == null) {
|
||||
null
|
||||
} else {
|
||||
if (serviceBeans!!.passwordEncoder().matches(newUser.password, it.password)) {
|
||||
it
|
||||
} else {
|
||||
null
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
return if ( returnCustomer != null ) {
|
||||
ResponseEntity.ok().body(returnCustomer)
|
||||
} else {
|
||||
ResponseEntity.badRequest().body("Customer does not exist or the password is wrong")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,15 +2,13 @@ package com.aitrainer.api.controller
|
||||
|
||||
import com.aitrainer.api.ApiApplication
|
||||
import com.aitrainer.api.repository.ConfigurationRepository
|
||||
import org.aspectj.lang.annotation.Around
|
||||
import org.aspectj.lang.JoinPoint
|
||||
import org.aspectj.lang.annotation.Aspect
|
||||
import org.aspectj.lang.annotation.Before
|
||||
import org.aspectj.lang.annotation.Pointcut
|
||||
import org.slf4j.LoggerFactory
|
||||
import org.springframework.beans.factory.annotation.Autowired
|
||||
import org.springframework.stereotype.Component
|
||||
|
||||
|
||||
@Suppress("unused")
|
||||
@Aspect
|
||||
@Component
|
||||
@@ -22,13 +20,11 @@ class CustomerControllerAspect {
|
||||
@Autowired
|
||||
private lateinit var properties: ApplicationProperties
|
||||
|
||||
@Suppress("unused")
|
||||
@Pointcut("execution(* com.aitrainer.api.controller.CustomerController.*())")
|
||||
fun customerControllerAspect() {
|
||||
}
|
||||
|
||||
@Before("customerControllerAspect()")
|
||||
fun loggingAop() {
|
||||
@Before("execution(* com.aitrainer.api.controller.CustomerController.*(..))")
|
||||
fun customerControllerAspect(joinPoint: JoinPoint) {
|
||||
println("customer controller")
|
||||
Singleton.checkDBUpdate(configurationRepository, properties)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -3,9 +3,7 @@ package com.aitrainer.api.controller
|
||||
import com.aitrainer.api.ApiApplication
|
||||
import com.aitrainer.api.model.Configuration
|
||||
import com.aitrainer.api.repository.ConfigurationRepository
|
||||
import org.hibernate.Hibernate.isInitialized
|
||||
import org.slf4j.LoggerFactory
|
||||
import org.springframework.beans.factory.annotation.Autowired
|
||||
import org.springframework.web.bind.annotation.RestController
|
||||
import java.io.File
|
||||
import java.sql.Connection
|
||||
@@ -73,8 +71,7 @@ object Singleton {
|
||||
|
||||
}
|
||||
|
||||
fun execSQL( sql: String ) {
|
||||
|
||||
private fun execSQL( sql: String ) {
|
||||
if (! this.initialized ) {
|
||||
this.getConnection()
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@ package com.aitrainer.api.controller
|
||||
|
||||
import com.aitrainer.api.ApiApplication
|
||||
import com.aitrainer.api.repository.ConfigurationRepository
|
||||
import org.aspectj.lang.annotation.Around
|
||||
import org.aspectj.lang.annotation.Aspect
|
||||
import org.aspectj.lang.annotation.Before
|
||||
import org.aspectj.lang.annotation.Pointcut
|
||||
|
||||
Reference in New Issue
Block a user