From ae949f123f3c2ae874924feff0d8d30f52d0fc82 Mon Sep 17 00:00:00 2001 From: Ryan Harrison Date: Sun, 13 Mar 2022 15:56:24 +0000 Subject: [PATCH] Add new version parameter to update link endpoint --- src/main/kotlin/lynks/entry/LinkResource.kt | 3 +- src/main/kotlin/lynks/entry/LinkService.kt | 4 +- .../kotlin/lynks/resource/LinkResourceTest.kt | 44 +++++++++++++++++++ 3 files changed, 48 insertions(+), 3 deletions(-) diff --git a/src/main/kotlin/lynks/entry/LinkResource.kt b/src/main/kotlin/lynks/entry/LinkResource.kt index 28b4a2c2..893b7b4d 100644 --- a/src/main/kotlin/lynks/entry/LinkResource.kt +++ b/src/main/kotlin/lynks/entry/LinkResource.kt @@ -42,7 +42,8 @@ fun Route.link(linkService: LinkService) { put { val link = call.receive() - val updated = linkService.update(link) + val newVersion = call.parameters["newVersion"]?.let { it.toBoolean() } ?: true + val updated = linkService.update(link, newVersion) if (!checkLink(link)) throw InvalidModelException("Invalid URL") else { if (updated == null) call.respond(HttpStatusCode.NotFound) diff --git a/src/main/kotlin/lynks/entry/LinkService.kt b/src/main/kotlin/lynks/entry/LinkService.kt index 9cab618d..3a440baf 100644 --- a/src/main/kotlin/lynks/entry/LinkService.kt +++ b/src/main/kotlin/lynks/entry/LinkService.kt @@ -60,8 +60,8 @@ class LinkService( return link } - fun update(entry: NewLink): Link? { - return super.update(entry, true)?.also { + override fun update(entry: NewLink, newVersion: Boolean): Link? { + return super.update(entry, newVersion)?.also { workerRegistry.acceptLinkWork(PersistLinkProcessingRequest(it, ResourceType.linkBaseline(), entry.process)) if (entry.process) workerRegistry.acceptDiscussionWork(it.id) diff --git a/src/testIntegration/kotlin/lynks/resource/LinkResourceTest.kt b/src/testIntegration/kotlin/lynks/resource/LinkResourceTest.kt index 4c69643a..bd19da13 100644 --- a/src/testIntegration/kotlin/lynks/resource/LinkResourceTest.kt +++ b/src/testIntegration/kotlin/lynks/resource/LinkResourceTest.kt @@ -311,6 +311,50 @@ class LinkResourceTest: ServerTest() { assertThat(current.dateCreated).isNotEqualTo(current.dateUpdated) } + @Test + fun testUpdateLinkNoNewVersion() { + val newLink = NewLink(null, "title", "google.com", emptyList(), emptyList(), false) + val created = given() + .contentType(ContentType.JSON) + .body(newLink) + .When() + .post("/link") + .then() + .statusCode(201) + .extract().to() + + assertThat(created.version).isOne() + assertThat(created.title).isEqualTo(newLink.title) + assertThat(created.url).isEqualTo(newLink.url) + assertThat(created.dateCreated).isEqualTo(created.dateUpdated) + + // update no new version + val updateLink = NewLink(created.id, "edited", "amazon.com", process = false) + val updated = given() + .contentType(ContentType.JSON) + .body(updateLink) + .When() + .put("/link?newVersion=false") + .then() + .statusCode(200) + .extract().to() + + assertThat(updated.title).isEqualTo(updateLink.title) + assertThat(updated.url).isEqualTo(updateLink.url) + assertThat(updated.version).isOne() + assertThat(updated.dateCreated).isNotEqualTo(updated.dateUpdated) + + // retrieve latest version + val current = get("/link/{id}", created.id) + .then() + .statusCode(200) + .extract().to() + assertThat(current.version).isOne() + assertThat(current.title).isEqualTo(updateLink.title) + assertThat(current.url).isEqualTo(updateLink.url) + assertThat(current.dateCreated).isNotEqualTo(current.dateUpdated) + } + @Test fun testSetReadInvalidLink() { post("/link/{id}/read", "invalid")