Skip to content

Commit b3b4e81

Browse files
committed
feat: implement duplicate link deletion
1 parent 9ce9516 commit b3b4e81

5 files changed

Lines changed: 41 additions & 5 deletions

File tree

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

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,10 @@ import com.sakethh.linkora.utils.Result
1515
import kotlinx.serialization.encodeToString
1616
import kotlinx.serialization.json.Json
1717
import kotlinx.serialization.json.encodeToJsonElement
18+
import org.jetbrains.exposed.sql.*
1819
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
19-
import org.jetbrains.exposed.sql.and
20-
import org.jetbrains.exposed.sql.deleteWhere
21-
import org.jetbrains.exposed.sql.insertAndGetId
20+
import org.jetbrains.exposed.sql.transactions.TransactionManager
2221
import org.jetbrains.exposed.sql.transactions.transaction
23-
import org.jetbrains.exposed.sql.update
2422
import java.time.Instant
2523

2624
class LinksImplementation : LinksRepository {
@@ -387,4 +385,27 @@ class LinksImplementation : LinksRepository {
387385
Result.Failure(e)
388386
}
389387
}
388+
389+
override suspend fun deleteDuplicateLinks(deleteDuplicateLinksDTO: DeleteDuplicateLinksDTO): Result<TimeStampBasedResponse> {
390+
return try {
391+
val eventTimestamp = Instant.now().epochSecond
392+
transaction {
393+
this.exec(
394+
"DELETE FROM links_table WHERE id IN (${
395+
deleteDuplicateLinksDTO.linkIds.toString().substringBefore("]").substringAfter("[").trim()
396+
})"
397+
)
398+
}
399+
Result.Success(
400+
response = TimeStampBasedResponse(
401+
eventTimestamp = eventTimestamp, message = "Deleted Duplicate links."
402+
), webSocketEvent = WebSocketEvent(
403+
operation = LinkRoute.DELETE_DUPLICATE_LINKS.name,
404+
payload = Json.encodeToJsonElement(deleteDuplicateLinksDTO.copy(eventTimestamp = eventTimestamp))
405+
)
406+
)
407+
} catch (e: Exception) {
408+
Result.Failure(e)
409+
}
410+
}
390411
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.sakethh.linkora.domain.dto.link
2+
3+
import com.sakethh.linkora.domain.dto.Correlation
4+
import kotlinx.serialization.Serializable
5+
6+
@Serializable
7+
data class DeleteDuplicateLinksDTO(
8+
val linkIds: List<Long>, val correlation: Correlation, val eventTimestamp: Long
9+
)

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@ interface LinksRepository {
1818
suspend fun markALinkAsImp(idBasedDTO: IDBasedDTO): Result<TimeStampBasedResponse>
1919
suspend fun markALinkAsNonImp(idBasedDTO: IDBasedDTO): Result<TimeStampBasedResponse>
2020
suspend fun updateLink(linkDTO: LinkDTO): Result<TimeStampBasedResponse>
21+
suspend fun deleteDuplicateLinks(deleteDuplicateLinksDTO: DeleteDuplicateLinksDTO): Result<TimeStampBasedResponse>
2122
}

src/main/kotlin/com/sakethh/linkora/domain/routes/LinkRoute.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@ enum class LinkRoute {
1313
UNARCHIVE_LINK,
1414
MARK_AS_IMP,
1515
UNMARK_AS_IMP,
16-
UPDATE_LINK
16+
UPDATE_LINK,
17+
DELETE_DUPLICATE_LINKS
1718
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ fun Application.linksRouting(linksRepository: LinksRepository) {
6464
post<LinkDTO>(LinkRoute.UPDATE_LINK.name) {
6565
respondWithResult(linksRepository.updateLink(it))
6666
}
67+
68+
post<DeleteDuplicateLinksDTO>(LinkRoute.DELETE_DUPLICATE_LINKS.name) {
69+
respondWithResult(linksRepository.deleteDuplicateLinks(it))
70+
}
6771
}
6872
}
6973
}

0 commit comments

Comments
 (0)