mirror of
https://github.com/Dannecron/spring-boot-demo.git
synced 2025-12-26 00:32:34 +03:00
add product repository, controller, tests
This commit is contained in:
@@ -4,8 +4,10 @@ import com.example.demo.provider.MockedShopProvider
|
||||
import com.example.demo.provider.ShopProvider
|
||||
import org.springframework.context.annotation.Bean
|
||||
import org.springframework.context.annotation.Configuration
|
||||
import org.springframework.data.jpa.repository.config.EnableJpaRepositories
|
||||
|
||||
@Configuration
|
||||
@EnableJpaRepositories(basePackages = ["com.example.demo.providers"])
|
||||
class AppConfig {
|
||||
@Bean
|
||||
fun shopProvider(): ShopProvider{
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.example.demo.controllers
|
||||
|
||||
import com.example.demo.exceptions.NotFoundException
|
||||
import com.example.demo.provider.ProductRepository
|
||||
import kotlinx.serialization.json.Json
|
||||
import kotlinx.serialization.json.encodeToJsonElement
|
||||
import org.springframework.beans.factory.annotation.Autowired
|
||||
import org.springframework.web.bind.annotation.*
|
||||
import java.util.*
|
||||
|
||||
@RestController
|
||||
@RequestMapping(value = ["/api/product"])
|
||||
class ProductController(
|
||||
@Autowired val productRepository: ProductRepository
|
||||
) {
|
||||
@GetMapping(value = ["{guid}"], produces = ["application/json"])
|
||||
@ResponseBody
|
||||
fun getProduct(
|
||||
@PathVariable guid: UUID
|
||||
): String {
|
||||
val product = productRepository.findByGuid(guid = guid) ?: throw NotFoundException()
|
||||
|
||||
return Json.encodeToJsonElement(value = product).toString()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.example.demo.provider
|
||||
|
||||
import com.example.demo.models.Product
|
||||
import org.springframework.data.jpa.repository.Query
|
||||
import org.springframework.data.repository.CrudRepository
|
||||
import org.springframework.stereotype.Repository
|
||||
import java.util.*
|
||||
|
||||
@Repository
|
||||
interface ProductRepository: CrudRepository<Product, Long> {
|
||||
@Query(value = "SELECT * FROM Product WHERE guid = :guid")
|
||||
fun findByGuid(guid: UUID): Product?
|
||||
}
|
||||
Reference in New Issue
Block a user