File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11import 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+ >
Original file line number Diff line number Diff line change 1- import { Elysia } from 'elysia'
1+ import { Elysia , t } from 'elysia'
22import { quoteService } from '@/src/module/quote/service'
3+ import { createQuoteSchema } from '@/src/module/quote/type'
34
45export 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 )
Original file line number Diff line number Diff line change 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
35class 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
Original file line number Diff line number Diff line change 1+ import { t } from "elysia"
2+
3+ export const createQuoteSchema = t . Object ( {
4+ text : t . String ( ) ,
5+ } )
You can’t perform that action at this time.
0 commit comments