Skip to content

Instantly share code, notes, and snippets.

@raphaeldelio
Created July 13, 2022 07:11
Show Gist options
  • Select an option

  • Save raphaeldelio/25bd79e1a77dea5296c5ad49c7195d41 to your computer and use it in GitHub Desktop.

Select an option

Save raphaeldelio/25bd79e1a77dea5296c5ad49c7195d41 to your computer and use it in GitHub Desktop.
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
id("org.springframework.boot") version "2.7.1"
id("io.spring.dependency-management") version "1.0.11.RELEASE"
kotlin("jvm") version "1.6.21"
kotlin("plugin.spring") version "1.6.21"
}
group = "com.raphaeldelio"
version = "0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_17
repositories {
mavenCentral()
}
dependencies {
implementation("org.springframework.boot:spring-boot-starter")
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
implementation("com.redis.om:redis-om-spring:0.5.0")
testImplementation("org.springframework.boot:spring-boot-starter-test")
}
tasks.withType<KotlinCompile> {
kotlinOptions {
freeCompilerArgs = listOf("-Xjsr305=strict")
jvmTarget = "17"
}
}
tasks.withType<Test> {
useJUnitPlatform()
}
data class Product(
@Indexed
val name: String,
@Indexed
val price: Double
)
@Document
data class Store(
@Id
@Indexed
var id: String? = null,
@Indexed
var name: String,
@Searchable
var storeLogo: String,
@Indexed
var products: List<Product>
)
@RestController
@RequestMapping("/store")
class StoreController(
private val storeRepository: StoreRepository
) {
@GetMapping
fun getAll(): List<Store> {
return storeRepository.findAll().toList()
}
@PostMapping
fun save(@RequestBody store: Store): Store {
return storeRepository.save(store)
}
@PutMapping
fun updateNameById(@RequestParam id: String, @RequestParam name: String): Optional<Store> {
val store = storeRepository.findById(id)
store.ifPresent {
it.name = name
storeRepository.save(it)
}
return store
}
@DeleteMapping
fun deleteById(@RequestParam id: String) {
storeRepository.deleteById(id)
}
}
interface StoreRepository : RedisDocumentRepository<Store, String>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment