Skip to content

Commit

Permalink
Add new version parameter to update link endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
raharrison committed Mar 13, 2022
1 parent 0f4ae0d commit ae949f1
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/main/kotlin/lynks/entry/LinkResource.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ fun Route.link(linkService: LinkService) {

put {
val link = call.receive<NewLink>()
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)
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/lynks/entry/LinkService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
44 changes: 44 additions & 0 deletions src/testIntegration/kotlin/lynks/resource/LinkResourceTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<Link>()

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<Link>()

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<Link>()
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")
Expand Down

0 comments on commit ae949f1

Please sign in to comment.