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 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( return Shop(name="shop", customers= listOf(
Customer( Customer(
name = "Foo-1", name = "Foo-1",
city = City(name= "Bar"), city = makeCity(id = 1, name = "Foo"),
orders = listOf( orders = listOf(
Order(products = listOf(productOne, productTwo), isDelivered = true), Order(products = listOf(productOne, productTwo), isDelivered = true),
Order(products = listOf(productThree), isDelivered = false), Order(products = listOf(productThree), isDelivered = false),
@@ -22,7 +22,7 @@ class MockedShopProvider: ShopProvider {
), ),
Customer( Customer(
name = "Foo-2", name = "Foo-2",
city = City(name= "Bar"), city = makeCity(id = 2, name = "Bar"),
orders = listOf( orders = listOf(
Order(products = listOf(productOne), isDelivered = false), Order(products = listOf(productOne), isDelivered = false),
Order(products = listOf(productTwo), isDelivered = true), Order(products = listOf(productTwo), isDelivered = true),
@@ -32,16 +32,23 @@ class MockedShopProvider: ShopProvider {
)) ))
} }
private fun makeProduct(id: Long, name: String, price: Double): Product { private fun makeProduct(id: Long, name: String, price: Double): Product = Product(
return Product( id = id,
id = id, guid = UUID.randomUUID(),
guid = UUID.randomUUID(), name = name,
name = name, description = null,
description = null, price = (price * 100).toLong(),
price = (price * 100).toLong(), createdAt = OffsetDateTime.now(),
createdAt = OffsetDateTime.now(), updatedAt = null,
updatedAt = null, deletedAt = 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( val shopMock = Shop(name="shop", customers= listOf(
Customer( Customer(
name = "cus-one", name = "cus-one",
city = City(name= "city-one"), city = makeCity(id = 1, name = "city-one"),
orders = listOf( orders = listOf(
Order(products = listOf(productOne), isDelivered = false), Order(products = listOf(productOne), isDelivered = false),
Order(products = listOf(productTwo), isDelivered = false), Order(products = listOf(productTwo), isDelivered = false),
@@ -39,7 +39,7 @@ class ShopControllerTest(@Autowired val mockMvc: MockMvc) {
), ),
Customer( Customer(
name = "cus-two", name = "cus-two",
city = City(name= "city-two"), city = makeCity(id = 2, name = "city-two"),
orders = listOf( orders = listOf(
Order(products = listOf(productOne), isDelivered = false), Order(products = listOf(productOne), isDelivered = false),
Order(products = listOf(productTwo), isDelivered = true), Order(products = listOf(productTwo), isDelivered = true),
@@ -75,16 +75,23 @@ class ShopControllerTest(@Autowired val mockMvc: MockMvc) {
.andExpect(content().json("""{"status":"not found"}""")) .andExpect(content().json("""{"status":"not found"}"""))
} }
private fun makeProduct(id: Long, name: String, price: Double): Product { private fun makeProduct(id: Long, name: String, price: Double): Product = Product(
return Product( id = id,
id = id, guid = UUID.randomUUID(),
guid = UUID.randomUUID(), name = name,
name = name, description = null,
description = null, price = (price * 100).toLong(),
price = (price * 100).toLong(), createdAt = OffsetDateTime.now(),
createdAt = OffsetDateTime.now(), updatedAt = null,
updatedAt = null, deletedAt = 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,
)
} }