diff --git a/build.gradle.kts b/build.gradle.kts index 49e1716..15df67b 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -23,6 +23,7 @@ plugins { id("io.spring.dependency-management") version "1.0.11.RELEASE" apply false kotlin("jvm") version "1.4.21" apply false kotlin("plugin.spring") version "1.4.21" apply false + kotlin("plugin.jpa") version "1.4.21" apply false } allprojects { diff --git a/customer-api/.gitignore b/customer-api/.gitignore new file mode 100644 index 0000000..c2065bc --- /dev/null +++ b/customer-api/.gitignore @@ -0,0 +1,37 @@ +HELP.md +.gradle +build/ +!gradle/wrapper/gradle-wrapper.jar +!**/src/main/**/build/ +!**/src/test/**/build/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache +bin/ +!**/src/main/**/bin/ +!**/src/test/**/bin/ + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ + +### VS Code ### +.vscode/ diff --git a/customer-api/build.gradle.kts b/customer-api/build.gradle.kts new file mode 100644 index 0000000..230a621 --- /dev/null +++ b/customer-api/build.gradle.kts @@ -0,0 +1,19 @@ +plugins { + id("org.springframework.boot") + id("io.spring.dependency-management") + kotlin("jvm") + kotlin("plugin.spring") +} + +dependencies { + implementation("org.springframework.boot:spring-boot-starter-data-mongodb") + implementation("org.springframework.boot:spring-boot-starter-web") + implementation("com.fasterxml.jackson.module:jackson-module-kotlin") + implementation("org.jetbrains.kotlin:kotlin-reflect") + implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8") + testImplementation("org.springframework.boot:spring-boot-starter-test") +} + +tasks.withType { + useJUnitPlatform() +} diff --git a/customer-api/gradle/wrapper/gradle-wrapper.jar b/customer-api/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000..e708b1c Binary files /dev/null and b/customer-api/gradle/wrapper/gradle-wrapper.jar differ diff --git a/customer-api/gradle/wrapper/gradle-wrapper.properties b/customer-api/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000..12d38de --- /dev/null +++ b/customer-api/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,5 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-6.6.1-bin.zip +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/customer-api/src/main/kotlin/com/example/customerapi/CustomerApiApplication.kt b/customer-api/src/main/kotlin/com/example/customerapi/CustomerApiApplication.kt new file mode 100644 index 0000000..c7ac699 --- /dev/null +++ b/customer-api/src/main/kotlin/com/example/customerapi/CustomerApiApplication.kt @@ -0,0 +1,71 @@ +package com.example.customerapi + +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.boot.CommandLineRunner +import org.springframework.boot.autoconfigure.SpringBootApplication +import org.springframework.boot.runApplication +import org.springframework.data.annotation.Id +import org.springframework.data.mongodb.core.mapping.Document +import org.springframework.data.mongodb.repository.MongoRepository +import org.springframework.stereotype.Repository +import org.springframework.web.bind.annotation.GetMapping +import org.springframework.web.bind.annotation.RequestMapping +import org.springframework.web.bind.annotation.RestController +import java.io.Serializable +import kotlin.random.Random + +@SpringBootApplication +class CustomerApiApplication @Autowired constructor( + private val customerRepository: CustomerRepository, +) : CommandLineRunner { + override fun run(vararg args: String?) { + + val customer = Customer.create("Sambo - ${Random.nextInt(1000)}", "Chea - ${Random.nextInt(1000)}") + + val saved = customerRepository.save(customer) + + println(saved) + } +} + +fun main(args: Array) { + runApplication(*args) +} + + +@Document +data class Customer( + @Id + val id: String? = null, + var firstName: String, + var lastName: String, +) : Serializable { + companion object { + fun create( + firstName: String, + lastName: String + ): Customer { + return Customer(null, firstName, lastName) + } + } +} + +@Repository +interface CustomerRepository : MongoRepository + +@RestController +@RequestMapping("/customers") +class CustomerController @Autowired constructor( + private val customerRepository: CustomerRepository +) { + @GetMapping + fun getAllCustomers(): Collection { + return customerRepository.findAll() + } + + @GetMapping("/create") + fun createCustomer(): Customer? { + val customer = Customer.create("Sambo - ${Random.nextInt(1000)}", "Chea - ${Random.nextInt(1000)}") + return customerRepository.save(customer) + } +} \ No newline at end of file diff --git a/customer-api/src/main/resources/application.properties b/customer-api/src/main/resources/application.properties new file mode 100644 index 0000000..78195c5 --- /dev/null +++ b/customer-api/src/main/resources/application.properties @@ -0,0 +1 @@ +spring.data.mongodb.uri=mongodb://192.168.0.202:27017/db-customer-api \ No newline at end of file diff --git a/customer-api/src/test/kotlin/com/example/customerapi/CustomerApiApplicationTests.kt b/customer-api/src/test/kotlin/com/example/customerapi/CustomerApiApplicationTests.kt new file mode 100644 index 0000000..5e115d3 --- /dev/null +++ b/customer-api/src/test/kotlin/com/example/customerapi/CustomerApiApplicationTests.kt @@ -0,0 +1,13 @@ +package com.example.customerapi + +import org.junit.jupiter.api.Test +import org.springframework.boot.test.context.SpringBootTest + +@SpringBootTest +class CustomerApiApplicationTests { + + @Test + fun contextLoads() { + } + +} diff --git a/settings.gradle.kts b/settings.gradle.kts index eff4bc5..110f265 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -1,3 +1,3 @@ rootProject.name = "sample-modules" - include("demo", "lib") \ No newline at end of file + include("demo", "lib", "customer-api") \ No newline at end of file