Skip to content

Commit

Permalink
Make possible to reorder tasks manually #14
Browse files Browse the repository at this point in the history
  • Loading branch information
yaskovdev committed Apr 10, 2020
1 parent f3b5b9b commit af7cf69
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 40 deletions.
7 changes: 7 additions & 0 deletions src/main/kotlin/org/motivepick/service/Lists.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.motivepick.service

object Lists {

fun insertWithShift(list: List<Long?>, index: Int, element: Long?): List<Long?> =
list.subList(0, index) + element + list.subList(index, list.size)
}
28 changes: 11 additions & 17 deletions src/main/kotlin/org/motivepick/service/TasksOrderServiceImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,29 @@ import org.motivepick.domain.entity.Task
import org.motivepick.domain.entity.TasksOrderEntity
import org.motivepick.repository.TasksOrderRepository
import org.motivepick.repository.UserRepository
import org.motivepick.service.Lists.insertWithShift
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Service

@Service
class TasksOrderServiceImpl(private val userRepository: UserRepository, private val tasksOrderRepository: TasksOrderRepository) : TasksOrderService {

val logger: Logger = LoggerFactory.getLogger(TasksOrderServiceImpl::class.java)

override fun ordered(accountId: String, tasks: List<Task>): List<Task> {
val order = findTasksOrderForUser(accountId)
return if (order.isEmpty()) {
val ids = tasks.map { it.id }
saveTasksOrderForUser(accountId, ids)
tasks
} else {
val taskToId: Map<Long?, Task> = tasks.map { it.id to it }.toMap() // TODO: add the remaining tasks to the order and write warning
order.map { taskToId[it]!! }
val taskToId: Map<Long?, Task> = tasks.map { it.id to it }.toMap()
val taskIdsNotInOrder = taskToId.keys.subtract(order)
if (taskIdsNotInOrder.isNotEmpty()) {
logger.warn("Tasks {} were not in the order, you forgot to update the order at some point", taskIdsNotInOrder)
}
order.map { taskToId[it]!! } + taskIdsNotInOrder.map { taskToId[it]!! }
}
}

Expand All @@ -38,21 +47,6 @@ class TasksOrderServiceImpl(private val userRepository: UserRepository, private
tasksOrderRepository.save(orderEntity)
}

// TODO: refactor this method
private fun insertWithShift(list: MutableList<Long?>, index: Int, element: Long?): MutableList<Long?> {
val result: MutableList<Long?> = mutableListOf()
for ((i, value) in list.iterator().withIndex()) {
if (i == index) {
result.add(element)
}
result.add(value)
}
if (index == list.size) {
result.add(element)
}
return result
}

private fun findTasksOrderForUser(accountId: String): MutableList<Long?> {
val orderEntity = tasksOrderRepository.findByUserAccountId(accountId)
return orderEntity?.orderedIds?.toMutableList() ?: ArrayList()
Expand Down
28 changes: 28 additions & 0 deletions src/test/kotlin/org/motivepick/service/ListsUnitTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.motivepick.service

import org.junit.Assert.assertEquals
import org.junit.Test
import org.motivepick.service.Lists.insertWithShift

class ListsUnitTest {

@Test
fun shouldCorrectlyInsertElementToEmptyList() {
assertEquals(listOf(1L), insertWithShift(listOf(), 0, 1L))
}

@Test
fun shouldCorrectlyInsertElementToTheBeginning() {
assertEquals(listOf(1L, 2L), insertWithShift(listOf(2L), 0, 1L))
}

@Test
fun shouldCorrectlyInsertElementToTheMiddle() {
assertEquals(listOf(1L, 2L, 3L), insertWithShift(listOf(1L, 3L), 1, 2L))
}

@Test
fun shouldCorrectlyInsertElementToTheEnd() {
assertEquals(listOf(1L, 2L), insertWithShift(listOf(1L), 1, 2L))
}
}

This file was deleted.

0 comments on commit af7cf69

Please sign in to comment.