add get products api

This commit is contained in:
Denis Savosin
2024-10-03 11:49:34 +07:00
parent c754557f0d
commit e89c1d99fb
6 changed files with 74 additions and 7 deletions

View File

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

View File

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

View File

@@ -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 *")

View File

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

View File

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