add city table, improve dto

This commit is contained in:
Denis Savosin
2024-10-01 15:16:13 +07:00
parent f9632ac568
commit dd4389e780
5 changed files with 78 additions and 29 deletions

View File

@@ -1,3 +1,24 @@
package com.example.demo.models
data class City(val name: String)
import com.example.demo.models.serializables.OffsetDateTimeSerialization
import kotlinx.serialization.Serializable
import org.springframework.data.relational.core.mapping.Column
import org.springframework.data.relational.core.mapping.Table
import java.time.OffsetDateTime
import java.util.*
@Table("city")
data class City(
val id: Long?,
val guid: UUID,
val name: String,
@Serializable(with = OffsetDateTimeSerialization::class)
@Column(value = "created_at")
val createdAt: OffsetDateTime,
@Serializable(with = OffsetDateTimeSerialization::class)
@Column(value = "updated_at")
val updatedAt: OffsetDateTime?,
@Serializable(with = OffsetDateTimeSerialization::class)
@Column(value = "deleted_at")
val deletedAt: OffsetDateTime?,
)

View File

@@ -14,7 +14,7 @@ class MockedShopProvider: ShopProvider {
return Shop(name="shop", customers= listOf(
Customer(
name = "Foo-1",
city = City(name= "Bar"),
city = makeCity(id = 1, name = "Foo"),
orders = listOf(
Order(products = listOf(productOne, productTwo), isDelivered = true),
Order(products = listOf(productThree), isDelivered = false),
@@ -22,7 +22,7 @@ class MockedShopProvider: ShopProvider {
),
Customer(
name = "Foo-2",
city = City(name= "Bar"),
city = makeCity(id = 2, name = "Bar"),
orders = listOf(
Order(products = listOf(productOne), isDelivered = false),
Order(products = listOf(productTwo), isDelivered = true),
@@ -32,8 +32,7 @@ class MockedShopProvider: ShopProvider {
))
}
private fun makeProduct(id: Long, name: String, price: Double): Product {
return Product(
private fun makeProduct(id: Long, name: String, price: Double): Product = Product(
id = id,
guid = UUID.randomUUID(),
name = name,
@@ -43,5 +42,13 @@ class MockedShopProvider: ShopProvider {
updatedAt = null,
deletedAt = null,
)
}
private fun makeCity(id: Long, name: String): City = City(
id = id,
guid = UUID.randomUUID(),
name = name,
createdAt = OffsetDateTime.now(),
updatedAt = null,
deletedAt = null,
)
}

View File

@@ -0,0 +1,4 @@
insert into city (guid, name, created_at) values
(gen_random_uuid(), 'Kemerovo', now()),
(gen_random_uuid(), 'Novosibirsk', now()),
(gen_random_uuid(), 'Krasnoyarsk', now());

View File

@@ -0,0 +1,10 @@
create table city (
id bigserial primary key,
guid uuid not null,
name varchar(255) not null,
created_at timestamptz not null,
updated_at timestamptz,
deleted_at timestamptz
);
create unique index city_guid_idx ON city (guid);

View File

@@ -30,7 +30,7 @@ class ShopControllerTest(@Autowired val mockMvc: MockMvc) {
val shopMock = Shop(name="shop", customers= listOf(
Customer(
name = "cus-one",
city = City(name= "city-one"),
city = makeCity(id = 1, name = "city-one"),
orders = listOf(
Order(products = listOf(productOne), isDelivered = false),
Order(products = listOf(productTwo), isDelivered = false),
@@ -39,7 +39,7 @@ class ShopControllerTest(@Autowired val mockMvc: MockMvc) {
),
Customer(
name = "cus-two",
city = City(name= "city-two"),
city = makeCity(id = 2, name = "city-two"),
orders = listOf(
Order(products = listOf(productOne), isDelivered = false),
Order(products = listOf(productTwo), isDelivered = true),
@@ -75,8 +75,7 @@ class ShopControllerTest(@Autowired val mockMvc: MockMvc) {
.andExpect(content().json("""{"status":"not found"}"""))
}
private fun makeProduct(id: Long, name: String, price: Double): Product {
return Product(
private fun makeProduct(id: Long, name: String, price: Double): Product = Product(
id = id,
guid = UUID.randomUUID(),
name = name,
@@ -86,5 +85,13 @@ class ShopControllerTest(@Autowired val mockMvc: MockMvc) {
updatedAt = null,
deletedAt = null,
)
}
private fun makeCity(id: Long, name: String): City = City(
id = id,
guid = UUID.randomUUID(),
name = name,
createdAt = OffsetDateTime.now(),
updatedAt = null,
deletedAt = null,
)
}