diff --git a/src/main/kotlin/com/example/demo/models/City.kt b/src/main/kotlin/com/example/demo/models/City.kt index dc62433..fc21b14 100644 --- a/src/main/kotlin/com/example/demo/models/City.kt +++ b/src/main/kotlin/com/example/demo/models/City.kt @@ -1,3 +1,24 @@ package com.example.demo.models -data class City(val name: String) \ No newline at end of file +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?, +) \ No newline at end of file diff --git a/src/main/kotlin/com/example/demo/provider/MockedShopProvider.kt b/src/main/kotlin/com/example/demo/provider/MockedShopProvider.kt index 2c2bd53..191e1d6 100644 --- a/src/main/kotlin/com/example/demo/provider/MockedShopProvider.kt +++ b/src/main/kotlin/com/example/demo/provider/MockedShopProvider.kt @@ -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,16 +32,23 @@ class MockedShopProvider: ShopProvider { )) } - private fun makeProduct(id: Long, name: String, price: Double): Product { - return Product( - id = id, - guid = UUID.randomUUID(), - name = name, - description = null, - price = (price * 100).toLong(), - createdAt = OffsetDateTime.now(), - updatedAt = null, - deletedAt = null, - ) - } + private fun makeProduct(id: Long, name: String, price: Double): Product = Product( + id = id, + guid = UUID.randomUUID(), + name = name, + description = null, + price = (price * 100).toLong(), + createdAt = OffsetDateTime.now(), + 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, + ) } \ No newline at end of file diff --git a/src/main/resources/db/migration/data/V4.1__insert_city_data.sql b/src/main/resources/db/migration/data/V4.1__insert_city_data.sql new file mode 100644 index 0000000..105605f --- /dev/null +++ b/src/main/resources/db/migration/data/V4.1__insert_city_data.sql @@ -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()); \ No newline at end of file diff --git a/src/main/resources/db/migration/structure/V4__create_city_table.sql b/src/main/resources/db/migration/structure/V4__create_city_table.sql new file mode 100644 index 0000000..8bd9d3c --- /dev/null +++ b/src/main/resources/db/migration/structure/V4__create_city_table.sql @@ -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); \ No newline at end of file diff --git a/src/test/kotlin/com/example/demo/controllers/ShopControllerTest.kt b/src/test/kotlin/com/example/demo/controllers/ShopControllerTest.kt index c7f67ba..ce64432 100644 --- a/src/test/kotlin/com/example/demo/controllers/ShopControllerTest.kt +++ b/src/test/kotlin/com/example/demo/controllers/ShopControllerTest.kt @@ -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,16 +75,23 @@ class ShopControllerTest(@Autowired val mockMvc: MockMvc) { .andExpect(content().json("""{"status":"not found"}""")) } - private fun makeProduct(id: Long, name: String, price: Double): Product { - return Product( - id = id, - guid = UUID.randomUUID(), - name = name, - description = null, - price = (price * 100).toLong(), - createdAt = OffsetDateTime.now(), - updatedAt = null, - deletedAt = null, - ) - } + private fun makeProduct(id: Long, name: String, price: Double): Product = Product( + id = id, + guid = UUID.randomUUID(), + name = name, + description = null, + price = (price * 100).toLong(), + createdAt = OffsetDateTime.now(), + 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, + ) } \ No newline at end of file