Skip to content

Commit 3d0b487

Browse files
committed
feat: add support to copy folders (partial)
- doesn't return copied folder ids yet, will add that later.
1 parent f5dc287 commit 3d0b487

3 files changed

Lines changed: 75 additions & 15 deletions

File tree

src/main/kotlin/com/sakethh/linkora/data/repository/MultiActionRepoImpl.kt

Lines changed: 58 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,9 @@ import kotlinx.coroutines.coroutineScope
1818
import kotlinx.serialization.encodeToString
1919
import kotlinx.serialization.json.Json
2020
import kotlinx.serialization.json.encodeToJsonElement
21+
import org.jetbrains.exposed.sql.*
2122
import org.jetbrains.exposed.sql.SqlExpressionBuilder.inList
22-
import org.jetbrains.exposed.sql.and
23-
import org.jetbrains.exposed.sql.deleteWhere
2423
import org.jetbrains.exposed.sql.transactions.transaction
25-
import org.jetbrains.exposed.sql.update
2624
import java.time.Instant
2725

2826
class MultiActionRepoImpl(
@@ -167,7 +165,7 @@ class MultiActionRepoImpl(
167165
return try {
168166
val eventTimestamp = Instant.now().epochSecond
169167
lateinit var linkIds: List<Long>
170-
168+
println("Remote Links : ${copyItemsDTO.linkIds.values}")
171169
// copy the links based on `copyItemsDTO.linkIds`
172170
transaction {
173171
this.exec(
@@ -184,16 +182,63 @@ class MultiActionRepoImpl(
184182
}.map { resultRow ->
185183
resultRow[LinksTable.id].value
186184
}.toList()
185+
186+
187+
// initially, we'll insert the root folders
188+
lateinit var rootFolderIds: List<Long>
189+
FoldersTable.selectAll().where {
190+
FoldersTable.id.inList(copyItemsDTO.folders.map { it.currentFolder.remoteId })
191+
}.toList().let { sourceRootFolders ->
192+
sourceRootFolders.forEach {
193+
println(it[FoldersTable.folderName] + ", " + it[FoldersTable.note] + ", " + it[FoldersTable.lastModified])
194+
}
195+
val eventTimestamp = Instant.now().epochSecond
196+
println("Index:")
197+
rootFolderIds = FoldersTable.batchInsert(sourceRootFolders) {
198+
this[FoldersTable.folderName] = it[FoldersTable.folderName]
199+
this[FoldersTable.lastModified] = eventTimestamp
200+
this[FoldersTable.note] = it[FoldersTable.note]
201+
this[FoldersTable.parentFolderID] = copyItemsDTO.newParentFolderId
202+
this[FoldersTable.isFolderArchived] = it[FoldersTable.isFolderArchived]
203+
}.toList().map { it[FoldersTable.id].value }
204+
}
205+
206+
207+
fun insertChildFolders(parentFolderId: Long, childFolders: List<CopyFolderDTO>) {
208+
FoldersTable.selectAll().where {
209+
FoldersTable.id.inList(childFolders.map { it.currentFolder.remoteId })
210+
}.toList().let { sourceRootFolders ->
211+
val eventTimestamp = Instant.now().epochSecond
212+
FoldersTable.batchInsert(sourceRootFolders) {
213+
this[FoldersTable.folderName] = it[FoldersTable.folderName]
214+
this[FoldersTable.lastModified] = eventTimestamp
215+
this[FoldersTable.note] = it[FoldersTable.note]
216+
this[FoldersTable.parentFolderID] = parentFolderId
217+
this[FoldersTable.isFolderArchived] = it[FoldersTable.isFolderArchived]
218+
}.toList().forEachIndexed { resultRowIndex, resultRow ->
219+
insertChildFolders(
220+
resultRow[FoldersTable.id].value, childFolders[resultRowIndex].childFolders
221+
)
222+
}
223+
}
224+
}
225+
copyItemsDTO.folders.forEachIndexed { index, parentFolder ->
226+
insertChildFolders(
227+
rootFolderIds[index], parentFolder.childFolders
228+
)
229+
}
230+
231+
}.let {
232+
Result.Success(
233+
response = CopyItemsResponseDTO(
234+
folders = TODO(), linkIds = copyItemsDTO.linkIds.run {
235+
this.toList().mapIndexed { index, pair ->
236+
pair.first to linkIds[index]
237+
}.toMap()
238+
}, correlation = copyItemsDTO.correlation, eventTimestamp = eventTimestamp
239+
), webSocketEvent = null
240+
)
187241
}
188-
Result.Success(
189-
response = CopyItemsResponseDTO(
190-
folderIds = copyItemsDTO.folderIds, linkIds = copyItemsDTO.linkIds.run {
191-
this.toList().mapIndexed { index, pair ->
192-
pair.first to linkIds[index]
193-
}.toMap()
194-
}, correlation = copyItemsDTO.correlation, eventTimestamp = eventTimestamp
195-
), webSocketEvent = null
196-
)
197242
} catch (e: Exception) {
198243
Result.Failure(e)
199244
}

src/main/kotlin/com/sakethh/linkora/domain/dto/CopyItemsDTO.kt

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,22 @@ import kotlinx.serialization.Serializable
55

66
@Serializable
77
data class CopyItemsDTO(
8-
val folderIds: Map<Long, Long>, // `key` belongs to the client, `value` belongs to this server's db
8+
val folders: List<CopyFolderDTO>,
99
val linkIds: Map<Long, Long>,// `key` belongs to the client, `value` belongs to this server's db
1010
val linkType: LinkType, val newParentFolderId: Long, val correlation: Correlation, val eventTimestamp: Long
1111
)
12+
13+
14+
@Serializable
15+
data class CopyFolderDTO(
16+
val currentFolder: CurrentFolder,
17+
val links: List<FolderLink>,
18+
val childFolders: List<CopyFolderDTO>
19+
)
20+
21+
@Serializable
22+
data class CurrentFolder(
23+
val localId: Long, val remoteId: Long
24+
)
25+
26+
typealias FolderLink = CurrentFolder

src/main/kotlin/com/sakethh/linkora/domain/dto/CopyItemsResponseDTO.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import kotlinx.serialization.Serializable
44

55
@Serializable
66
data class CopyItemsResponseDTO(
7-
val folderIds: Map<Long, Long>,
7+
val folders: List<CopyFolderDTO>,
88
val linkIds: Map<Long, Long>,
99
val correlation: Correlation,
1010
val eventTimestamp: Long

0 commit comments

Comments
 (0)