1
0

fix: fetching entry is actually correct now

This commit is contained in:
dusk 2024-08-24 21:06:55 +03:00
parent a7b73023d0
commit 76a058a312
Signed by: dusk
SSH Key Fingerprint: SHA256:Abmvag+juovVufZTxyWY8KcVgrznxvBjQpJesv071Aw
4 changed files with 17 additions and 19 deletions

View File

@ -32,20 +32,12 @@ object Guestbook:
os.read(config.entryCountPath).toInt os.read(config.entryCountPath).toInt
def read(config: Config, from: Int, count: Int): F[Page] = def read(config: Config, from: Int, count: Int): F[Page] =
val entryCount = entriesSize(config) val entryCount = entriesSize(config)
// limit from to 1 cuz duh lol val entryIds = (1 to entryCount).reverse.drop(from).take(count)
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)")
// actually get the entries // actually get the entries
val entries = (startFrom to endAt) val entries = entryIds
.map((no) => // read the entries .map((no) => // read the entries
val entryNo = entryCount - no + 1 logger.info(s"reading entry at $no")
logger.trace(s"reading entry at $entryNo") no -> decode[Entry](os.read(config.entriesPath / no.toString)).getOrElse(
entryNo -> decode[Entry](os.read(config.entriesPath / entryNo.toString)).getOrElse(
Entry( Entry(
author = "error", author = "error",
content = "woops, this is an error!", content = "woops, this is an error!",
@ -54,7 +46,7 @@ object Guestbook:
) )
) )
.toList .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] = def write(config: Config, entry: Entry): F[Unit] =
val entryNo = entriesSize(config) + 1 val entryNo = entriesSize(config) + 1
val entryPath = config.entriesPath / entryNo.toString val entryPath = config.entriesPath / entryNo.toString

View File

@ -32,7 +32,8 @@ class GuestbookRoutes(
def routes( def routes(
G: Guestbook[IO] G: Guestbook[IO]
): HttpRoutes[IO] = ): HttpRoutes[IO] =
val putEntry = HttpRoutes.of[IO] { case req @ POST -> Root => val putEntry = HttpRoutes.of[IO] {
case req @ POST -> Root =>
for { for {
entry <- req.as[UrlForm].map { form => entry <- req.as[UrlForm].map { form =>
val author = form.getFirstOrElse("author", "error") val author = form.getFirstOrElse("author", "error")
@ -44,9 +45,12 @@ class GuestbookRoutes(
resp <- SeeOther(Location(websiteUri / "guestbook")) resp <- SeeOther(Location(websiteUri / "guestbook"))
} yield resp } 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 { for {
entries <- G.read(guestbookConfig, (page - 1).max(0) * 5, 5) entries <- G.read(guestbookConfig, offset, count)
resp <- Ok(entries) resp <- Ok(entries)
} yield resp } yield resp
} }

View File

@ -115,7 +115,9 @@ export async function load({ url, fetch, cookies }) {
data.page = Math.max(data.page, 1) data.page = Math.max(data.page, 1)
let respRaw: Response let respRaw: Response
try { 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) { } catch (err: any) {
data.getError = `${err.toString()} (is guestbook server running?)` data.getError = `${err.toString()} (is guestbook server running?)`
return data return data
@ -126,7 +128,7 @@ export async function load({ url, fetch, cookies }) {
try { try {
body = await respRaw.json() body = await respRaw.json()
} catch (err: any) { } catch (err: any) {
data.getError = err.toString() data.getError = `invalid body? (${err.toString()})`
return data return data
} }
data.entries = body.entries data.entries = body.entries

View File

@ -94,7 +94,7 @@
{#if hasPreviousPage || hasNextPage} {#if hasPreviousPage || hasNextPage}
<div class="flex flex-row w-full justify-center items-center font-monospace"> <div class="flex flex-row w-full justify-center items-center font-monospace">
{#if hasPreviousPage} {#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}"
>&lt;&lt; previous</a >&lt;&lt; previous</a
> >
{/if} {/if}