package com.estateunified.panel import com.google.gson.Gson import com.google.gson.reflect.TypeToken import java.nio.charset.StandardCharsets internal data class NewsDto( val id: String, val title: String, val tags: List = emptyList(), val publishedAt: String = "", val source: String = "", val preview: String = "", val body: String = "", ) internal class NewsStore( private val gson: Gson, ) { private val items: List = load() fun all(): List = items fun get(id: String): NewsDto? = items.firstOrNull { it.id == id } private fun load(): List { val stream = this.javaClass.classLoader.getResourceAsStream("mock-news.json") ?: return emptyList() val text = stream.use { it.readBytes().toString(StandardCharsets.UTF_8) } return runCatching { gson.fromJson>(text, listType) ?: emptyList() } .getOrElse { emptyList() } } companion object { private val listType = object : TypeToken>() {}.type } }