Skip to content
This repository has been archived by the owner on Oct 18, 2024. It is now read-only.

Commit

Permalink
feat: create now takes array (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
brizzbuzz authored Mar 10, 2022
1 parent fb44a74 commit 5d3785e
Show file tree
Hide file tree
Showing 10 changed files with 174 additions and 134 deletions.
16 changes: 8 additions & 8 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ jobs:
org.gradle.vfs.verbose=false
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
publish-to-nexus:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-java@v2
with:
distribution: 'adopt'
java-version: '17'
# publish-to-nexus:
# runs-on: ubuntu-latest
# steps:
# - uses: actions/checkout@v2
# - uses: actions/setup-java@v2
# with:
# distribution: 'adopt'
# java-version: '17'
# - name: Publlish to GithubPackages
# uses: burrunan/gradle-cache-action@v1
# with:
Expand Down
16 changes: 10 additions & 6 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased [0.2.0-SNAPSHOT]
## Unreleased
### Added

### Changed

### Removed

## [0.2.0] - March 10th, 2022
### Added
- Support for nullable table columns
- Centralize test utils in util fixture
Expand All @@ -13,15 +20,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Count All functionality
- Get All functionality
- Set indices on relational tables
- Set indices on documents
- Set indices on documents
- Persistence Annotation Module
- RDBMS foreign key support
- One-to-many relation support
- Many-to-many relation support

### Changed

### Removed
- Create now takes multiple entities

## [0.1.0] - 02/20/2022
### Added
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ import com.squareup.kotlinpoet.FileSpec
import com.squareup.kotlinpoet.FunSpec
import com.squareup.kotlinpoet.MemberName
import com.squareup.kotlinpoet.ParameterSpec
import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy
import com.squareup.kotlinpoet.TypeSpec
import com.squareup.kotlinpoet.asClassName
import com.squareup.kotlinpoet.ksp.KotlinPoetKspPreview
import com.squareup.kotlinpoet.ksp.addOriginatingKSFile
import io.bkbn.lerasium.core.Domain
Expand Down Expand Up @@ -91,7 +93,12 @@ class ApiVisitor(private val fileBuilder: FileSpec.Builder, private val logger:
add(CodeBlock.builder().apply {
val toc = MemberName(domain.toTocClass(), "create${domain.name}")
addControlFlow("%M(%M)", notarizedPostMember, toc) {
addStatement("val request = %M.%M<%T>()", callMember, receiveMember, domain.toCreateRequestClass())
addStatement(
"val request = %M.%M<%T>()",
callMember,
receiveMember,
List::class.asClassName().parameterizedBy(domain.toCreateRequestClass())
)
addStatement("val result = dao.create(request)")
addStatement("%M.%M(result)", callMember, respondMember)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,21 +80,23 @@ class TocVisitor(private val fileBuilder: FileSpec.Builder, private val logger:
}

private fun TypeSpec.Builder.addCreateInfo(domain: Domain) {
val requestClass = List::class.asClassName().parameterizedBy(domain.toCreateRequestClass())
val responseClass = List::class.asClassName().parameterizedBy(domain.toResponseClass())
val createPropType = PostInfo::class.asClassName()
.parameterizedBy(Unit::class.asClassName(), domain.toCreateRequestClass(), domain.toResponseClass())
.parameterizedBy(Unit::class.asClassName(), requestClass, responseClass)
addProperty(PropertySpec.builder("create${domain.name}", createPropType).apply {
initializer(CodeBlock.builder().apply {
addObjectInstantiation(createPropType) {
addStatement("summary = %S,", "Create ${domain.name}")
addStatement("description = %S,", "Creates a new ${domain.name}")
addStatement("description = %S,", "Creates new ${domain.name} entities for the provided request objects")
add("requestInfo = ")
addObjectInstantiation(RequestInfo::class.asTypeName(), trailingComma = true) {
addStatement("description = %S,", "Details required to create a new ${domain.name}")
addStatement("description = %S,", "Details required to create new ${domain.name} entities")
}
add("responseInfo = ")
addObjectInstantiation(ResponseInfo::class.asClassName(), trailingComma = true) {
addStatement("status = %T.Created,", HttpStatusCode::class)
addStatement("description = %S", "The ${domain.name} was retrieved successfully")
addStatement("description = %S", "The ${domain.name} entities were created successfully")
}
addStatement("tags = setOf(%S)", domain.name)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,13 @@ class KtorProcessorProviderTest : DescribeSpec({
import io.ktor.routing.route
import java.util.UUID
import kotlin.Unit
import kotlin.collections.List

public object UserApi {
public fun Route.userController(dao: UserDao): Unit {
route("/user") {
notarizedPost(createUser) {
val request = call.receive<UserCreateRequest>()
val request = call.receive<List<UserCreateRequest>>()
val result = dao.create(request)
call.respond(result)
}
Expand Down Expand Up @@ -225,12 +226,13 @@ class KtorProcessorProviderTest : DescribeSpec({
import io.ktor.routing.route
import java.util.UUID
import kotlin.Unit
import kotlin.collections.List

public object CountryApi {
public fun Route.countryController(dao: CountryDao): Unit {
route("/country") {
notarizedPost(createCountry) {
val request = call.receive<CountryCreateRequest>()
val request = call.receive<List<CountryCreateRequest>>()
val result = dao.create(request)
call.respond(result)
}
Expand Down Expand Up @@ -314,16 +316,16 @@ class KtorProcessorProviderTest : DescribeSpec({
import kotlin.collections.List

public object UserToC {
public val createUser: PostInfo<Unit, UserCreateRequest, UserResponse> =
PostInfo<Unit, UserCreateRequest, UserResponse>(
public val createUser: PostInfo<Unit, List<UserCreateRequest>, List<UserResponse>> =
PostInfo<Unit, List<UserCreateRequest>, List<UserResponse>>(
summary = "Create User",
description = "Creates a new User",
description = "Creates new User entities for the provided request objects",
requestInfo = RequestInfo(
description = "Details required to create a new User",
description = "Details required to create new User entities",
),
responseInfo = ResponseInfo(
status = HttpStatusCode.Created,
description = "The User was retrieved successfully"
description = "The User entities were created successfully"
),
tags = setOf("User")
)
Expand Down Expand Up @@ -459,16 +461,16 @@ class KtorProcessorProviderTest : DescribeSpec({
import kotlin.collections.List

public object CountryToC {
public val createCountry: PostInfo<Unit, CountryCreateRequest, CountryResponse> =
PostInfo<Unit, CountryCreateRequest, CountryResponse>(
public val createCountry: PostInfo<Unit, List<CountryCreateRequest>, List<CountryResponse>> =
PostInfo<Unit, List<CountryCreateRequest>, List<CountryResponse>>(
summary = "Create Country",
description = "Creates a new Country",
description = "Creates new Country entities for the provided request objects",
requestInfo = RequestInfo(
description = "Details required to create a new Country",
description = "Details required to create new Country entities",
),
responseInfo = ResponseInfo(
status = HttpStatusCode.Created,
description = "The Country was retrieved successfully"
description = "The Country entities were created successfully"
),
tags = setOf("Country")
)
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/kotlin/io/bkbn/lerasium/core/dao/Dao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ interface Dao<ENT, RESP, CRE, UPT>
ENT : Entity<RESP>,
CRE : Request.Create,
UPT : Request.Update {
fun create(request: CRE): RESP
fun create(requests: List<CRE>): List<RESP>
fun read(id: UUID): RESP
fun update(id: UUID, request: UPT): RESP
fun delete(id: UUID)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,17 @@ class DaoVisitor(private val fileBuilder: FileSpec.Builder, private val logger:
) {
addFunction(FunSpec.builder("create").apply {
addModifiers(KModifier.OVERRIDE)
addParameter("request", requestClass)
returns(responseClass)
addParameter("requests", List::class.asClassName().parameterizedBy(requestClass))
returns(List::class.asClassName().parameterizedBy(responseClass))
addCode(CodeBlock.builder().apply {
addStatement("val now = %T.now().%M(%T.UTC)", Clock.System::class, toLDT, TimeZone::class)
add("val entity = ")
convertToNewEntity(entityClass, cd, "request", true)
addStatement("collection.%M(entity)", Save)
addStatement("return entity.toResponse()")
addControlFlow("val entities = requests.map { request ->") {
add("val entity = ")
convertToNewEntity(entityClass, cd, "request", true)
addStatement("collection.%M(entity)", Save)
addStatement("entity")
}
addStatement("return entities.map { it.toResponse() }")
}.build())
}.build())
}
Expand Down
Loading

0 comments on commit 5d3785e

Please sign in to comment.