@@ -18,11 +18,9 @@ import kotlinx.coroutines.coroutineScope
1818import kotlinx.serialization.encodeToString
1919import kotlinx.serialization.json.Json
2020import kotlinx.serialization.json.encodeToJsonElement
21+ import org.jetbrains.exposed.sql.*
2122import org.jetbrains.exposed.sql.SqlExpressionBuilder.inList
22- import org.jetbrains.exposed.sql.and
23- import org.jetbrains.exposed.sql.deleteWhere
2423import org.jetbrains.exposed.sql.transactions.transaction
25- import org.jetbrains.exposed.sql.update
2624import java.time.Instant
2725
2826class 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 }
0 commit comments