-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Arnau Mora <arnyminerz@proton.me>
- Loading branch information
1 parent
f5ad728
commit 1343364
Showing
11 changed files
with
184 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
...m/arnyminerz/escalaralcoiaicomtat/backend/server/endpoints/blocking/PatchBlockEndpoint.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package com.arnyminerz.escalaralcoiaicomtat.backend.server.endpoints.blocking | ||
|
||
import com.arnyminerz.escalaralcoiaicomtat.backend.ServerDatabase | ||
import com.arnyminerz.escalaralcoiaicomtat.backend.data.BlockingRecurrenceYearly | ||
import com.arnyminerz.escalaralcoiaicomtat.backend.data.BlockingTypes | ||
import com.arnyminerz.escalaralcoiaicomtat.backend.database.entity.Blocking | ||
import com.arnyminerz.escalaralcoiaicomtat.backend.server.endpoints.EndpointBase | ||
import com.arnyminerz.escalaralcoiaicomtat.backend.server.error.Errors | ||
import com.arnyminerz.escalaralcoiaicomtat.backend.server.response.respondFailure | ||
import com.arnyminerz.escalaralcoiaicomtat.backend.server.response.respondSuccess | ||
import com.arnyminerz.escalaralcoiaicomtat.backend.utils.areAllNull | ||
import com.arnyminerz.escalaralcoiaicomtat.backend.utils.getEnumOrNull | ||
import com.arnyminerz.escalaralcoiaicomtat.backend.utils.getJSONObjectOrNull | ||
import com.arnyminerz.escalaralcoiaicomtat.backend.utils.getStringOrNull | ||
import com.arnyminerz.escalaralcoiaicomtat.backend.utils.json | ||
import com.arnyminerz.escalaralcoiaicomtat.backend.utils.jsonOf | ||
import io.ktor.http.HttpStatusCode | ||
import io.ktor.server.application.ApplicationCall | ||
import io.ktor.server.application.call | ||
import io.ktor.server.request.receiveText | ||
import io.ktor.server.util.getValue | ||
import io.ktor.util.pipeline.PipelineContext | ||
import java.time.LocalDateTime | ||
|
||
object PatchBlockEndpoint: EndpointBase() { | ||
override suspend fun PipelineContext<Unit, ApplicationCall>.endpoint() { | ||
val blockId: Int by call.parameters | ||
|
||
val block: Blocking = ServerDatabase.instance.query { Blocking.findById(blockId) } | ||
?: return respondFailure(Errors.ObjectNotFound) | ||
|
||
val body = call.receiveText().json | ||
val type = body.getEnumOrNull(BlockingTypes::class, "type") | ||
val recurrence = body.getJSONObjectOrNull("recurrence")?.let(BlockingRecurrenceYearly::fromJson) | ||
val endDate = body.getStringOrNull("end_date")?.let(LocalDateTime::parse) | ||
|
||
if (areAllNull(type, recurrence, endDate)) { | ||
return respondSuccess(httpStatusCode = HttpStatusCode.NoContent) | ||
} | ||
|
||
println("Updating block. Type=$type, recurrence=$recurrence, endDate=$endDate") | ||
|
||
val updatedBlock = ServerDatabase.instance.query { | ||
type?.let { block.type = it } | ||
recurrence?.let { block.recurrence = it } | ||
endDate?.let { block.endDate = it } | ||
|
||
block.toJson() | ||
} | ||
|
||
respondSuccess( | ||
jsonOf("element" to updatedBlock) | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
.../arnyminerz/escalaralcoiaicomtat/backend/server/endpoints/block/TestPatchBlockEndpoint.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package com.arnyminerz.escalaralcoiaicomtat.backend.server.endpoints.block | ||
|
||
import com.arnyminerz.escalaralcoiaicomtat.backend.assertions.assertSuccess | ||
import com.arnyminerz.escalaralcoiaicomtat.backend.data.BlockingRecurrenceYearly | ||
import com.arnyminerz.escalaralcoiaicomtat.backend.data.BlockingTypes | ||
import com.arnyminerz.escalaralcoiaicomtat.backend.server.DataProvider | ||
import com.arnyminerz.escalaralcoiaicomtat.backend.server.base.ApplicationTestBase | ||
import com.arnyminerz.escalaralcoiaicomtat.backend.utils.getJSONObjectOrNull | ||
import com.arnyminerz.escalaralcoiaicomtat.backend.utils.jsonOf | ||
import io.ktor.client.request.setBody | ||
import io.ktor.http.HttpStatusCode | ||
import java.time.LocalDateTime | ||
import java.time.Month | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertNotNull | ||
import org.json.JSONObject | ||
|
||
class TestPatchBlockEndpoint: ApplicationTestBase() { | ||
private fun patch(parameterName: String, parameterValue: Any, assert: (element: JSONObject) -> Unit) = test { | ||
val areaId = DataProvider.provideSampleArea() | ||
val zoneId = DataProvider.provideSampleZone(areaId) | ||
val sectorId = DataProvider.provideSampleSector(zoneId) | ||
val pathId = DataProvider.provideSamplePath(sectorId) | ||
|
||
var blockId: Int? = null | ||
|
||
post("/block/$pathId") { | ||
setBody( | ||
jsonOf( | ||
"type" to BlockingTypes.BUILD | ||
).toString() | ||
) | ||
}.apply { | ||
assertSuccess(HttpStatusCode.Created) { | ||
val element = it?.getJSONObjectOrNull("element") | ||
assertNotNull(element) | ||
blockId = element.getInt("id") | ||
} | ||
} | ||
assertNotNull(blockId) | ||
|
||
patch("/block/$blockId") { | ||
setBody( | ||
jsonOf(parameterName to parameterValue).toString() | ||
) | ||
}.apply { | ||
assertSuccess { | ||
val element = it?.getJSONObjectOrNull("element") | ||
assertNotNull(element) | ||
assert(element) | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
fun `test block patching - type`() = patch("type", BlockingTypes.BIRD) { | ||
assertEquals(BlockingTypes.BIRD, it.getEnum(BlockingTypes::class.java, "type")) | ||
} | ||
|
||
@Test | ||
fun `test block patching - recurrence`() { | ||
val recurrence = BlockingRecurrenceYearly(1U, Month.MARCH, 1U, Month.JULY) | ||
|
||
patch("recurrence", recurrence) { | ||
val actual = BlockingRecurrenceYearly.fromJson(it.getJSONObject("recurrence")) | ||
|
||
assertEquals(recurrence, actual) | ||
} | ||
} | ||
|
||
@Test | ||
fun `test block patching - endDate`() { | ||
val endDate = LocalDateTime.of(2023, 1, 1, 0, 0, 0, 0) | ||
|
||
patch("end_date", endDate.toString()) { | ||
assertEquals(endDate.toString(), it.getString("end_date")) | ||
} | ||
} | ||
} |