From 5f0a7fe2a33e9bddd878ec7c237aa8daa5aa63e3 Mon Sep 17 00:00:00 2001 From: Andrey Kassaev Date: Mon, 1 Jan 2024 11:06:45 +0400 Subject: [PATCH] db connected --- build.gradle.kts | 20 ++++---- .../com/kassaev/notes/config/Configuration.kt | 5 +- .../notes/controller/auth/AuthController.kt | 10 +++- .../notes/controller/note/NoteController.kt | 11 ++++- .../notes/controller/user/UserController.kt | 37 +++++++------- .../notes/controller/user/UserResponse.kt | 4 +- .../kotlin/com/kassaev/notes/model/Note.kt | 8 +++- .../com/kassaev/notes/model/RefreshToken.kt | 14 ++++++ .../kotlin/com/kassaev/notes/model/User.kt | 9 +++- .../notes/repository/INoteRepository.kt | 8 ++++ .../repository/IRefreshTokenRepository.kt | 11 +++++ .../notes/repository/IUserRepository.kt | 15 ++++++ .../notes/repository/MockRepository.kt | 33 ------------- .../notes/repository/UserRepository.kt | 15 +++--- .../notes/service/AuthenticationService.kt | 48 ++++++++++++++++++- .../notes/service/CustomUserDetailsService.kt | 4 +- .../com/kassaev/notes/service/NoteService.kt | 27 ++++++----- .../com/kassaev/notes/service/TokenService.kt | 4 ++ .../com/kassaev/notes/service/UserService.kt | 15 +++--- src/main/resources/application.yaml | 15 +++++- .../kassaev/notes/NotesApplicationTests.kt | 13 ----- 21 files changed, 208 insertions(+), 118 deletions(-) create mode 100644 src/main/kotlin/com/kassaev/notes/model/RefreshToken.kt create mode 100644 src/main/kotlin/com/kassaev/notes/repository/INoteRepository.kt create mode 100644 src/main/kotlin/com/kassaev/notes/repository/IRefreshTokenRepository.kt create mode 100644 src/main/kotlin/com/kassaev/notes/repository/IUserRepository.kt delete mode 100644 src/main/kotlin/com/kassaev/notes/repository/MockRepository.kt delete mode 100644 src/test/kotlin/com/kassaev/notes/NotesApplicationTests.kt diff --git a/build.gradle.kts b/build.gradle.kts index 98000a5..6bf2235 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -3,8 +3,9 @@ import org.jetbrains.kotlin.gradle.tasks.KotlinCompile plugins { id("org.springframework.boot") version "3.2.0" id("io.spring.dependency-management") version "1.1.4" - kotlin("jvm") version "1.9.20" - kotlin("plugin.spring") version "1.9.20" + kotlin("jvm") version "1.9.22" + kotlin("plugin.spring") version "1.9.22" + kotlin("plugin.jpa") version "1.9.22" } group = "com.kassaev" @@ -19,20 +20,23 @@ repositories { } dependencies { - implementation("org.springframework.boot:spring-boot-starter-web") - implementation("com.fasterxml.jackson.module:jackson-module-kotlin") - implementation("org.jetbrains.kotlin:kotlin-reflect") - testImplementation("org.springframework.boot:spring-boot-starter-test") + implementation("org.springframework.boot:spring-boot-starter-web:3.2.1") + implementation("com.fasterxml.jackson.module:jackson-module-kotlin:2.16.1") + implementation("org.jetbrains.kotlin:kotlin-reflect:1.9.22") compileOnly("org.projectlombok:lombok:1.18.30") annotationProcessor("org.projectlombok:lombok:1.18.30") - implementation ("org.springframework.boot:spring-boot-starter-security") - testImplementation("org.springframework.security:spring-security-test") + implementation("org.springframework.boot:spring-boot-starter-security:3.2.1") + implementation("io.jsonwebtoken:jjwt-api:0.12.3") implementation("io.jsonwebtoken:jjwt-impl:0.12.3") implementation("io.jsonwebtoken:jjwt-jackson:0.12.3") + + + implementation("org.postgresql:postgresql:42.7.1") + implementation("org.springframework.boot:spring-boot-starter-data-jpa:3.2.1") } tasks.withType { diff --git a/src/main/kotlin/com/kassaev/notes/config/Configuration.kt b/src/main/kotlin/com/kassaev/notes/config/Configuration.kt index f371929..e62645f 100644 --- a/src/main/kotlin/com/kassaev/notes/config/Configuration.kt +++ b/src/main/kotlin/com/kassaev/notes/config/Configuration.kt @@ -1,5 +1,6 @@ package com.kassaev.notes.config +import com.kassaev.notes.repository.IUserRepository import com.kassaev.notes.repository.UserRepository import com.kassaev.notes.service.CustomUserDetailsService import org.springframework.boot.context.properties.EnableConfigurationProperties @@ -18,14 +19,14 @@ import org.springframework.security.crypto.password.PasswordEncoder class Configuration { @Bean - fun userDetailsService(userRepository: UserRepository): UserDetailsService = + fun userDetailsService(userRepository: IUserRepository): UserDetailsService = CustomUserDetailsService(userRepository) @Bean fun encoder(): PasswordEncoder = BCryptPasswordEncoder() @Bean - fun authenticationProvider(userRepository: UserRepository): AuthenticationProvider = + fun authenticationProvider(userRepository: IUserRepository): AuthenticationProvider = DaoAuthenticationProvider() .also { it.setUserDetailsService(userDetailsService(userRepository)) diff --git a/src/main/kotlin/com/kassaev/notes/controller/auth/AuthController.kt b/src/main/kotlin/com/kassaev/notes/controller/auth/AuthController.kt index 917e36c..492d018 100644 --- a/src/main/kotlin/com/kassaev/notes/controller/auth/AuthController.kt +++ b/src/main/kotlin/com/kassaev/notes/controller/auth/AuthController.kt @@ -18,7 +18,7 @@ class AuthController( fun authenticate(@RequestBody authRequest: AuthenticationRequest): AuthenticationResponse = authenticationService.authentication(authRequest) - @PostMapping("/refresh") + @PostMapping("/refresh_old") fun refreshAccessToken( @RequestBody request: RefreshTokenRequest ): TokenResponse = @@ -26,6 +26,14 @@ class AuthController( ?.mapToTokenResponse() ?: throw ResponseStatusException(HttpStatus.FORBIDDEN, "Invalid refresh token!") + @PostMapping("/refresh") + fun refreshAccessToken2( + @RequestBody request: RefreshTokenRequest + ): TokenResponse = + authenticationService.refreshAccessToken2(request.token) + ?.mapToTokenResponse() + ?: throw ResponseStatusException(HttpStatus.FORBIDDEN, "Invalid refresh token!") + private fun String.mapToTokenResponse(): TokenResponse = TokenResponse( token = this diff --git a/src/main/kotlin/com/kassaev/notes/controller/note/NoteController.kt b/src/main/kotlin/com/kassaev/notes/controller/note/NoteController.kt index 2e055d0..55571dc 100644 --- a/src/main/kotlin/com/kassaev/notes/controller/note/NoteController.kt +++ b/src/main/kotlin/com/kassaev/notes/controller/note/NoteController.kt @@ -7,10 +7,12 @@ import org.springframework.beans.factory.annotation.Autowired import org.springframework.web.bind.annotation.DeleteMapping import org.springframework.web.bind.annotation.GetMapping import org.springframework.web.bind.annotation.PathVariable +import org.springframework.web.bind.annotation.PostMapping import org.springframework.web.bind.annotation.PutMapping import org.springframework.web.bind.annotation.RequestBody import org.springframework.web.bind.annotation.RequestMapping import org.springframework.web.bind.annotation.RestController +import java.util.* @RestController @RequestMapping("/api/v1.0.0/note") @@ -25,8 +27,13 @@ class NoteController(val service: NoteService) { return service.getAllNotes() } + @PostMapping("/create") + fun createNote(@RequestBody note: Note): Note { + return service.updateNote(note) + } + @GetMapping("/{id}") - fun getNoteById(@PathVariable id: Int): Note? { + fun getNoteById(@PathVariable id: Long): Optional { return service.getNoteById(id) } @@ -36,7 +43,7 @@ class NoteController(val service: NoteService) { } @DeleteMapping("/remove/{id}") - fun deleteNote(@PathVariable id: Int){ + fun deleteNote(@PathVariable id: Long){ return service.deleteNote(id) } } \ No newline at end of file diff --git a/src/main/kotlin/com/kassaev/notes/controller/user/UserController.kt b/src/main/kotlin/com/kassaev/notes/controller/user/UserController.kt index a3528d4..f224161 100644 --- a/src/main/kotlin/com/kassaev/notes/controller/user/UserController.kt +++ b/src/main/kotlin/com/kassaev/notes/controller/user/UserController.kt @@ -3,8 +3,9 @@ package com.kassaev.notes.controller.user import com.kassaev.notes.model.Role import com.kassaev.notes.model.User import com.kassaev.notes.service.UserService +import org.springframework.data.jpa.domain.AbstractPersistable_.id import org.springframework.http.HttpStatus -import org.springframework.http.ResponseEntity +import org.springframework.security.crypto.password.PasswordEncoder import org.springframework.web.bind.annotation.DeleteMapping import org.springframework.web.bind.annotation.GetMapping import org.springframework.web.bind.annotation.PathVariable @@ -13,12 +14,12 @@ import org.springframework.web.bind.annotation.RequestBody import org.springframework.web.bind.annotation.RequestMapping import org.springframework.web.bind.annotation.RestController import org.springframework.web.server.ResponseStatusException -import java.util.UUID @RestController @RequestMapping("/api/v1.0.0/user") class UserController( - private val userService: UserService + private val userService: UserService, + private val encoder: PasswordEncoder ) { @PostMapping @@ -34,34 +35,32 @@ class UserController( userService.findAll() .map { it.toResponse() } - @GetMapping("/{uuid}") - fun findByUUID(@PathVariable uuid: UUID): UserResponse = - userService.findByUUID(uuid) - ?.toResponse() - ?: throw ResponseStatusException(HttpStatus.NOT_FOUND, "Cannot find a user.") - - @DeleteMapping("/{uuid}") - fun deleteByUUID(@PathVariable uuid: UUID): ResponseEntity { - val success = userService.deleteByUUID(uuid) - - return if(success) { - ResponseEntity.noContent().build() - } else { + @GetMapping("/{id}") + fun findById(@PathVariable id: Long): UserResponse { + return if (userService.findById(id).isPresent){ + val user = userService.findById(id).get() + user.toResponse() + }else{ throw ResponseStatusException(HttpStatus.NOT_FOUND, "Cannot find a user.") } } + @DeleteMapping("/{id}") + fun deleteById(@PathVariable id: Long){ + userService.deleteById(id) + } + private fun UserRequest.toModel(): User = User( - id = UUID.randomUUID(), + id = null, email = this.email, - password = this.password, + password = encoder.encode(this.password), role = Role.USER ) private fun User.toResponse(): UserResponse = UserResponse( - uuid = this.id, + id = this.id, email = this.email ) } \ No newline at end of file diff --git a/src/main/kotlin/com/kassaev/notes/controller/user/UserResponse.kt b/src/main/kotlin/com/kassaev/notes/controller/user/UserResponse.kt index d6d05fe..510bf38 100644 --- a/src/main/kotlin/com/kassaev/notes/controller/user/UserResponse.kt +++ b/src/main/kotlin/com/kassaev/notes/controller/user/UserResponse.kt @@ -1,8 +1,6 @@ package com.kassaev.notes.controller.user -import java.util.UUID - data class UserResponse( - val uuid: UUID, + val id: Long?, val email: String ) diff --git a/src/main/kotlin/com/kassaev/notes/model/Note.kt b/src/main/kotlin/com/kassaev/notes/model/Note.kt index e5e40f1..7bc32a3 100644 --- a/src/main/kotlin/com/kassaev/notes/model/Note.kt +++ b/src/main/kotlin/com/kassaev/notes/model/Note.kt @@ -1,7 +1,13 @@ package com.kassaev.notes.model +import jakarta.persistence.* + +@Entity +@Table(name = "notes") data class Note( - val id: Int, + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + val id: Long?, val text: String, val dateCreated: String ) \ No newline at end of file diff --git a/src/main/kotlin/com/kassaev/notes/model/RefreshToken.kt b/src/main/kotlin/com/kassaev/notes/model/RefreshToken.kt new file mode 100644 index 0000000..cf777f6 --- /dev/null +++ b/src/main/kotlin/com/kassaev/notes/model/RefreshToken.kt @@ -0,0 +1,14 @@ +package com.kassaev.notes.model + +import jakarta.persistence.Entity +import jakarta.persistence.Id +import jakarta.persistence.Table +import org.springframework.security.core.userdetails.UserDetails + +@Entity +@Table(name = "refresh_tokens") +data class RefreshToken( + @Id + val email: String, + val token: String +) diff --git a/src/main/kotlin/com/kassaev/notes/model/User.kt b/src/main/kotlin/com/kassaev/notes/model/User.kt index f716c7c..5695827 100644 --- a/src/main/kotlin/com/kassaev/notes/model/User.kt +++ b/src/main/kotlin/com/kassaev/notes/model/User.kt @@ -1,11 +1,16 @@ package com.kassaev.notes.model -import java.util.UUID +import jakarta.persistence.* +@Entity +@Table(name="users") data class User( - val id: UUID, + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + val id: Long?, val email: String, val password: String, + @Enumerated(EnumType.STRING) val role: Role ) diff --git a/src/main/kotlin/com/kassaev/notes/repository/INoteRepository.kt b/src/main/kotlin/com/kassaev/notes/repository/INoteRepository.kt new file mode 100644 index 0000000..0e74184 --- /dev/null +++ b/src/main/kotlin/com/kassaev/notes/repository/INoteRepository.kt @@ -0,0 +1,8 @@ +package com.kassaev.notes.repository + +import com.kassaev.notes.model.Note +import org.springframework.data.jpa.repository.JpaRepository + +interface INoteRepository: JpaRepository { + +} \ No newline at end of file diff --git a/src/main/kotlin/com/kassaev/notes/repository/IRefreshTokenRepository.kt b/src/main/kotlin/com/kassaev/notes/repository/IRefreshTokenRepository.kt new file mode 100644 index 0000000..0ae8764 --- /dev/null +++ b/src/main/kotlin/com/kassaev/notes/repository/IRefreshTokenRepository.kt @@ -0,0 +1,11 @@ +package com.kassaev.notes.repository + +import com.kassaev.notes.model.RefreshToken +import org.springframework.data.jpa.repository.JpaRepository +import org.springframework.data.jpa.repository.Query +import org.springframework.data.repository.query.Param + +interface IRefreshTokenRepository: JpaRepository { + @Query("SELECT * FROM refresh_tokens WHERE email = :email", nativeQuery = true) + fun findByEmail(@Param("email") email: String): RefreshToken? +} \ No newline at end of file diff --git a/src/main/kotlin/com/kassaev/notes/repository/IUserRepository.kt b/src/main/kotlin/com/kassaev/notes/repository/IUserRepository.kt new file mode 100644 index 0000000..31d4f68 --- /dev/null +++ b/src/main/kotlin/com/kassaev/notes/repository/IUserRepository.kt @@ -0,0 +1,15 @@ +package com.kassaev.notes.repository + +import com.kassaev.notes.model.User +import org.springframework.data.jpa.repository.JpaRepository +import org.springframework.data.jpa.repository.Query +import org.springframework.data.repository.query.Param +import java.awt.print.Book +import java.time.LocalDate + + +interface IUserRepository: JpaRepository { + + @Query("SELECT * FROM users WHERE email = :email", nativeQuery = true) + fun findByEmail(@Param("email") email: String): User? +} \ No newline at end of file diff --git a/src/main/kotlin/com/kassaev/notes/repository/MockRepository.kt b/src/main/kotlin/com/kassaev/notes/repository/MockRepository.kt deleted file mode 100644 index 58b56ec..0000000 --- a/src/main/kotlin/com/kassaev/notes/repository/MockRepository.kt +++ /dev/null @@ -1,33 +0,0 @@ -package com.kassaev.notes.repository - -import com.kassaev.notes.model.Note -import org.springframework.stereotype.Repository -import org.springframework.stereotype.Service -import java.util.* - -@Repository -class MockRepository { - - private val notesList = mutableListOf( - Note(id = 0, text = "first", dateCreated = "21:15"), - Note(id = 1, text = "second", dateCreated = "22:15"), - Note(id = 2, text = "third", dateCreated = "23:15"), - Note(id = 3, text = "fourth", dateCreated = "00:15"), - ) - fun getAllNotes(): List{ - return notesList - } - - fun getNoteById(id: Int): Note? { - return notesList.find{ it.id == id} - } - - fun updateNote(note: Note): Note { - notesList[note.id] = note - return note - } - - fun deleteNote(id: Int){ - notesList.removeIf { it.id == id } - } -} \ No newline at end of file diff --git a/src/main/kotlin/com/kassaev/notes/repository/UserRepository.kt b/src/main/kotlin/com/kassaev/notes/repository/UserRepository.kt index dca5f4d..6c37395 100644 --- a/src/main/kotlin/com/kassaev/notes/repository/UserRepository.kt +++ b/src/main/kotlin/com/kassaev/notes/repository/UserRepository.kt @@ -4,7 +4,6 @@ import com.kassaev.notes.model.Role import com.kassaev.notes.model.User import org.springframework.security.crypto.password.PasswordEncoder import org.springframework.stereotype.Repository -import java.util.* @Repository class UserRepository( @@ -12,19 +11,19 @@ class UserRepository( ) { private val users = mutableListOf( User( - id = UUID.randomUUID(), + id = 1, email = "user1@mail.com", password = encoder.encode("pass1"), role = Role.USER ), User( - id = UUID.randomUUID(), + id = 2, email = "user2@mail.com", password = encoder.encode("pass2"), role = Role.USER ), User( - id = UUID.randomUUID(), + id = 3, email = "user3@mail.com", password = encoder.encode("pass3"), role = Role.ADMIN @@ -41,14 +40,14 @@ class UserRepository( fun findByEmail(email: String): User? = users.firstOrNull { it.email == email } - fun findByUUID(uuid: UUID): User? = - users.firstOrNull { it.id == uuid } + fun findById(id: Long): User? = + users.firstOrNull { it.id == id } fun findAll(): List = users - fun deleteByUUID(uuid: UUID): Boolean { - val foundUser = findByUUID(uuid) + fun deleteById(id: Long): Boolean { + val foundUser = findById(id) return foundUser?.let { users.remove(it) diff --git a/src/main/kotlin/com/kassaev/notes/service/AuthenticationService.kt b/src/main/kotlin/com/kassaev/notes/service/AuthenticationService.kt index 52844a2..a768b6e 100644 --- a/src/main/kotlin/com/kassaev/notes/service/AuthenticationService.kt +++ b/src/main/kotlin/com/kassaev/notes/service/AuthenticationService.kt @@ -3,6 +3,8 @@ package com.kassaev.notes.service import com.kassaev.notes.config.JwtProperties import com.kassaev.notes.controller.auth.AuthenticationRequest import com.kassaev.notes.controller.auth.AuthenticationResponse +import com.kassaev.notes.model.RefreshToken +import com.kassaev.notes.repository.IRefreshTokenRepository import com.kassaev.notes.repository.RefreshTokenRepository import org.springframework.security.authentication.AuthenticationManager import org.springframework.security.authentication.UsernamePasswordAuthenticationToken @@ -16,7 +18,8 @@ class AuthenticationService( private val userDetailsService: CustomUserDetailsService, private val tokenService: TokenService, private val jwtProperties: JwtProperties, - private val refreshTokenRepository: RefreshTokenRepository + private val refreshTokenRepository: RefreshTokenRepository, + private val refreshTokenRepository2: IRefreshTokenRepository ) { fun authentication(authRequest: AuthenticationRequest): AuthenticationResponse { authManager.authenticate( @@ -31,7 +34,13 @@ class AuthenticationService( val accessToken = generateAccessToken(user) val refreshToken = generateRefreshToken(user) - refreshTokenRepository.save(refreshToken, user) + refreshTokenRepository2.save( + RefreshToken( + email = user.username, + token = refreshToken + ) + ) +// refreshTokenRepository2.save(refreshToken, user) return AuthenticationResponse( accessToken = accessToken, @@ -63,5 +72,40 @@ class AuthenticationService( null } } + fun refreshAccessToken2(token: String): String? { + val extractedEmail = tokenService.extractEmail(token) + + println() + println(extractedEmail) + println() + + return extractedEmail?.let { email -> + val currentUserDetails = userDetailsService.loadUserByUsername(email) + + println() + println("AAAAAAAAAAAAAAAA") + println(currentUserDetails.username) + println() + + val refreshTokenInDB = refreshTokenRepository2.findByEmail(currentUserDetails.username) +// val refreshTokenUserDetails = refreshTokenRepository2.findUserDetailsByToken(token) + + println() + println("BBBBBBBBBBB") + println(refreshTokenInDB?.email) + println() + + if (!tokenService.isExpired(token) && currentUserDetails.username == refreshTokenInDB?.email){ + + // TODO: check if this refresh token is the same token in db for this user . If so generate new access token and refresh token and update record in with newly created refresh token for this user. + println() + println("PPPPPPPPPPPPPPPPPPAAAAAAAAAAAAAAAASSSSSSSSSSSSSSSEEEEEEEEEEEEEEEDDDDDDDDDDDD!!!") + println() + generateAccessToken(currentUserDetails) + + } else + null + } + } } diff --git a/src/main/kotlin/com/kassaev/notes/service/CustomUserDetailsService.kt b/src/main/kotlin/com/kassaev/notes/service/CustomUserDetailsService.kt index 667d609..ca3db38 100644 --- a/src/main/kotlin/com/kassaev/notes/service/CustomUserDetailsService.kt +++ b/src/main/kotlin/com/kassaev/notes/service/CustomUserDetailsService.kt @@ -1,6 +1,6 @@ package com.kassaev.notes.service -import com.kassaev.notes.repository.UserRepository +import com.kassaev.notes.repository.IUserRepository import org.springframework.security.core.userdetails.User import org.springframework.security.core.userdetails.UserDetails import org.springframework.security.core.userdetails.UserDetailsService @@ -10,7 +10,7 @@ import org.springframework.stereotype.Service typealias ApplicationUser = com.kassaev.notes.model.User @Service class CustomUserDetailsService( - private val userRepository: UserRepository + private val userRepository: IUserRepository ): UserDetailsService { override fun loadUserByUsername(username: String): UserDetails = userRepository.findByEmail(username) diff --git a/src/main/kotlin/com/kassaev/notes/service/NoteService.kt b/src/main/kotlin/com/kassaev/notes/service/NoteService.kt index bcd5ba2..5d79233 100644 --- a/src/main/kotlin/com/kassaev/notes/service/NoteService.kt +++ b/src/main/kotlin/com/kassaev/notes/service/NoteService.kt @@ -1,30 +1,31 @@ package com.kassaev.notes.service import com.kassaev.notes.model.Note -import com.kassaev.notes.repository.MockRepository -import lombok.AllArgsConstructor -import org.springframework.beans.factory.annotation.Autowired +//import com.kassaev.notes.repository.MockRepository +import com.kassaev.notes.repository.INoteRepository import org.springframework.stereotype.Service +import java.util.* @Service -class NoteService(val repository: MockRepository) { - -// @Autowired -// lateinit var repository: MockRepository +class NoteService(val repository: INoteRepository) { fun getAllNotes(): List{ - return repository.getAllNotes() + return repository.findAll() +// return repository.getAllNotes() } - fun getNoteById(id: Int): Note? { - return repository.getNoteById(id) + fun getNoteById(id: Long): Optional { + return repository.findById(id) +// return repository.getNoteById(id) } fun updateNote(note: Note): Note { - return repository.updateNote(note) + return repository.save(note) +// return repository.updateNote(note) } - fun deleteNote(id: Int){ - repository.deleteNote(id) + fun deleteNote(id: Long){ + repository.deleteById(id) +// repository.deleteNote(id) } } \ No newline at end of file diff --git a/src/main/kotlin/com/kassaev/notes/service/TokenService.kt b/src/main/kotlin/com/kassaev/notes/service/TokenService.kt index 476b6d3..02f9764 100644 --- a/src/main/kotlin/com/kassaev/notes/service/TokenService.kt +++ b/src/main/kotlin/com/kassaev/notes/service/TokenService.kt @@ -12,7 +12,11 @@ import java.util.Date class TokenService( jwtProperties: JwtProperties ) { + val sekret = jwtProperties.key + + private val secretKey = Keys.hmacShaKeyFor( + jwtProperties.key.toByteArray() ) diff --git a/src/main/kotlin/com/kassaev/notes/service/UserService.kt b/src/main/kotlin/com/kassaev/notes/service/UserService.kt index d01067d..24997d0 100644 --- a/src/main/kotlin/com/kassaev/notes/service/UserService.kt +++ b/src/main/kotlin/com/kassaev/notes/service/UserService.kt @@ -1,13 +1,14 @@ package com.kassaev.notes.service import com.kassaev.notes.model.User -import com.kassaev.notes.repository.UserRepository +import com.kassaev.notes.repository.IUserRepository +import org.springframework.security.crypto.password.PasswordEncoder import org.springframework.stereotype.Service -import java.util.UUID +import java.util.* @Service class UserService( - private val userRepository: UserRepository + private val userRepository: IUserRepository ) { fun createUser(user: User): User? { val found = userRepository.findByEmail(user.email) @@ -18,13 +19,13 @@ class UserService( } else null } - fun findByUUID(uuid: UUID): User? = - userRepository.findByUUID(uuid) + fun findById(id: Long): Optional = + userRepository.findById(id) fun findAll(): List = userRepository.findAll() - fun deleteByUUID(uuid: UUID): Boolean = - userRepository.deleteByUUID(uuid) + fun deleteById(id: Long) = + userRepository.deleteById(id) } \ No newline at end of file diff --git a/src/main/resources/application.yaml b/src/main/resources/application.yaml index ddade16..6c2efff 100644 --- a/src/main/resources/application.yaml +++ b/src/main/resources/application.yaml @@ -1,4 +1,15 @@ jwt: key: ${JWT_KEY} - access-token-expiration: 3600000 - refresh-token-expiration: 86400000 \ No newline at end of file + access-token-expiration: ${JWT_ACCESS_EXP} + refresh-token-expiration: ${JWT_REFRESH_EXP} +spring: + datasource: + driver-class-name: ${DB_DRIVER} + url: ${DB_URL} + username: ${DB_USERNAME} + password: ${DB_PASSWORD} + jpa: + hibernate: + ddl-auto: update +server: + port: ${SPRING_PORT} \ No newline at end of file diff --git a/src/test/kotlin/com/kassaev/notes/NotesApplicationTests.kt b/src/test/kotlin/com/kassaev/notes/NotesApplicationTests.kt deleted file mode 100644 index 08e3ba8..0000000 --- a/src/test/kotlin/com/kassaev/notes/NotesApplicationTests.kt +++ /dev/null @@ -1,13 +0,0 @@ -package com.kassaev.notes - -import org.junit.jupiter.api.Test -import org.springframework.boot.test.context.SpringBootTest - -@SpringBootTest -class NotesApplicationTests { - - @Test - fun contextLoads() { - } - -}