mirror of
https://github.com/Dannecron/spring-boot-demo.git
synced 2025-12-26 00:32:34 +03:00
rename project
This commit is contained in:
31
src/main/kotlin/com/github/dannecron/demo/models/City.kt
Normal file
31
src/main/kotlin/com/github/dannecron/demo/models/City.kt
Normal file
@@ -0,0 +1,31 @@
|
||||
package com.github.dannecron.demo.models
|
||||
|
||||
import com.github.dannecron.demo.services.serializables.OffsetDateTimeSerialization
|
||||
import com.github.dannecron.demo.services.serializables.UuidSerialization
|
||||
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 java.time.OffsetDateTime
|
||||
import java.util.*
|
||||
|
||||
@Table("city")
|
||||
@Serializable
|
||||
data class City(
|
||||
@Id
|
||||
val id: Long?,
|
||||
@Serializable(with = UuidSerialization::class)
|
||||
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?,
|
||||
) {
|
||||
fun isDeleted(): Boolean = deletedAt != null
|
||||
}
|
||||
27
src/main/kotlin/com/github/dannecron/demo/models/Customer.kt
Normal file
27
src/main/kotlin/com/github/dannecron/demo/models/Customer.kt
Normal file
@@ -0,0 +1,27 @@
|
||||
package com.github.dannecron.demo.models
|
||||
|
||||
import com.github.dannecron.demo.services.serializables.OffsetDateTimeSerialization
|
||||
import com.github.dannecron.demo.services.serializables.UuidSerialization
|
||||
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 java.time.OffsetDateTime
|
||||
import java.util.*
|
||||
|
||||
@Table("customer")
|
||||
@Serializable
|
||||
data class Customer(
|
||||
@Id
|
||||
val id: Long?,
|
||||
@Serializable(with = UuidSerialization::class)
|
||||
val guid: UUID,
|
||||
val name: String,
|
||||
val cityId: Long?,
|
||||
@Serializable(with = OffsetDateTimeSerialization::class)
|
||||
@Column(value = "created_at")
|
||||
val createdAt: OffsetDateTime,
|
||||
@Serializable(with = OffsetDateTimeSerialization::class)
|
||||
@Column(value = "updated_at")
|
||||
val updatedAt: OffsetDateTime?,
|
||||
)
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.github.dannecron.demo.models
|
||||
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
data class CustomerExtended(
|
||||
val customer: Customer,
|
||||
val city: City?,
|
||||
)
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.github.dannecron.demo.models
|
||||
|
||||
data class CustomerLocal(val name: String, val city: City, val orders: List<Order>) {
|
||||
/**
|
||||
* Return the most expensive product among all delivered products
|
||||
*/
|
||||
fun getMostExpensiveDeliveredProduct(): Product? = orders.filter { ord -> ord.isDelivered }
|
||||
.flatMap { ord -> ord.products }
|
||||
.maxByOrNull { pr -> pr.price }
|
||||
|
||||
fun getMostExpensiveOrderedProduct(): Product? = orders.flatMap { ord -> ord.products }.maxByOrNull { pr -> pr.price }
|
||||
|
||||
fun getOrderedProducts(): Set<Product> = orders.flatMap { order -> order.products }.toSet()
|
||||
|
||||
fun getTotalOrderPrice(): Double = orders.flatMap { ord -> ord.products }.sumOf { pr -> pr.getPriceDouble()}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
package com.github.dannecron.demo.models
|
||||
|
||||
data class Order(val products: List<Product>, val isDelivered: Boolean)
|
||||
37
src/main/kotlin/com/github/dannecron/demo/models/Product.kt
Normal file
37
src/main/kotlin/com/github/dannecron/demo/models/Product.kt
Normal file
@@ -0,0 +1,37 @@
|
||||
package com.github.dannecron.demo.models
|
||||
|
||||
|
||||
import com.github.dannecron.demo.services.serializables.OffsetDateTimeSerialization
|
||||
import com.github.dannecron.demo.services.serializables.UuidSerialization
|
||||
import com.github.dannecron.demo.utils.roundTo
|
||||
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 java.time.OffsetDateTime
|
||||
import java.util.*
|
||||
|
||||
@Table(value = "product")
|
||||
@Serializable
|
||||
data class Product(
|
||||
@Id
|
||||
val id: Long?,
|
||||
@Serializable(with = UuidSerialization::class)
|
||||
val guid: UUID,
|
||||
val name: String,
|
||||
val description: String?,
|
||||
val price: Long,
|
||||
@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?,
|
||||
) {
|
||||
fun getPriceDouble(): Double = (price.toDouble() / 100).roundTo(2)
|
||||
|
||||
fun isDeleted(): Boolean = deletedAt != null
|
||||
}
|
||||
48
src/main/kotlin/com/github/dannecron/demo/models/Shop.kt
Normal file
48
src/main/kotlin/com/github/dannecron/demo/models/Shop.kt
Normal file
@@ -0,0 +1,48 @@
|
||||
package com.github.dannecron.demo.models
|
||||
|
||||
data class Shop(val name: String, val customers: List<CustomerLocal>) {
|
||||
fun checkAllCustomersAreFrom(city: City): Boolean = customers.count { cus -> cus.city == city } == customers.count()
|
||||
|
||||
fun countCustomersFrom(city: City): Int = customers.count { cus -> cus.city == city }
|
||||
|
||||
fun getCitiesCustomersAreFrom(): Set<City> = customers.map { cus -> cus.city }.toSet()
|
||||
|
||||
fun findAnyCustomerFrom(city: City): CustomerLocal? = customers.firstOrNull { cus -> cus.city == city }
|
||||
|
||||
fun getAllOrderedProducts(): Set<Product> = customers.flatMap { cus -> cus.getOrderedProducts() }.toSet()
|
||||
|
||||
fun getCustomersFrom(city: City): List<CustomerLocal> = customers.filter { cus -> cus.city == city }
|
||||
|
||||
fun getCustomersSortedByNumberOfOrders(): List<CustomerLocal> = customers.sortedBy { cus -> cus.orders.count() }
|
||||
|
||||
fun getCustomerWithMaximumNumberOfOrders(): CustomerLocal? = customers.maxByOrNull { cus -> cus.orders.count() }
|
||||
|
||||
/**
|
||||
* Return customers who have more undelivered orders than delivered
|
||||
*/
|
||||
fun getCustomersWithMoreUndeliveredOrdersThanDelivered(): Set<CustomerLocal> = customers.partition(predicate = fun (cus): Boolean {
|
||||
val (del, undel) = cus.orders.partition { ord -> ord.isDelivered }
|
||||
|
||||
return del.count() < undel.count()
|
||||
}).first.toSet()
|
||||
|
||||
fun getNumberOfTimesProductWasOrdered(product: Product): Int = customers
|
||||
.flatMap { cus -> cus.orders.flatMap { ord -> ord.products } }
|
||||
.count { pr -> pr.name == product.name }
|
||||
|
||||
/**
|
||||
* Return the set of products that were ordered by every customer.
|
||||
* Note: a customer may order the same product for several times.
|
||||
*/
|
||||
fun getSetOfProductsOrderedByEveryCustomer(): Set<Product> {
|
||||
val products = customers.flatMap { cus -> cus.orders.flatMap { ord -> ord.products } }.toSet()
|
||||
|
||||
return customers.fold(products) { orderedProducts, cus ->
|
||||
orderedProducts.intersect(cus.orders.flatMap { ord -> ord.products }.toSet())
|
||||
}.toSet()
|
||||
}
|
||||
|
||||
fun groupCustomersByCity(): Map<City, List<CustomerLocal>> = customers.groupBy { cus -> cus.city }
|
||||
|
||||
fun hasCustomerFrom(city: City): Boolean = customers.any { cus -> cus.city == city }
|
||||
}
|
||||
Reference in New Issue
Block a user