Skip to content

Commit e9442d1

Browse files
committed
fix: include copied folder IDs with their link IDs in the response
1 parent 3d0b487 commit e9442d1

3 files changed

Lines changed: 123 additions & 31 deletions

File tree

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

Lines changed: 87 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,19 @@ import com.sakethh.linkora.domain.tables.FoldersTable
1212
import com.sakethh.linkora.domain.tables.LinksTable
1313
import com.sakethh.linkora.domain.tables.helper.TombStoneHelper
1414
import com.sakethh.linkora.utils.checkForLWWConflictAndThrow
15+
import com.sakethh.linkora.utils.copy
1516
import kotlinx.coroutines.async
1617
import kotlinx.coroutines.awaitAll
1718
import kotlinx.coroutines.coroutineScope
1819
import kotlinx.serialization.encodeToString
1920
import kotlinx.serialization.json.Json
2021
import kotlinx.serialization.json.encodeToJsonElement
21-
import org.jetbrains.exposed.sql.*
2222
import org.jetbrains.exposed.sql.SqlExpressionBuilder.inList
23+
import org.jetbrains.exposed.sql.and
24+
import org.jetbrains.exposed.sql.deleteWhere
25+
import org.jetbrains.exposed.sql.selectAll
2326
import org.jetbrains.exposed.sql.transactions.transaction
27+
import org.jetbrains.exposed.sql.update
2428
import java.time.Instant
2529

