11package com.sakethh.linkora.data.repository
22
3+ import com.sakethh.linkora.domain.LWWConflictException
34import com.sakethh.linkora.domain.LinkType
45import com.sakethh.linkora.domain.dto.IDBasedDTO
56import com.sakethh.linkora.domain.dto.NewItemResponseDTO
@@ -23,6 +24,17 @@ import org.jetbrains.exposed.sql.update
2324import java.time.Instant
2425
2526class LinksImplementation : LinksRepository {
27+ private fun checkForLWWConflictAndThrow (id : Long , timeStamp : Long ) {
28+ transaction {
29+ LinksTable .select(LinksTable .lastModified).where {
30+ LinksTable .id.eq(id)
31+ }.let {
32+ if (it.single()[LinksTable .lastModified] > timeStamp) {
33+ throw LWWConflictException ()
34+ }
35+ }
36+ }
37+ }
2638 override suspend fun createANewLink (addLinkDTO : AddLinkDTO ): Result <NewItemResponseDTO > {
2739 return try {
2840 val eventTimestamp = Instant .now().epochSecond
@@ -105,6 +117,10 @@ class LinksImplementation : LinksRepository {
105117
106118 override suspend fun updateLinkedFolderIdOfALink (updateLinkedFolderIDDto : UpdateLinkedFolderIDDto ): Result <TimeStampBasedResponse > {
107119 return try {
120+ checkForLWWConflictAndThrow(
121+ id = updateLinkedFolderIDDto.linkId,
122+ timeStamp = updateLinkedFolderIDDto.eventTimestamp
123+ )
108124 val eventTimestamp = Instant .now().epochSecond
109125 transaction {
110126 LinksTable .update(where = {
@@ -132,6 +148,10 @@ class LinksImplementation : LinksRepository {
132148
133149 override suspend fun updateTitleOfTheLink (updateTitleOfTheLinkDTO : UpdateTitleOfTheLinkDTO ): Result <TimeStampBasedResponse > {
134150 return try {
151+ checkForLWWConflictAndThrow(
152+ id = updateTitleOfTheLinkDTO.linkId,
153+ timeStamp = updateTitleOfTheLinkDTO.eventTimestamp
154+ )
135155 val eventTimestamp = Instant .now().epochSecond
136156 transaction {
137157 LinksTable .update(where = {
@@ -158,6 +178,10 @@ class LinksImplementation : LinksRepository {
158178
159179 override suspend fun updateNote (updateNoteOfALinkDTO : UpdateNoteOfALinkDTO ): Result <TimeStampBasedResponse > {
160180 return try {
181+ checkForLWWConflictAndThrow(
182+ id = updateNoteOfALinkDTO.linkId,
183+ timeStamp = updateNoteOfALinkDTO.eventTimestamp
184+ )
161185 val eventTimestamp = Instant .now().epochSecond
162186 transaction {
163187 LinksTable .update(where = {
@@ -210,6 +234,10 @@ class LinksImplementation : LinksRepository {
210234
211235 override suspend fun archiveALink (idBasedDTO : IDBasedDTO ): Result <TimeStampBasedResponse > {
212236 return try {
237+ checkForLWWConflictAndThrow(
238+ id = idBasedDTO.id,
239+ timeStamp = idBasedDTO.eventTimestamp
240+ )
213241 val eventTimestamp = Instant .now().epochSecond
214242 transaction {
215243 LinksTable .update(where = {
@@ -235,6 +263,10 @@ class LinksImplementation : LinksRepository {
235263
236264 override suspend fun unArchiveALink (idBasedDTO : IDBasedDTO ): Result <TimeStampBasedResponse > {
237265 return try {
266+ checkForLWWConflictAndThrow(
267+ id = idBasedDTO.id,
268+ timeStamp = idBasedDTO.eventTimestamp
269+ )
238270 val eventTimestamp = Instant .now().epochSecond
239271 transaction {
240272 LinksTable .update(where = {
@@ -261,6 +293,10 @@ class LinksImplementation : LinksRepository {
261293
262294 override suspend fun markALinkAsImp (idBasedDTO : IDBasedDTO ): Result <TimeStampBasedResponse > {
263295 return try {
296+ checkForLWWConflictAndThrow(
297+ id = idBasedDTO.id,
298+ timeStamp = idBasedDTO.eventTimestamp
299+ )
264300 val eventTimestamp = Instant .now().epochSecond
265301 transaction {
266302 LinksTable .update(where = {
@@ -286,6 +322,10 @@ class LinksImplementation : LinksRepository {
286322
287323 override suspend fun markALinkAsNonImp (idBasedDTO : IDBasedDTO ): Result <TimeStampBasedResponse > {
288324 return try {
325+ checkForLWWConflictAndThrow(
326+ id = idBasedDTO.id,
327+ timeStamp = idBasedDTO.eventTimestamp
328+ )
289329 val eventTimestamp = Instant .now().epochSecond
290330 transaction {
291331 LinksTable .update(where = {
@@ -311,6 +351,10 @@ class LinksImplementation : LinksRepository {
311351
312352 override suspend fun updateLink (linkDTO : LinkDTO ): Result <TimeStampBasedResponse > {
313353 return try {
354+ checkForLWWConflictAndThrow(
355+ id = linkDTO.id,
356+ timeStamp = linkDTO.eventTimestamp
357+ )
314358 val eventTimestamp = Instant .now().epochSecond
315359 transaction {
316360 LinksTable .update(where = {
0 commit comments