Skip to content

Commit b8138ef

Browse files
committed
Ran "bunx drizzle-kit migrate" to apply changes.
then used db query to return db data
1 parent 34cd598 commit b8138ef

4 files changed

Lines changed: 47 additions & 18 deletions

File tree

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
11
import type { quotes } from "@/src/database/schema/quote/quote.schema"
22

3-
export type Quote = typeof quotes.$inferSelect
3+
export type Quote = typeof quotes.$inferSelect
4+
5+
export type NewQuote = Omit<
6+
Quote,
7+
"id"
8+
| "createdAt"
9+
| "updatedAt"
10+
| "deletedAt"
11+
| "isDeleted"
12+
>

‎backend/src/module/quote/controller.ts‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { Elysia } from 'elysia'
1+
import { Elysia, t } from 'elysia'
22
import { quoteService } from '@/src/module/quote/service'
3+
import { createQuoteSchema } from '@/src/module/quote/type'
34

45
export const quote = new Elysia({ prefix: '/quote' })
56
.get(
@@ -14,4 +15,20 @@ export const quote = new Elysia({ prefix: '/quote' })
1415
tags: ['quote'],
1516
}
1617
}
18+
)
19+
.post(
20+
'/',
21+
async ({ body, cookie: { session } }) => {
22+
const response = await quoteService.createQuote({
23+
text: body.text,
24+
})
25+
return response
26+
}, {
27+
body: createQuoteSchema,
28+
detail: {
29+
summary: 'Create a new quote',
30+
description: 'Create a new quote',
31+
tags: ['quote'],
32+
}
33+
}
1734
)

‎backend/src/module/quote/service.ts‎

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
1-
// import type { Quote } from '@/model/quote.model'
1+
import { db } from '@/src/database'
2+
import { table } from '@/src/database/schema/quote/quote.schema'
3+
import type { NewQuote, Quote } from '@/src/database/model/quote/quote.model'
24

35
class QuoteService {
4-
async getQuotes(): Promise<any[]> {
5-
return [
6-
{
7-
id: 1,
8-
text: 'The only way to do great work is to love what you do.'
9-
},
10-
{
11-
id: 2,
12-
text: 'The best way to predict the future is to invent it.'
13-
},
14-
{
15-
id: 3,
16-
text: 'The only way to do great work is to love what you do.'
17-
}
18-
]
6+
async getQuotes(): Promise<Quote[]> {
7+
const quotes = await db.select().from(table.quotes)
8+
return quotes
9+
}
10+
async createQuote(quote: NewQuote): Promise<Quote> {
11+
const newQuote = await db.insert(table.quotes).values({
12+
text: quote.text,
13+
createdAt: new Date(),
14+
updatedAt: new Date(),
15+
}).returning()
16+
return newQuote[0]
1917
}
2018
}
2119

‎backend/src/module/quote/type.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { t } from "elysia"
2+
3+
export const createQuoteSchema = t.Object({
4+
text: t.String(),
5+
})

0 commit comments

Comments
 (0)