Skip to content

Commit 90f7015

Browse files
committed
refactor: merge moving of existing folders and links into MOVE_EXISTING_ITEMS
1 parent d6f6ceb commit 90f7015

10 files changed

Lines changed: 65 additions & 60 deletions

File tree

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

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.sakethh.linkora.data.repository
33
import com.sakethh.linkora.domain.Result
44
import com.sakethh.linkora.domain.Route
55
import com.sakethh.linkora.domain.dto.IDBasedDTO
6+
import com.sakethh.linkora.domain.dto.MoveItemsDTO
67
import com.sakethh.linkora.domain.dto.NewItemResponseDTO
78
import com.sakethh.linkora.domain.dto.TimeStampBasedResponse
89
import com.sakethh.linkora.domain.dto.folder.*
@@ -228,39 +229,6 @@ class FoldersRepoImpl(private val panelsRepo: PanelsRepo) : FoldersRepo {
228229
}
229230
}
230231

231-
override suspend fun moveFolders(moveFoldersDTO: MoveFoldersDTO): Result<TimeStampBasedResponse> {
232-
return try {
233-
FoldersTable.checkForLWWConflictAndThrow(
234-
id = moveFoldersDTO.folderIds.last(),
235-
timeStamp = moveFoldersDTO.eventTimestamp,
236-
lastModifiedColumn = FoldersTable.lastModified
237-
)
238-
val eventTimestamp = Instant.now().epochSecond
239-
var rowsUpdated = 0
240-
transaction {
241-
rowsUpdated += FoldersTable.update(where = { FoldersTable.id.inList(moveFoldersDTO.folderIds) }) {
242-
it[lastModified] = eventTimestamp
243-
it[parentFolderID] = moveFoldersDTO.newParentFolderId
244-
}
245-
}
246-
Result.Success(
247-
response = TimeStampBasedResponse(
248-
message = "Number of rows affected by the update = $rowsUpdated",
249-
eventTimestamp = eventTimestamp
250-
), webSocketEvent = WebSocketEvent(
251-
operation = Route.Folder.MOVE_FOLDERS.name,
252-
payload = Json.encodeToJsonElement(
253-
moveFoldersDTO.copy(
254-
eventTimestamp = eventTimestamp
255-
)
256-
),
257-
)
258-
)
259-
} catch (e: Exception) {
260-
Result.Failure(e)
261-
}
262-
}
263-
264232
override suspend fun updateFolderName(updateFolderNameDTO: UpdateFolderNameDTO): Result<TimeStampBasedResponse> {
265233
val folderId = updateFolderNameDTO.folderId
266234
val newFolderName = updateFolderNameDTO.newFolderName

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import com.sakethh.linkora.domain.Result
66
import com.sakethh.linkora.domain.Route
77
import com.sakethh.linkora.domain.dto.ArchiveMultipleItemsDTO
88
import com.sakethh.linkora.domain.dto.IDBasedDTO
9+
import com.sakethh.linkora.domain.dto.MoveItemsDTO
910
import com.sakethh.linkora.domain.dto.TimeStampBasedResponse
1011
import com.sakethh.linkora.domain.model.WebSocketEvent
1112
import com.sakethh.linkora.domain.repository.FoldersRepo
@@ -115,4 +116,52 @@ class MultiActionRepoImpl(
115116
Result.Failure(e)
116117
}
117118
}
119+
120+
override suspend fun moveMultipleItems(moveItemsDTO: MoveItemsDTO): Result<TimeStampBasedResponse> {
121+
return try {
122+
if (moveItemsDTO.folderIds.isNotEmpty()) {
123+
FoldersTable.checkForLWWConflictAndThrow(
124+
id = moveItemsDTO.folderIds.last(),
125+
timeStamp = moveItemsDTO.eventTimestamp,
126+
lastModifiedColumn = FoldersTable.lastModified
127+
)
128+
}
129+
if (moveItemsDTO.linkIds.isNotEmpty()) {
130+
LinksTable.checkForLWWConflictAndThrow(
131+
id = moveItemsDTO.linkIds.last(),
132+
timeStamp = moveItemsDTO.eventTimestamp,
133+
lastModifiedColumn = LinksTable.lastModified
134+
)
135+
}
136+
val eventTimestamp = Instant.now().epochSecond
137+
var rowsUpdated = 0
138+
transaction {
139+
rowsUpdated += LinksTable.update(where = {
140+
LinksTable.id.inList(moveItemsDTO.linkIds)
141+
}) {
142+
it[lastModified] = eventTimestamp
143+
it[idOfLinkedFolder] = moveItemsDTO.newParentFolderId
144+
it[linkType] = moveItemsDTO.linkType.name
145+
}
146+
rowsUpdated += FoldersTable.update(where = { FoldersTable.id.inList(moveItemsDTO.folderIds) }) {
147+
it[lastModified] = eventTimestamp
148+
it[parentFolderID] = moveItemsDTO.newParentFolderId
149+
}
150+
}
151+
Result.Success(
152+
response = TimeStampBasedResponse(
153+
message = "Number of rows affected by the update = $rowsUpdated", eventTimestamp = eventTimestamp
154+
), webSocketEvent = WebSocketEvent(
155+
operation = Route.MultiAction.MOVE_EXISTING_ITEMS.name,
156+
payload = Json.encodeToJsonElement(
157+
moveItemsDTO.copy(
158+
eventTimestamp = eventTimestamp
159+
)
160+
),
161+
)
162+
)
163+
} catch (e: Exception) {
164+
Result.Failure(e)
165+
}
166+
}
118167
}

