Skip to content

Commit

Permalink
Make possible to reorder tasks manually #14
Browse files Browse the repository at this point in the history
- Draft implementation with database.
  • Loading branch information
yaskovdev committed Apr 9, 2020
1 parent 1405eaa commit f3b5b9b
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 15 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@
<artifactId>postgresql</artifactId>
<version>${postgresql.version}</version>
</dependency>
<dependency>
<groupId>com.vladmihalcea</groupId>
<artifactId>hibernate-types-52</artifactId>
<version>2.9.7</version>
</dependency>
<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
Expand Down
15 changes: 15 additions & 0 deletions src/main/kotlin/org/motivepick/domain/entity/TasksOrderEntity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.motivepick.domain.entity

import com.vladmihalcea.hibernate.type.array.ListArrayType
import org.hibernate.annotations.Type
import org.hibernate.annotations.TypeDef
import javax.persistence.Column
import javax.persistence.Entity
import javax.persistence.FetchType.LAZY
import javax.persistence.JoinColumn
import javax.persistence.ManyToOne

@Entity(name = "TASKS_ORDER")
@TypeDef(name = "ListArray", typeClass = ListArrayType::class)
class TasksOrderEntity(@ManyToOne(fetch = LAZY) @JoinColumn(name = "USER_ID", nullable = false) var user: User,
@Type(type = "ListArray") @Column(nullable = false, columnDefinition = "BIGINT[]") var orderedIds: List<Long?>) : AbstractEntity()
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.motivepick.repository

import org.motivepick.domain.entity.TasksOrderEntity
import org.springframework.data.repository.PagingAndSortingRepository

interface TasksOrderRepository : PagingAndSortingRepository<TasksOrderEntity, Long> {

fun findByUserAccountId(accountId: String): TasksOrderEntity?
}
28 changes: 16 additions & 12 deletions src/main/kotlin/org/motivepick/service/TasksOrderServiceImpl.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package org.motivepick.service

import org.motivepick.domain.entity.Task
import org.motivepick.domain.entity.TasksOrderEntity
import org.motivepick.repository.TasksOrderRepository
import org.motivepick.repository.UserRepository
import org.springframework.stereotype.Service

val ACCOUNT_ID_TO_ORDER = mutableMapOf<String, MutableList<Long?>>()

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

override fun ordered(accountId: String, tasks: List<Task>): List<Task> {
val order = findTasksOrderForUser(accountId)
Expand All @@ -22,15 +23,19 @@ class TasksOrderServiceImpl : TasksOrderService {

// TODO: if you update a page very quickly after move, it will read tasks faster than the order was saved (probably because of the map, not database)
override fun moveTask(accountId: String, sourceId: Long, destinationId: Long) {
val order = ACCOUNT_ID_TO_ORDER[accountId]!!
val orderEntity = tasksOrderRepository.findByUserAccountId(accountId)!!
val order = orderEntity.orderedIds.toMutableList()
val destinationIndex = order.indexOf(destinationId)
order.remove(sourceId)
ACCOUNT_ID_TO_ORDER[accountId] = insertWithShift(order, destinationIndex, sourceId)
orderEntity.orderedIds = insertWithShift(order, destinationIndex, sourceId)
tasksOrderRepository.save(orderEntity)
}

override fun addTask(accountId: String, taskId: Long) {
val order = ACCOUNT_ID_TO_ORDER[accountId]!!
ACCOUNT_ID_TO_ORDER[accountId] = insertWithShift(order, 0, taskId)
val orderEntity = tasksOrderRepository.findByUserAccountId(accountId)!!
val order = orderEntity.orderedIds.toMutableList()
orderEntity.orderedIds = insertWithShift(order, 0, taskId)
tasksOrderRepository.save(orderEntity)
}

// TODO: refactor this method
Expand All @@ -49,13 +54,12 @@ class TasksOrderServiceImpl : TasksOrderService {
}

private fun findTasksOrderForUser(accountId: String): MutableList<Long?> {
if (!ACCOUNT_ID_TO_ORDER.containsKey(accountId)) {
ACCOUNT_ID_TO_ORDER[accountId] = ArrayList()
}
return ACCOUNT_ID_TO_ORDER[accountId]!!
val orderEntity = tasksOrderRepository.findByUserAccountId(accountId)
return orderEntity?.orderedIds?.toMutableList() ?: ArrayList()
}

private fun saveTasksOrderForUser(accountId: String, order: List<Long?>) {
ACCOUNT_ID_TO_ORDER[accountId] = order.toMutableList()
val user = userRepository.findByAccountId(accountId)
tasksOrderRepository.save(TasksOrderEntity(user!!, order))
}
}
4 changes: 2 additions & 2 deletions src/main/kotlin/org/motivepick/web/ScheduleFactory.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import org.springframework.stereotype.Component
import java.time.Clock
import java.time.LocalDate
import java.time.LocalDateTime
import java.time.LocalTime
import java.time.LocalTime.MAX
import java.time.format.DateTimeFormatter
import java.util.*
import kotlin.collections.LinkedHashMap
Expand Down Expand Up @@ -45,7 +45,7 @@ class ScheduleFactory(private val clock: Clock) {

private fun week(): MutableMap<LocalDateTime, List<Task>> {
val schedule: MutableMap<LocalDateTime, List<Task>> = LinkedHashMap()
val endOfToday = LocalDate.now(clock).atTime(LocalTime.MAX)
val endOfToday = LocalDate.now(clock).atTime(MAX)
for (i in 0..6) {
schedule[endOfToday.plusDays(i.toLong())] = ArrayList()
}
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/application-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ spring.datasource:
username: postgres
password: postgres

spring.jpa.show-sql: true
spring.jpa.properties.hibernate.format_sql: true

enforce.https.for.oauth: false

facebook:
Expand Down
22 changes: 22 additions & 0 deletions src/main/resources/db/changelog/006.tasks_order.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.5.xsd">

<changeSet id="202004092347" author="Sergey Yaskov">
<createTable tableName="TASKS_ORDER">
<column name="ID" autoIncrement="true" type="BIGINT">
<constraints primaryKey="true" primaryKeyName="PK__ID__TASKS_ORDER"/>
</column>

<column name="USER_ID" type="BIGINT">
<constraints nullable="false" foreignKeyName="FK__TASKS_ORDER__USER_ID__USER_ACCOUNT__ID"
references="USER_ACCOUNT(ID)"/>
</column>

<column name="ORDERED_IDS" type="BIGINT[]">
<constraints nullable="false"/>
</column>
</createTable>
</changeSet>
</databaseChangeLog>
1 change: 1 addition & 0 deletions src/main/resources/db/changelog/db.changelog-master.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@
<include file="db/changelog/003.task.closing_date.column.xml"/>
<include file="db/changelog/004.task.visible.column.xml"/>
<include file="db/changelog/005.user_account.xml"/>
<include file="db/changelog/006.tasks_order.xml"/>

</databaseChangeLog>
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package org.motivepick.service

import org.junit.Test

class TasksOrderServiceImplTest {
class TasksOrderServiceImplUnitTest {

@Test
fun test() {
Expand Down

0 comments on commit f3b5b9b

Please sign in to comment.