加上新增整月的表格
This commit is contained in:
+27
@@ -11,6 +11,8 @@ import com.mongodb.client.model.Filters.and
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
import org.bson.Document
|
||||
import java.time.LocalDate
|
||||
import java.time.YearMonth
|
||||
|
||||
class BattleRecordService(private val database: MongoDatabase) {
|
||||
var collection: MongoCollection<Document>
|
||||
@@ -70,4 +72,29 @@ class BattleRecordService(private val database: MongoDatabase) {
|
||||
collection.insertMany(emptyBattleRecordList.map { it.toDocument() })
|
||||
collection.find(Filters.eq("date", date)).toList().map(BattleRecordDto::fromDocument)
|
||||
}
|
||||
|
||||
suspend fun createTableMonth(date: String): List<List<BattleRecordDto>> = withContext(Dispatchers.IO) {
|
||||
val memberList = memberCollection.find(Filters.eq("leave", false)).toList().map(MemberDto::fromDocument)
|
||||
|
||||
// 1. 指定年份與月份(例如:2026年8月)
|
||||
val dateSplit = date.split("-").map { it.toInt() }
|
||||
val yearMonth = YearMonth.of(dateSplit[0], dateSplit[1])
|
||||
|
||||
// 2. 取得該月最後一天的 LocalDate 物件
|
||||
val lastDayOfMonth = yearMonth.atEndOfMonth()
|
||||
|
||||
// 3. 取得最後 5 天的日期列表(由舊到新排序)
|
||||
val lastFiveDays: List<LocalDate> = (4 downTo 0).map { lastDayOfMonth.minusDays(it.toLong()) }
|
||||
|
||||
// 4. Temp list
|
||||
val tables = mutableListOf<List<BattleRecordDto>>()
|
||||
|
||||
// 5. 寫入資料
|
||||
lastFiveDays.forEach { date ->
|
||||
val emptyBattleRecordList = memberList.map { it.toEmptyBattleRecordDto(date.toString()) }
|
||||
tables.add(emptyBattleRecordList)
|
||||
collection.insertMany(emptyBattleRecordList.map { it.toDocument() })
|
||||
}
|
||||
tables
|
||||
}
|
||||
}
|
||||
|
||||
@@ -130,12 +130,19 @@ fun Application.configureMongo() {
|
||||
}
|
||||
}
|
||||
|
||||
post("/battle_record/create_table/{date}") {
|
||||
post("/battle_record/create_table_day/{date}") {
|
||||
val date = call.parameters["date"] ?: throw IllegalArgumentException("No date")
|
||||
battleRecordService.createTable(date).let { battleRecord ->
|
||||
call.respond(HttpStatusCode.OK, battleRecord)
|
||||
}
|
||||
}
|
||||
|
||||
post("/battle_record/create_table_month/{date}") {
|
||||
val date = call.parameters["date"] ?: throw IllegalArgumentException("No date")
|
||||
battleRecordService.createTableMonth(date).let { battleRecordList ->
|
||||
call.respond(HttpStatusCode.OK, battleRecordList)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user