mirror of
https://github.com/Dannecron/spring-boot-demo.git
synced 2025-12-25 16:22:35 +03:00
add create product api-endpoint
This commit is contained in:
@@ -1,11 +1,14 @@
|
|||||||
package com.example.demo.controllers
|
package com.example.demo.controllers
|
||||||
|
|
||||||
import com.example.demo.exceptions.NotFoundException
|
import com.example.demo.exceptions.NotFoundException
|
||||||
|
import com.example.demo.models.Product
|
||||||
import com.example.demo.provider.ProductRepository
|
import com.example.demo.provider.ProductRepository
|
||||||
|
import com.example.demo.requests.CreateProductRequest
|
||||||
import kotlinx.serialization.json.Json
|
import kotlinx.serialization.json.Json
|
||||||
import kotlinx.serialization.json.encodeToJsonElement
|
import kotlinx.serialization.json.encodeToJsonElement
|
||||||
import org.springframework.beans.factory.annotation.Autowired
|
import org.springframework.beans.factory.annotation.Autowired
|
||||||
import org.springframework.web.bind.annotation.*
|
import org.springframework.web.bind.annotation.*
|
||||||
|
import java.time.OffsetDateTime
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@@ -22,4 +25,26 @@ class ProductController(
|
|||||||
|
|
||||||
return Json.encodeToJsonElement(value = product).toString()
|
return Json.encodeToJsonElement(value = product).toString()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@PostMapping(value = [""], consumes = ["application/json"], produces = ["application/json"])
|
||||||
|
@ResponseBody
|
||||||
|
fun createProduct(
|
||||||
|
@RequestBody product: CreateProductRequest
|
||||||
|
): String {
|
||||||
|
val productModel = Product(
|
||||||
|
id = null,
|
||||||
|
guid = UUID.randomUUID(),
|
||||||
|
name = product.name,
|
||||||
|
description = product.description,
|
||||||
|
price = product.price,
|
||||||
|
createdAt = OffsetDateTime.now(),
|
||||||
|
updatedAt = null,
|
||||||
|
)
|
||||||
|
|
||||||
|
val saved = productRepository.save(productModel)
|
||||||
|
|
||||||
|
return Json.encodeToJsonElement(value = saved).toString()
|
||||||
|
}
|
||||||
|
|
||||||
|
// todo delete with soft-delete
|
||||||
}
|
}
|
||||||
@@ -4,9 +4,9 @@ package com.example.demo.models
|
|||||||
import com.example.demo.models.serializables.OffsetDateTimeSerialization
|
import com.example.demo.models.serializables.OffsetDateTimeSerialization
|
||||||
import com.example.demo.models.serializables.UuidSerialization
|
import com.example.demo.models.serializables.UuidSerialization
|
||||||
import com.example.demo.utils.roundTo
|
import com.example.demo.utils.roundTo
|
||||||
import jakarta.persistence.Column
|
|
||||||
import jakarta.persistence.Id
|
|
||||||
import kotlinx.serialization.Serializable
|
import kotlinx.serialization.Serializable
|
||||||
|
import org.springframework.data.annotation.Id
|
||||||
|
import org.springframework.data.relational.core.mapping.Column
|
||||||
import org.springframework.data.relational.core.mapping.Table
|
import org.springframework.data.relational.core.mapping.Table
|
||||||
import java.time.OffsetDateTime
|
import java.time.OffsetDateTime
|
||||||
import java.util.*
|
import java.util.*
|
||||||
@@ -14,17 +14,18 @@ import java.util.*
|
|||||||
@Table(value = "product", schema = "public")
|
@Table(value = "product", schema = "public")
|
||||||
@Serializable
|
@Serializable
|
||||||
data class Product(
|
data class Product(
|
||||||
@Id var id: Long,
|
@Id
|
||||||
|
val id: Long?,
|
||||||
@Serializable(with = UuidSerialization::class)
|
@Serializable(with = UuidSerialization::class)
|
||||||
val guid: UUID,
|
val guid: UUID,
|
||||||
val name: String,
|
val name: String,
|
||||||
val description: String?,
|
val description: String?,
|
||||||
val price: Int,
|
val price: Long,
|
||||||
@Serializable(with = OffsetDateTimeSerialization::class)
|
@Serializable(with = OffsetDateTimeSerialization::class)
|
||||||
@Column(name = "created_at")
|
@Column(value = "created_at")
|
||||||
val createdAt: OffsetDateTime,
|
val createdAt: OffsetDateTime,
|
||||||
@Serializable(with = OffsetDateTimeSerialization::class)
|
@Serializable(with = OffsetDateTimeSerialization::class)
|
||||||
@Column(name = "updated_at")
|
@Column(value = "updated_at")
|
||||||
val updatedAt: OffsetDateTime?,
|
val updatedAt: OffsetDateTime?,
|
||||||
) {
|
) {
|
||||||
fun getPriceDouble(): Double = (price.toDouble() / 100).roundTo(2)
|
fun getPriceDouble(): Double = (price.toDouble() / 100).roundTo(2)
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ class MockedShopProvider: ShopProvider {
|
|||||||
guid = UUID.randomUUID(),
|
guid = UUID.randomUUID(),
|
||||||
name = name,
|
name = name,
|
||||||
description = null,
|
description = null,
|
||||||
price = (price * 100).toInt(),
|
price = (price * 100).toLong(),
|
||||||
createdAt = OffsetDateTime.now(),
|
createdAt = OffsetDateTime.now(),
|
||||||
updatedAt = null,
|
updatedAt = null,
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package com.example.demo.requests
|
||||||
|
|
||||||
|
data class CreateProductRequest(
|
||||||
|
val name: String,
|
||||||
|
val description: String?,
|
||||||
|
val price: Long,
|
||||||
|
)
|
||||||
Reference in New Issue
Block a user