Add customer api module

This commit is contained in:
Sambo Chea 2021-01-27 08:55:39 +07:00
parent 7cd9ccd87b
commit eedeb3abfa
9 changed files with 148 additions and 1 deletions

View File

@ -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 {

37
customer-api/.gitignore vendored Normal file
View File

@ -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/

View File

@ -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<Test> {
useJUnitPlatform()
}

Binary file not shown.

View File

@ -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

View File

@ -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<String>) {
runApplication<CustomerApiApplication>(*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<Customer, String>
@RestController
@RequestMapping("/customers")
class CustomerController @Autowired constructor(
private val customerRepository: CustomerRepository
) {
@GetMapping
fun getAllCustomers(): Collection<Customer> {
return customerRepository.findAll()
}
@GetMapping("/create")
fun createCustomer(): Customer? {
val customer = Customer.create("Sambo - ${Random.nextInt(1000)}", "Chea - ${Random.nextInt(1000)}")
return customerRepository.save(customer)
}
}

View File

@ -0,0 +1 @@
spring.data.mongodb.uri=mongodb://192.168.0.202:27017/db-customer-api

View File

@ -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() {
}
}

View File

@ -1,3 +1,3 @@
rootProject.name = "sample-modules"
include("demo", "lib")
include("demo", "lib", "customer-api")