src/main/kotlin/com/sakethh/linkora/domain/Route.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ package com.sakethh.linkora.domain
22

33
sealed interface Route {
44
enum class Link : Route {
5-
UPDATE_LINK_TITLE, UPDATE_LINK_NOTE, CREATE_A_NEW_LINK, DELETE_A_LINK, UPDATE_LINKED_FOLDER_ID, UPDATE_USER_AGENT, GET_LINKS_FROM_A_FOLDER, GET_LINKS, ARCHIVE_LINK, UNARCHIVE_LINK, MARK_AS_IMP, UNMARK_AS_IMP, UPDATE_LINK, DELETE_DUPLICATE_LINKS, MOVE_LINKS
5+
UPDATE_LINK_TITLE, UPDATE_LINK_NOTE, CREATE_A_NEW_LINK, DELETE_A_LINK, UPDATE_LINKED_FOLDER_ID, UPDATE_USER_AGENT, GET_LINKS_FROM_A_FOLDER, GET_LINKS, ARCHIVE_LINK, UNARCHIVE_LINK, MARK_AS_IMP, UNMARK_AS_IMP, UPDATE_LINK, DELETE_DUPLICATE_LINKS
66
}
77

88
enum class Folder : Route {
9-
CREATE_FOLDER, DELETE_FOLDER, GET_CHILD_FOLDERS, GET_ROOT_FOLDERS, MARK_FOLDER_AS_ARCHIVE, MARK_AS_REGULAR_FOLDER, UPDATE_FOLDER_NAME, UPDATE_FOLDER_NOTE, DELETE_FOLDER_NOTE, MOVE_FOLDERS, MARK_FOLDERS_AS_ROOT
9+
CREATE_FOLDER, DELETE_FOLDER, GET_CHILD_FOLDERS, GET_ROOT_FOLDERS, MARK_FOLDER_AS_ARCHIVE, MARK_AS_REGULAR_FOLDER, UPDATE_FOLDER_NAME, UPDATE_FOLDER_NOTE, DELETE_FOLDER_NOTE, MARK_FOLDERS_AS_ROOT
1010
}
1111

1212
enum class Panel : Route {
@@ -18,6 +18,6 @@ sealed interface Route {
1818
}
1919

2020
enum class MultiAction : Route {
21-
ARCHIVE_MULTIPLE_ITEMS, DELETE_MULTIPLE_ITEMS
21+
ARCHIVE_MULTIPLE_ITEMS, DELETE_MULTIPLE_ITEMS, MOVE_EXISTING_ITEMS
2222
}
2323
}

src/main/kotlin/com/sakethh/linkora/domain/TypeAliases.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.sakethh.linkora.domain
22

33
import com.sakethh.linkora.domain.dto.ArchiveMultipleItemsDTO
4-
import com.sakethh.linkora.domain.dto.link.MoveLinksDTO
54

65
typealias PlaceHolder = String
76

src/main/kotlin/com/sakethh/linkora/domain/dto/link/MoveLinksDTO.kt renamed to src/main/kotlin/com/sakethh/linkora/domain/dto/MoveItemsDTO.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
package com.sakethh.linkora.domain.dto.link
1+
package com.sakethh.linkora.domain.dto
22

33
import com.sakethh.linkora.domain.LinkType
4-
import com.sakethh.linkora.domain.dto.Correlation
54
import kotlinx.serialization.Serializable
65

76
@Serializable
8-
data class MoveLinksDTO(
7+
data class MoveItemsDTO(
8+
val folderIds: List<Long>,
99
val linkIds: List<Long>,
10-
val parentFolderId: Long?,
1110
val linkType: LinkType,
11+
val newParentFolderId: Long,
1212
val correlation: Correlation,
1313
val eventTimestamp: Long
1414
)

src/main/kotlin/com/sakethh/linkora/domain/dto/folder/MoveFoldersDTO.kt

Lines changed: 0 additions & 12 deletions
This file was deleted.

src/main/kotlin/com/sakethh/linkora/domain/repository/FoldersRepo.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import com.sakethh.linkora.domain.dto.TimeStampBasedResponse
66
import com.sakethh.linkora.domain.dto.folder.*
77
import com.sakethh.linkora.domain.model.Folder
88
import com.sakethh.linkora.domain.Result
9+
import com.sakethh.linkora.domain.dto.MoveItemsDTO
910

1011
interface FoldersRepo {
1112
suspend fun createFolder(addFolderDTO: AddFolderDTO): Result<NewItemResponseDTO>
@@ -14,7 +15,6 @@ interface FoldersRepo {
1415
suspend fun getRootFolders(): Result<List<Folder>>
1516
suspend fun markAsArchive(idBasedDTO: IDBasedDTO): Result<TimeStampBasedResponse>
1617
suspend fun markAsRegularFolder(idBasedDTO: IDBasedDTO): Result<TimeStampBasedResponse>
17-
suspend fun moveFolders(moveFoldersDTO: MoveFoldersDTO): Result<TimeStampBasedResponse>
1818
suspend fun updateFolderName(updateFolderNameDTO: UpdateFolderNameDTO): Result<TimeStampBasedResponse>
1919

2020
suspend fun updateFolderNote(updateFolderNoteDTO: UpdateFolderNoteDTO): Result<TimeStampBasedResponse>

src/main/kotlin/com/sakethh/linkora/domain/repository/MultiActionRepo.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ package com.sakethh.linkora.domain.repository
33
import com.sakethh.linkora.domain.DeleteMultipleItemsDTO
44
import com.sakethh.linkora.domain.Result
55
import com.sakethh.linkora.domain.dto.ArchiveMultipleItemsDTO
6+
import com.sakethh.linkora.domain.dto.MoveItemsDTO
67
import com.sakethh.linkora.domain.dto.TimeStampBasedResponse
78

89
interface MultiActionRepo {
910
suspend fun archiveMultipleItems(archiveMultipleItemsDTO: ArchiveMultipleItemsDTO): Result<TimeStampBasedResponse>
1011
suspend fun deleteMultipleItems(deleteMultipleItemsDTO: DeleteMultipleItemsDTO): Result<TimeStampBasedResponse>
12+
suspend fun moveMultipleItems(moveItemsDTO: MoveItemsDTO): Result<TimeStampBasedResponse>
1113
}

src/main/kotlin/com/sakethh/linkora/presentation/routing/http/FoldersRouting.kt

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.sakethh.linkora.presentation.routing.http
33
import com.sakethh.linkora.Security
44
import com.sakethh.linkora.domain.Route
55
import com.sakethh.linkora.domain.dto.IDBasedDTO
6+
import com.sakethh.linkora.domain.dto.MoveItemsDTO
67
import com.sakethh.linkora.domain.dto.folder.*
78
import com.sakethh.linkora.domain.repository.FoldersRepo
89
import com.sakethh.linkora.utils.respondWithResult
@@ -37,12 +38,6 @@ fun Application.foldersRouting(foldersRepo: FoldersRepo) {
3738
respondWithResult(foldersRepo.markAsRegularFolder(it))
3839
}
3940

40-
post<MoveFoldersDTO>(Route.Folder.MOVE_FOLDERS.name) {
41-
respondWithResult(
42-
foldersRepo.moveFolders(it)
43-
)
44-
}
45-
4641
post<UpdateFolderNameDTO>(Route.Folder.UPDATE_FOLDER_NAME.name) {
4742
respondWithResult(
4843
foldersRepo.updateFolderName(it)

src/main/kotlin/com/sakethh/linkora/presentation/routing/http/MultiActionRouting.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import com.sakethh.linkora.Security
44
import com.sakethh.linkora.domain.DeleteMultipleItemsDTO
55
import com.sakethh.linkora.domain.Route
66
import com.sakethh.linkora.domain.dto.ArchiveMultipleItemsDTO
7+
import com.sakethh.linkora.domain.dto.MoveItemsDTO
78
import com.sakethh.linkora.domain.repository.MultiActionRepo
89
import com.sakethh.linkora.utils.respondWithResult
910
import io.ktor.server.application.*
@@ -19,6 +20,9 @@ fun Application.multiActionRouting(multiActionRepo: MultiActionRepo) {
1920
post<DeleteMultipleItemsDTO>(Route.MultiAction.DELETE_MULTIPLE_ITEMS.name) {
2021
respondWithResult(multiActionRepo.deleteMultipleItems(it))
2122
}
23+
post<MoveItemsDTO>(Route.MultiAction.MOVE_EXISTING_ITEMS.name) {
24+
respondWithResult(multiActionRepo.moveMultipleItems(it))
25+
}
2226
}
2327
}
2428
}

0 commit comments

Comments
 (0)