Skip to content

Commit

Permalink
- Upgrade Kotlin version to the latest.
Browse files Browse the repository at this point in the history
- Cleanup the OffsetBasedPageRequest class.
- Add .editorconfig.
- Use let more.
  • Loading branch information
yaskovdev committed Aug 11, 2024
1 parent 7fac039 commit 6cedcc4
Show file tree
Hide file tree
Showing 13 changed files with 1,291 additions and 160 deletions.
1,209 changes: 1,209 additions & 0 deletions .editorconfig

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
</scm>
<properties>
<java.version>17</java.version>
<kotlin.version>1.9.24</kotlin.version>
<kotlin.version>2.0.20-RC</kotlin.version>
<swagger.version>2.9.2</swagger.version>
<dbunit.version>2.8.0</dbunit.version>
<spring.test.dbunit.version>1.3.0</spring.test.dbunit.version>
Expand Down
4 changes: 1 addition & 3 deletions src/main/kotlin/org/motivepick/config/SecurityConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,7 @@ internal class SecurityConfig {
val matchers: OrRequestMatcher = OrRequestMatcher(ANONYMOUS_URIS.map { AntPathRequestMatcher(it) })
val processingMatcher: RequestMatcher = AntPathRequestMatcher("/**")

override fun matches(request: HttpServletRequest): Boolean {
return !matchers.matches(request) && processingMatcher.matches(request)
}
override fun matches(request: HttpServletRequest): Boolean = !matchers.matches(request) && processingMatcher.matches(request)
}
return JwtTokenAuthenticationProcessingFilter(requestMatcher, jwtTokenService)
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/org/motivepick/config/VkConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ class VkConfig (

@Value("\${vk.apiVersion}")
val apiVersion: String
): Oauth2Config
): Oauth2Config
16 changes: 8 additions & 8 deletions src/main/kotlin/org/motivepick/security/FacebookService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ import org.springframework.web.util.UriComponentsBuilder
class FacebookService(userService: UserService, tokenService: JwtTokenService, private val config: FacebookConfig,
private val httpClient: RestTemplate) : AbstractTokenGenerator(userService, tokenService, config, httpClient) {

override fun requestProfile(accessToken: TokenResponse): Profile {
val uri = UriComponentsBuilder.fromUriString(config.userInfoUri)
.queryParam("access_token", accessToken.token)
.build().toUri()
val response = httpClient.getForObject(uri, FacebookProfile::class.java)
?: throw AuthenticationServiceException("Could not retrieve profile")
return Profile(response.id, response.name, false)
}
override fun requestProfile(accessToken: TokenResponse): Profile =
UriComponentsBuilder.fromUriString(config.userInfoUri)
.queryParam("access_token", accessToken.token)
.build()
.toUri()
.let { httpClient.getForObject(it, FacebookProfile::class.java) }
?.let { Profile(it.id, it.name, false) }
?: throw AuthenticationServiceException("Could not retrieve profile")

internal data class FacebookProfile(

Expand Down
22 changes: 11 additions & 11 deletions src/main/kotlin/org/motivepick/security/VkService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ import org.springframework.web.util.UriComponentsBuilder
class VkService(userService: UserService, jwtTokenService: JwtTokenService, private val config: VkConfig,
private val httpClient: RestTemplate) : AbstractTokenGenerator(userService, jwtTokenService, config, httpClient) {

override fun requestProfile(accessToken: TokenResponse): Profile {
val uri = UriComponentsBuilder.fromUriString(config.userInfoUri)
.queryParam("access_token", accessToken.token)
.queryParam("user_ids", accessToken.id)
.queryParam("v", config.apiVersion)
.build().toUri()
val response = httpClient.getForObject(uri, VkProfileResponse::class.java)
?: throw AuthenticationServiceException("Could not retrieve profile")
val profile = response.profiles[0]
return Profile(accessToken.id!!, profile.firstName + " " + profile.lastName, false)
}
override fun requestProfile(accessToken: TokenResponse): Profile =
UriComponentsBuilder.fromUriString(config.userInfoUri)
.queryParam("access_token", accessToken.token)
.queryParam("user_ids", accessToken.id)
.queryParam("v", config.apiVersion)
.build()
.toUri()
.let { httpClient.getForObject(it, VkProfileResponse::class.java) }
?.let { it.profiles[0] }
?.let { Profile(accessToken.id!!, it.firstName + " " + it.lastName, false) }
?: throw AuthenticationServiceException("Could not retrieve profile")

internal data class VkProfileResponse(

Expand Down
114 changes: 0 additions & 114 deletions src/main/kotlin/org/motivepick/service/OffsetBasedPageRequest.kt

This file was deleted.

45 changes: 45 additions & 0 deletions src/main/kotlin/org/motivepick/service/OffsetBasedPageable.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package org.motivepick.service

import org.springframework.data.domain.Pageable
import org.springframework.data.domain.Sort

@Suppress("DataClassPrivateConstructor") // Kotlin issue, will be fixed, see KT-11914
internal data class OffsetBasedPageable private constructor(private val offset: Long, private val limit: Int, private val sort: Sort) : Pageable {

companion object {

/**
* Creates a new unsorted [OffsetBasedPageable].
*
* @param offset zero-based offset.
* @param limit the size of the elements to be returned.
*/
operator fun invoke(offset: Long, limit: Int): OffsetBasedPageable {
require(offset >= 0) { "Offset must not be less than zero" }
require(limit >= 1) { "Limit must not be less than one" }
return OffsetBasedPageable(offset, limit, Sort.unsorted())
}
}

override fun getPageNumber(): Int = (offset / limit).toInt()

override fun getPageSize(): Int = limit

override fun getOffset(): Long = offset

override fun getSort(): Sort = sort

override fun next(): Pageable =
OffsetBasedPageable(getOffset() + pageSize, pageSize, getSort())

override fun previousOrFirst(): Pageable =
if (hasPrevious()) OffsetBasedPageable(getOffset() - pageSize, pageSize, getSort()) else first()

override fun first(): Pageable =
OffsetBasedPageable(0, pageSize, getSort())

override fun withPage(pageNumber: Int): Pageable =
OffsetBasedPageable(getOffset() + pageNumber * pageSize, pageSize, getSort())

override fun hasPrevious(): Boolean = offset > limit
}
2 changes: 1 addition & 1 deletion src/main/kotlin/org/motivepick/service/TaskService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ interface TaskService {

fun softDeleteTaskById(taskId: Long): TaskView?

fun findForCurrentUser(listType: TaskListType, offset: Int, limit: Int): Page<TaskView>
fun findForCurrentUser(listType: TaskListType, offset: Long, limit: Int): Page<TaskView>

fun findScheduleForCurrentUser(): ScheduleView

Expand Down
11 changes: 5 additions & 6 deletions src/main/kotlin/org/motivepick/service/TaskServiceImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ internal class TaskServiceImpl(
}

@Transactional
override fun findForCurrentUser(listType: TaskListType, offset: Int, limit: Int): Page<TaskView> {
val pageable: Pageable = OffsetBasedPageRequest(offset, limit)
override fun findForCurrentUser(listType: TaskListType, offset: Long, limit: Int): Page<TaskView> {
val pageable = OffsetBasedPageable(offset, limit)
val accountId = currentUser.getAccountId()
val taskList = taskListRepository.findByUserAccountIdAndType(accountId, listType)
val taskIdsPage = taskList!!.orderedIds.withPageable(pageable)
Expand All @@ -93,11 +93,10 @@ internal class TaskServiceImpl(
}

@Transactional
override fun findScheduleForCurrentUser(): ScheduleView {
val tasks = taskRepository.findAllByUserAccountIdAndClosedFalseAndDueDateNotNullAndVisibleTrue(currentUser.getAccountId())
override fun findScheduleForCurrentUser(): ScheduleView =
taskRepository.findAllByUserAccountIdAndClosedFalseAndDueDateNotNullAndVisibleTrue(currentUser.getAccountId())
.map { it.view() }
return scheduleFactory.scheduleFor(tasks)
}
.let(scheduleFactory::scheduleFor)

@Transactional
override fun createTask(request: CreateTaskRequest): TaskView {
Expand Down
20 changes: 7 additions & 13 deletions src/main/kotlin/org/motivepick/web/TaskController.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package org.motivepick.web

import org.motivepick.domain.view.CreateTaskRequest
import org.motivepick.domain.view.UpdateTaskRequest
import org.motivepick.domain.view.TaskView
import org.motivepick.domain.view.UpdateTaskRequest
import org.motivepick.service.TaskListService
import org.motivepick.service.TaskService
import org.springframework.http.HttpStatus.CREATED
Expand All @@ -19,16 +19,12 @@ internal class TaskController(private val taskService: TaskService, private val
ResponseEntity(taskService.createTask(request), CREATED)

@GetMapping("/tasks/{id}")
fun read(@PathVariable("id") taskId: Long): ResponseEntity<TaskView> {
val task = taskService.findTaskById(taskId)
return if (task == null) notFound().build() else ok(task)
}
fun read(@PathVariable("id") taskId: Long): ResponseEntity<TaskView> =
taskService.findTaskById(taskId)?.let(::ok) ?: notFound().build()

@PutMapping("/tasks/{id}")
fun update(@PathVariable("id") taskId: Long, @RequestBody request: UpdateTaskRequest): ResponseEntity<TaskView> {
val updatedTask = taskService.updateTaskById(taskId, request)
return if (updatedTask == null) notFound().build() else ok(updatedTask)
}
fun update(@PathVariable("id") taskId: Long, @RequestBody request: UpdateTaskRequest): ResponseEntity<TaskView> =
taskService.updateTaskById(taskId, request)?.let(::ok) ?: notFound().build()

@PutMapping("/tasks/{id}/closing")
fun close(@PathVariable("id") taskId: Long): ResponseEntity<TaskView> =
Expand All @@ -43,8 +39,6 @@ internal class TaskController(private val taskService: TaskService, private val
.orElse(notFound().build())

@DeleteMapping("/tasks/{id}")
fun delete(@PathVariable("id") taskId: Long): ResponseEntity<TaskView> {
val deletedTask = taskService.softDeleteTaskById(taskId)
return if (deletedTask == null) notFound().build() else ok(deletedTask)
}
fun delete(@PathVariable("id") taskId: Long): ResponseEntity<TaskView> =
taskService.softDeleteTaskById(taskId)?.let(::ok) ?: notFound().build()
}
2 changes: 1 addition & 1 deletion src/main/kotlin/org/motivepick/web/TaskListController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import org.springframework.web.bind.annotation.*
internal class TaskListController(private val taskService: TaskService, private val taskListService: TaskListService) {

@GetMapping("/task-lists/{type}")
fun read(@PathVariable("type") listType: TaskListType, offset: Int, limit: Int): ResponseEntity<Page<TaskView>> =
fun read(@PathVariable("type") listType: TaskListType, offset: Long, limit: Int): ResponseEntity<Page<TaskView>> =
ok(taskService.findForCurrentUser(listType, offset, limit))

@PostMapping("/orders")
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/org/motivepick/web/UserController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ import org.springframework.web.bind.annotation.RestController
internal class UserController(private val userService: UserService) {

@GetMapping("/user")
fun read(): ResponseEntity<UserView> = userService.readCurrentUser()?.let { ok(it) } ?: notFound().build()
fun read(): ResponseEntity<UserView> = userService.readCurrentUser()?.let(::ok) ?: notFound().build()
}

0 comments on commit 6cedcc4

Please sign in to comment.