Skip to content

Commit

Permalink
add response schema
Browse files Browse the repository at this point in the history
  • Loading branch information
carafelix committed Jun 28, 2024
1 parent 75218fa commit ab544fe
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 15 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# API de poesía

[![codecov](https://codecov.io/github/carafelix/poesia-api/graph/badge.svg?token=IFV2KESJWK)](https://codecov.io/github/carafelix/poesia-api)
Stack:

- Router: Hono
- OpenAPI solution: Chanafa
- Data: Xata, Drizzle, Zod
Expand Down
13 changes: 13 additions & 0 deletions src/db/zodSchemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,20 @@ export const PoemSchema = createSelectSchema(poems)
export const createAuthorSchema = createInsertSchema(authors)
export const AuthorSchema = createSelectSchema(authors)
export const AuthorsArr = z.array(AuthorSchema)
export type Author = z.TypeOf<typeof AuthorSchema>

// Books
export const createBookSchema = createInsertSchema(books)
export const BookSchema = createSelectSchema(books)
export type Book = z.TypeOf<typeof BookSchema>


// Response Schemas

export const PoemResponseSchema = z.object({
poemTitle: z.string(),
poemSubindex: z.number(),
poemText: z.string(),
bookTitle: z.string(),
authorName: z.string()
})
39 changes: 24 additions & 15 deletions src/endpoints/poems/poemaRand.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import * as schema from '../../db/drizzle/schema'
import * as tables from '../../db/drizzle/schema'
import { XataClient } from '../../db/xata'
import { PoemSchema } from '../../db/zodSchemas'
import { PoemResponseSchema, PoemSchema } from '../../db/zodSchemas'
import { drizzle } from 'drizzle-orm/xata-http'
import { Bindings } from 'types'
import { OpenAPIRoute } from 'chanfana'
import type { Context } from 'hono'
import { z } from 'zod'
import { sql } from 'drizzle-orm'
import { lte, eq } from 'drizzle-orm'

export class PoemRandom extends OpenAPIRoute {
schema = {
Expand All @@ -22,9 +22,7 @@ export class PoemRandom extends OpenAPIRoute {
responses: {
'200': {
description: 'Returns a Poem exact match or a list of fuzzy Poems',
schema: {
poem: PoemSchema,
},
schema: PoemResponseSchema
},
},
}
Expand All @@ -41,21 +39,32 @@ export class PoemRandom extends OpenAPIRoute {
})

const db = drizzle(xata, {
schema,
schema: tables,
})

let { length } = data.query
length ??= 500
length ??= 3000
try {
const result = await db.query.poems.findMany({
where(fields, { lte }) {
return lte(fields.length, length)
},
const poems = await db.select({
poemTitle: tables.poems.title,
poemSubindex: tables.poems.subindex,
poemText: tables.poems.text,
bookTitle: tables.books.title,
authorName: tables.authors.name
})
if (!result) {
return new Response('No poem Found', { status: 404 })
.from(tables.poems)
.where(lte(tables.poems.length, length))
.leftJoin(tables.books, eq(tables.books.xata_id, tables.poems.book))
.leftJoin(tables.authors, eq(tables.authors.xata_id, tables.books.author))
.execute()

const poem = poems[Math.floor(Math.random() * poems.length)]

if (!poem) {
return new Response('No Poem correctly Found', { status: 404 })
}
return result[Math.floor(Math.random() * result.length)]

return poem
} catch (error) {
console.log(error)
return new Response(JSON.stringify(data), { status: error.status })
Expand Down
2 changes: 2 additions & 0 deletions src/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Poem } from 'db/zodSchemas'

export type Bindings = {
POEMAS_DB: D1Database
TOKENS_KV: KVNamespace
Expand Down

0 comments on commit ab544fe

Please sign in to comment.