fix: fetching entry is actually correct now
This commit is contained in:
parent
a7b73023d0
commit
76a058a312
@ -32,20 +32,12 @@ object Guestbook:
|
||||
os.read(config.entryCountPath).toInt
|
||||
def read(config: Config, from: Int, count: Int): F[Page] =
|
||||
val entryCount = entriesSize(config)
|
||||
// limit from to 1 cuz duh lol
|
||||
val startFrom = from.max(1)
|
||||
// limit count to however many entries there are
|
||||
val endAt = (startFrom + count - 1).min(entryCount).max(1)
|
||||
// if it wants us to start from after entries just return empty
|
||||
if startFrom > entryCount then
|
||||
return Page(entries = List.empty, hasNext = false).pure[F]
|
||||
logger.trace(s"want to read entries from $startFrom ($from) to $endAt ($from + $count)")
|
||||
val entryIds = (1 to entryCount).reverse.drop(from).take(count)
|
||||
// actually get the entries
|
||||
val entries = (startFrom to endAt)
|
||||
val entries = entryIds
|
||||
.map((no) => // read the entries
|
||||
val entryNo = entryCount - no + 1
|
||||
logger.trace(s"reading entry at $entryNo")
|
||||
entryNo -> decode[Entry](os.read(config.entriesPath / entryNo.toString)).getOrElse(
|
||||
logger.info(s"reading entry at $no")
|
||||
no -> decode[Entry](os.read(config.entriesPath / no.toString)).getOrElse(
|
||||
Entry(
|
||||
author = "error",
|
||||
content = "woops, this is an error!",
|
||||
@ -54,7 +46,7 @@ object Guestbook:
|
||||
)
|
||||
)
|
||||
.toList
|
||||
Page(entries, hasNext = entryCount > endAt).pure[F]
|
||||
Page(entries, hasNext = entries.last._1 > 1).pure[F]
|
||||
def write(config: Config, entry: Entry): F[Unit] =
|
||||
val entryNo = entriesSize(config) + 1
|
||||
val entryPath = config.entriesPath / entryNo.toString
|
||||
|
@ -32,7 +32,8 @@ class GuestbookRoutes(
|
||||
def routes(
|
||||
G: Guestbook[IO]
|
||||
): HttpRoutes[IO] =
|
||||
val putEntry = HttpRoutes.of[IO] { case req @ POST -> Root =>
|
||||
val putEntry = HttpRoutes.of[IO] {
|
||||
case req @ POST -> Root =>
|
||||
for {
|
||||
entry <- req.as[UrlForm].map { form =>
|
||||
val author = form.getFirstOrElse("author", "error")
|
||||
@ -44,9 +45,12 @@ class GuestbookRoutes(
|
||||
resp <- SeeOther(Location(websiteUri / "guestbook"))
|
||||
} yield resp
|
||||
}
|
||||
val getEntries = HttpRoutes.of[IO] { case GET -> Root / IntVar(page) =>
|
||||
object OffsetParam extends QueryParamDecoderMatcher[Int]("offset")
|
||||
object CountParam extends QueryParamDecoderMatcher[Int]("count")
|
||||
val getEntries = HttpRoutes.of[IO] {
|
||||
case GET -> Root :? OffsetParam(offset) +& CountParam(count) =>
|
||||
for {
|
||||
entries <- G.read(guestbookConfig, (page - 1).max(0) * 5, 5)
|
||||
entries <- G.read(guestbookConfig, offset, count)
|
||||
resp <- Ok(entries)
|
||||
} yield resp
|
||||
}
|
||||
|
@ -115,7 +115,9 @@ export async function load({ url, fetch, cookies }) {
|
||||
data.page = Math.max(data.page, 1)
|
||||
let respRaw: Response
|
||||
try {
|
||||
respRaw = await fetch(`${GUESTBOOK_BASE_URL}/${data.page}`)
|
||||
const count = 5
|
||||
const offset = (data.page - 1) * count
|
||||
respRaw = await fetch(`${GUESTBOOK_BASE_URL}?offset=${offset}&count=${count}`)
|
||||
} catch (err: any) {
|
||||
data.getError = `${err.toString()} (is guestbook server running?)`
|
||||
return data
|
||||
@ -126,7 +128,7 @@ export async function load({ url, fetch, cookies }) {
|
||||
try {
|
||||
body = await respRaw.json()
|
||||
} catch (err: any) {
|
||||
data.getError = err.toString()
|
||||
data.getError = `invalid body? (${err.toString()})`
|
||||
return data
|
||||
}
|
||||
data.entries = body.entries
|
||||
|
@ -94,7 +94,7 @@
|
||||
{#if hasPreviousPage || hasNextPage}
|
||||
<div class="flex flex-row w-full justify-center items-center font-monospace">
|
||||
{#if hasPreviousPage}
|
||||
<a href="/guestbook/?page={data.entries.length < 0 ? data.page - 1 : 1}"
|
||||
<a href="/guestbook/?page={data.entries.length > 0 ? data.page - 1 : 1}"
|
||||
><< previous</a
|
||||
>
|
||||
{/if}
|
||||
|
Loading…
Reference in New Issue
Block a user