Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cafe-12-end 셋팅 #12

Open
wants to merge 1 commit into
base: lecture/cafe-12-start
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<OrderDto.StatsResponse> = dbQuery {
val countExpression = table.id.count().alias("count")
val priceSumExpression = table.price.sum().alias("price")
val orderDateExpression = table.orderedAt.castTo<LocalDate>(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
)
}
}
}
4 changes: 4 additions & 0 deletions src/main/kotlin/com/example/route/OrderRoute.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,9 @@ fun Route.orderRoute() {
val orders: List<OrderDto.DisplayResponse> = orderService.getOrders()
call.respond(orders)
}
get("/orders/stats") {
val stats = orderService.getOrderStats()
call.respond(stats)
}
}
}
12 changes: 12 additions & 0 deletions src/main/kotlin/com/example/service/OrderService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,16 @@ class OrderService(
fun getOrders(): List<OrderDto.DisplayResponse> {
return cafeOrderRepository.findByOrders()
}

fun getOrderStats(): List<OrderDto.StatsResponse> {
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 }
}
}
10 changes: 10 additions & 0 deletions src/main/kotlin/com/example/shared/dto/OrderDto.kt
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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,
)
}