2630
class MultiActionRepoImpl(
@@ -167,6 +171,7 @@ class MultiActionRepoImpl(
167171
lateinit var linkIds: List<Long>
168172
println("Remote Links : ${copyItemsDTO.linkIds.values}")
169173
// copy the links based on `copyItemsDTO.linkIds`
174+
val copiedFolderResponse = mutableListOf<CopiedFolderResponse>()
170175
transaction {
171176
this.exec(
172177
"""
@@ -184,54 +189,106 @@ class MultiActionRepoImpl(
184189
}.toList()
185190

186191

192+
187193
// initially, we'll insert the root folders
188-
lateinit var rootFolderIds: List<Long>
189-
FoldersTable.selectAll().where {
194+
lateinit var copiedRootFolderIds: List<Long>
195+
val sourceRootFolders = FoldersTable.selectAll().where {
190196
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])
197+
}.toList()
198+
val eventTimestamp = Instant.now().epochSecond
199+
200+
copiedRootFolderIds = FoldersTable.copy(
201+
source = sourceRootFolders,
202+
eventTimestamp = eventTimestamp,
203+
parentFolderId = copyItemsDTO.newParentFolderId
204+
).mapIndexed { index, resultRow ->
205+
resultRow[FoldersTable.id].value.run {
206+
copiedFolderResponse.add(
207+
CopiedFolderResponse(
208+
currentFolder = CurrentFolder(
209+
localId = copyItemsDTO.folders[index].currentFolder.localId, remoteId = this
210+
), links = emptyList()
211+
)
212+
)
213+
this
194214
}
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 }
204215
}
205216

217+
// insert links of root folders
218+
copyItemsDTO.folders.map { it.currentFolder.remoteId }.forEachIndexed { folderIndex, folderId ->
219+
val sourceLinksOfRootFolder = LinksTable.selectAll().where {
220+
LinksTable.idOfLinkedFolder.eq(folderId)
221+
}.toList()
222+
val eventTimestamp = Instant.now().epochSecond
223+
LinksTable.copy(
224+
source = sourceLinksOfRootFolder,
225+
eventTimestamp = eventTimestamp,
226+
parentFolderId = copiedRootFolderIds[folderIndex]
227+
).mapIndexed { linkResultRowIndex, resultRow ->
228+
FolderLink(
229+
localId = copyItemsDTO.folders[folderIndex].links[linkResultRowIndex].localId,
230+
remoteId = resultRow[LinksTable.id].value
231+
)
232+
}.let {
233+
copiedFolderResponse[folderIndex] = copiedFolderResponse[folderIndex].copy(
234+
links = it
235+
)
236+
}
237+
}
206238

207-
fun insertChildFolders(parentFolderId: Long, childFolders: List<CopyFolderDTO>) {
208-
FoldersTable.selectAll().where {
239+
fun insertChildFolders(
240+
parentFolderId: Long,
241+
childFolders: List<CopyFolderDTO>,
242+
) {
243+
val sourceFolders = FoldersTable.selectAll().where {
209244
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 ->
245+
}.toList()
246+
val eventTimestamp = Instant.now().epochSecond
247+
FoldersTable.copy(source = sourceFolders, eventTimestamp, parentFolderId)
248+
.forEachIndexed { insertedFolderRowIndex, insertedFolderRow ->
249+
val newParentFolderId = insertedFolderRow[FoldersTable.id].value
250+
val sourceParentFolderId = sourceFolders[insertedFolderRowIndex][FoldersTable.id].value
251+
252+
LinksTable.selectAll().where {
253+
LinksTable.idOfLinkedFolder.eq(sourceParentFolderId)
254+
}.let {
255+
LinksTable.copy(
256+
source = it.toList(),
257+
eventTimestamp = eventTimestamp,
258+
parentFolderId = newParentFolderId
259+
).mapIndexed { insertedLinkRowIndex, insertedLinkRow ->
260+
FolderLink(
261+
localId = childFolders[insertedFolderRowIndex].links[insertedLinkRowIndex].localId,
262+
remoteId = insertedLinkRow[LinksTable.id].value
263+
)
264+
}.let {
265+
copiedFolderResponse.add(
266+
CopiedFolderResponse(
267+
currentFolder = CurrentFolder(
268+
localId = childFolders[insertedFolderRowIndex].currentFolder.localId,
269+
remoteId = newParentFolderId
270+
), links = it
271+
)
272+
)
273+
}
274+
}
219275
insertChildFolders(
220-
resultRow[FoldersTable.id].value, childFolders[resultRowIndex].childFolders
276+
newParentFolderId, childFolders[insertedFolderRowIndex].childFolders
221277
)
222278
}
223-
}
224279
}
280+
281+
// insert child folders
225282
copyItemsDTO.folders.forEachIndexed { index, parentFolder ->
226283
insertChildFolders(
227-
rootFolderIds[index], parentFolder.childFolders
284+
copiedRootFolderIds[index], parentFolder.childFolders
228285
)
229286
}
230287

231288
}.let {
232289
Result.Success(
233290
response = CopyItemsResponseDTO(
234-
folders = TODO(), linkIds = copyItemsDTO.linkIds.run {
291+
folders = copiedFolderResponse.toList(), linkIds = copyItemsDTO.linkIds.run {
235292
this.toList().mapIndexed { index, pair ->
236293
pair.first to linkIds[index]
237294
}.toMap()

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,13 @@ import kotlinx.serialization.Serializable
44

55
@Serializable
66
data class CopyItemsResponseDTO(
7-
val folders: List<CopyFolderDTO>,
7+
val folders: List<CopiedFolderResponse>,
88
val linkIds: Map<Long, Long>,
99
val correlation: Correlation,
1010
val eventTimestamp: Long
1111
)
12+
13+
@Serializable
14+
data class CopiedFolderResponse(
15+
val currentFolder: CurrentFolder, val links: List<FolderLink>
16+
)

src/main/kotlin/com/sakethh/linkora/utils/HelperFunctions.kt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
package com.sakethh.linkora.utils
22

33
import com.sakethh.linkora.domain.LWWConflictException
4+
import com.sakethh.linkora.domain.tables.FoldersTable
5+
import com.sakethh.linkora.domain.tables.LinksTable
46
import org.jetbrains.exposed.dao.id.LongIdTable
57
import org.jetbrains.exposed.sql.Column
8+
import org.jetbrains.exposed.sql.ResultRow
9+
import org.jetbrains.exposed.sql.batchInsert
610
import org.jetbrains.exposed.sql.transactions.transaction
711

812
fun useSysEnvValues(): Boolean {
@@ -31,4 +35,30 @@ fun LongIdTable.checkForLWWConflictAndThrow(id: Long, timeStamp: Long, lastModif
3135
}
3236
}
3337
}
38+
}
39+
40+
fun FoldersTable.copy(source: List<ResultRow>, eventTimestamp: Long, parentFolderId: Long?): List<ResultRow> {
41+
return batchInsert(source) {
42+
this[folderName] = it[folderName]
43+
this[lastModified] = eventTimestamp
44+
this[note] = it[note]
45+
this[parentFolderID] = parentFolderId
46+
this[isFolderArchived] = it[isFolderArchived]
47+
}.toList()
48+
}
49+
50+
fun LinksTable.copy(source: List<ResultRow>, eventTimestamp: Long, parentFolderId: Long?): List<ResultRow> {
51+
return batchInsert(source) {
52+
set(lastModified, eventTimestamp)
53+
set(linkType, it[linkType])
54+
set(linkTitle, it[linkTitle])
55+
set(url, it[url])
56+
set(baseURL, it[baseURL])
57+
set(imgURL, it[imgURL])
58+
set(note, it[note])
59+
set(idOfLinkedFolder, parentFolderId)
60+
set(userAgent, it[userAgent])
61+
set(mediaType, it[mediaType])
62+
set(markedAsImportant, it[markedAsImportant])
63+
}
3464
}

0 commit comments

Comments
 (0)