mirror of
https://github.com/Dannecron/spring-boot-demo.git
synced 2025-12-26 08:42:33 +03:00
add get products api
This commit is contained in:
@@ -10,18 +10,25 @@ import com.example.demo.services.database.product.ProductService
|
||||
import com.example.demo.services.database.product.ProductServiceImpl
|
||||
import com.example.demo.services.kafka.Producer
|
||||
import com.fasterxml.jackson.databind.ObjectMapper
|
||||
import com.fasterxml.jackson.databind.SerializationFeature
|
||||
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule
|
||||
import org.springframework.beans.factory.annotation.Autowired
|
||||
import org.springframework.beans.factory.annotation.Value
|
||||
import org.springframework.context.annotation.Bean
|
||||
import org.springframework.context.annotation.Configuration
|
||||
|
||||
@Configuration
|
||||
class AppConfig(
|
||||
@Value("\${kafka.producer.product.default-sync-topic}")
|
||||
private val defaultProductSyncTopic: String
|
||||
) {
|
||||
@Bean
|
||||
fun objectMapper(): ObjectMapper = ObjectMapper()
|
||||
fun objectMapper(): ObjectMapper {
|
||||
val objectMapper = ObjectMapper()
|
||||
objectMapper.registerModules(JavaTimeModule())
|
||||
objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
|
||||
|
||||
return objectMapper
|
||||
}
|
||||
|
||||
@Bean
|
||||
fun shopProvider(): ShopProvider = MockedShopProvider()
|
||||
@@ -39,3 +46,4 @@ class AppConfig(
|
||||
@Bean
|
||||
fun cityService(@Autowired cityRepository: CityRepository): CityService = CityServiceImpl(cityRepository)
|
||||
}
|
||||
|
||||
|
||||
@@ -9,6 +9,8 @@ import com.example.demo.services.database.product.ProductService
|
||||
import com.example.demo.services.database.product.exceptions.ProductNotFoundException
|
||||
import com.example.demo.services.kafka.exceptions.InvalidArgumentException
|
||||
import jakarta.validation.Valid
|
||||
import org.springdoc.core.annotations.ParameterObject
|
||||
import org.springframework.data.domain.Pageable
|
||||
import org.springframework.http.HttpStatus
|
||||
import org.springframework.http.MediaType
|
||||
import org.springframework.http.ResponseEntity
|
||||
@@ -21,7 +23,6 @@ class ProductController(
|
||||
val productService: ProductService,
|
||||
) {
|
||||
@GetMapping("/{guid}")
|
||||
@ResponseBody
|
||||
@Throws(NotFoundException::class)
|
||||
fun getProduct(
|
||||
@PathVariable guid: UUID,
|
||||
@@ -31,8 +32,25 @@ class ProductController(
|
||||
return ResponseEntity(product, HttpStatus.OK)
|
||||
}
|
||||
|
||||
@GetMapping("")
|
||||
fun getProducts(
|
||||
@ParameterObject pageable: Pageable,
|
||||
): ResponseEntity<Any> {
|
||||
val products = productService.findAll(pageable)
|
||||
|
||||
return ResponseEntity(
|
||||
mapOf(
|
||||
"data" to products.content,
|
||||
"meta" to mapOf(
|
||||
"total" to products.totalElements,
|
||||
"pages" to products.totalPages,
|
||||
),
|
||||
),
|
||||
HttpStatus.OK,
|
||||
)
|
||||
}
|
||||
|
||||
@PostMapping("/{guid}/sync")
|
||||
@ResponseBody
|
||||
@Throws(NotFoundException::class)
|
||||
fun syncProductToKafka(
|
||||
@PathVariable guid: UUID,
|
||||
@@ -48,7 +66,6 @@ class ProductController(
|
||||
}
|
||||
|
||||
@PostMapping(value = [""], consumes = [MediaType.APPLICATION_JSON_VALUE])
|
||||
@ResponseBody
|
||||
fun createProduct(
|
||||
@Valid @RequestBody product: CreateProductRequest,
|
||||
): ResponseEntity<Any> {
|
||||
@@ -62,7 +79,6 @@ class ProductController(
|
||||
}
|
||||
|
||||
@DeleteMapping("/{guid}")
|
||||
@ResponseBody
|
||||
@Throws(NotFoundException::class, UnprocessableException::class)
|
||||
fun deleteProduct(
|
||||
@PathVariable guid: UUID,
|
||||
|
||||
@@ -3,13 +3,14 @@ package com.example.demo.providers
|
||||
import com.example.demo.models.Product
|
||||
import org.springframework.data.jdbc.repository.query.Query
|
||||
import org.springframework.data.repository.CrudRepository
|
||||
import org.springframework.data.repository.PagingAndSortingRepository
|
||||
import org.springframework.data.repository.query.Param
|
||||
import org.springframework.stereotype.Repository
|
||||
import java.time.OffsetDateTime
|
||||
import java.util.*
|
||||
|
||||
@Repository
|
||||
interface ProductRepository: CrudRepository<Product, Long> {
|
||||
interface ProductRepository: CrudRepository<Product, Long>, PagingAndSortingRepository<Product, Long> {
|
||||
fun findByGuid(guid: UUID): Product?
|
||||
|
||||
@Query(value = "UPDATE Product SET deleted_at = :deletedAt WHERE guid = :guid RETURNING *")
|
||||
|
||||
@@ -4,6 +4,8 @@ import com.example.demo.models.Product
|
||||
import com.example.demo.services.database.exceptions.AlreadyDeletedException
|
||||
import com.example.demo.services.database.product.exceptions.ProductNotFoundException
|
||||
import com.example.demo.services.kafka.exceptions.InvalidArgumentException
|
||||
import org.springframework.data.domain.Page
|
||||
import org.springframework.data.domain.Pageable
|
||||
import org.springframework.stereotype.Service
|
||||
import java.util.*
|
||||
|
||||
@@ -11,6 +13,8 @@ import java.util.*
|
||||
interface ProductService {
|
||||
fun findByGuid(guid: UUID): Product?
|
||||
|
||||
fun findAll(pageable: Pageable): Page<Product>
|
||||
|
||||
fun create(name: String, price: Long, description: String?): Product
|
||||
|
||||
@Throws(ProductNotFoundException::class, AlreadyDeletedException::class)
|
||||
|
||||
@@ -5,6 +5,8 @@ import com.example.demo.providers.ProductRepository
|
||||
import com.example.demo.services.database.exceptions.AlreadyDeletedException
|
||||
import com.example.demo.services.database.product.exceptions.ProductNotFoundException
|
||||
import com.example.demo.services.kafka.Producer
|
||||
import org.springframework.data.domain.Page
|
||||
import org.springframework.data.domain.Pageable
|
||||
import java.time.OffsetDateTime
|
||||
import java.util.*
|
||||
|
||||
@@ -15,6 +17,8 @@ class ProductServiceImpl(
|
||||
): ProductService {
|
||||
override fun findByGuid(guid: UUID): Product? = productRepository.findByGuid(guid)
|
||||
|
||||
override fun findAll(pageable: Pageable): Page<Product> = productRepository.findAll(pageable)
|
||||
|
||||
override fun create(name: String, price: Long, description: String?): Product {
|
||||
val product = Product(
|
||||
id = null,
|
||||
|
||||
Reference in New Issue
Block a user