From 45321bfa92c89b42cdd96518923aaa8ccdb69f8a Mon Sep 17 00:00:00 2001 From: chaeda Date: Sun, 31 Mar 2024 08:05:48 +0900 Subject: [PATCH] =?UTF-8?q?cafe-12-end=20=EC=85=8B=ED=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/repository/CafeOrderRepository.kt | 36 +++++++++++++++++++ .../kotlin/com/example/route/OrderRoute.kt | 4 +++ .../com/example/service/OrderService.kt | 12 +++++++ .../kotlin/com/example/shared/dto/OrderDto.kt | 10 ++++++ 4 files changed, 62 insertions(+) diff --git a/src/main/kotlin/com/example/domain/repository/CafeOrderRepository.kt b/src/main/kotlin/com/example/domain/repository/CafeOrderRepository.kt index c48da0c..f276836 100644 --- a/src/main/kotlin/com/example/domain/repository/CafeOrderRepository.kt +++ b/src/main/kotlin/com/example/domain/repository/CafeOrderRepository.kt @@ -9,9 +9,15 @@ import com.example.shared.dto.OrderDto import org.jetbrains.exposed.dao.id.EntityID import org.jetbrains.exposed.sql.ResultRow import org.jetbrains.exposed.sql.SortOrder +import org.jetbrains.exposed.sql.alias +import org.jetbrains.exposed.sql.castTo +import org.jetbrains.exposed.sql.count +import org.jetbrains.exposed.sql.javatime.JavaLocalDateColumnType import org.jetbrains.exposed.sql.selectAll import org.jetbrains.exposed.sql.statements.InsertStatement import org.jetbrains.exposed.sql.statements.UpdateStatement +import org.jetbrains.exposed.sql.sum +import java.time.LocalDate class CafeOrderRepository( override val table: CafeOrderTable, @@ -96,4 +102,34 @@ class CafeOrderRepository( ) } } + + /** + * SELECT CAST(CAFE_ORDER.ORDERED_AT AS DATE), + * COUNT(CAFE_ORDER.ID) count, + * SUM(CAFE_ORDER.PRICE) price + * FROM CAFE_ORDER + * GROUP BY CAST(CAFE_ORDER.ORDERED_AT AS DATE) + * ORDER BY CAST(CAFE_ORDER.ORDERED_AT AS DATE) DESC; + */ + fun findOrderStats(): List = dbQuery { + val countExpression = table.id.count().alias("count") + val priceSumExpression = table.price.sum().alias("price") + val orderDateExpression = table.orderedAt.castTo(JavaLocalDateColumnType()) + + table + .select( + orderDateExpression, + countExpression, + priceSumExpression + ) + .groupBy(orderDateExpression) + .orderBy(orderDateExpression to SortOrder.DESC) + .map { + OrderDto.StatsResponse( + orderDate = it[orderDateExpression], + totalOrderCount = it[countExpression], + totalOrderPrice = it[priceSumExpression]?.toLong() ?: 0 + ) + } + } } \ No newline at end of file diff --git a/src/main/kotlin/com/example/route/OrderRoute.kt b/src/main/kotlin/com/example/route/OrderRoute.kt index df3fa60..2bc8156 100644 --- a/src/main/kotlin/com/example/route/OrderRoute.kt +++ b/src/main/kotlin/com/example/route/OrderRoute.kt @@ -42,5 +42,9 @@ fun Route.orderRoute() { val orders: List = orderService.getOrders() call.respond(orders) } + get("/orders/stats") { + val stats = orderService.getOrderStats() + call.respond(stats) + } } } \ No newline at end of file diff --git a/src/main/kotlin/com/example/service/OrderService.kt b/src/main/kotlin/com/example/service/OrderService.kt index 6a25d25..cd0d173 100644 --- a/src/main/kotlin/com/example/service/OrderService.kt +++ b/src/main/kotlin/com/example/service/OrderService.kt @@ -94,4 +94,16 @@ class OrderService( fun getOrders(): List { return cafeOrderRepository.findByOrders() } + + fun getOrderStats(): List { + return cafeOrderRepository.findOrderStats() +// return cafeOrderRepository.findAll().groupBy { it.orderedAt.toLocalDate() } +// .map { +// OrderDto.StatsResponse( +// orderDate = it.key, +// totalOrderCount = it.value.size.toLong(), +// totalOrderPrice = it.value.sumOf { it.price }.toLong() +// ) +// }.sortedByDescending { it.orderDate } + } } diff --git a/src/main/kotlin/com/example/shared/dto/OrderDto.kt b/src/main/kotlin/com/example/shared/dto/OrderDto.kt index 0da9e14..079d0f4 100644 --- a/src/main/kotlin/com/example/shared/dto/OrderDto.kt +++ b/src/main/kotlin/com/example/shared/dto/OrderDto.kt @@ -1,8 +1,10 @@ package com.example.shared.dto import com.example.shared.CafeOrderStatus +import com.example.shared.LocalDateSerializer import com.example.shared.LocalDateTimeSerializer import kotlinx.serialization.Serializable +import java.time.LocalDate import java.time.LocalDateTime class OrderDto { @@ -23,4 +25,12 @@ class OrderDto { @Serializable data class UpdateStatusRequest(val status: CafeOrderStatus) + + @Serializable + data class StatsResponse( + @Serializable(with = LocalDateSerializer::class) + val orderDate: LocalDate, + val totalOrderCount: Long, + val totalOrderPrice: Long, + ) }