Skip to content

Commit 20af8d1

Browse files
committed
added update quote api
1 parent 7f781ba commit 20af8d1

3 files changed

Lines changed: 36 additions & 7 deletions

File tree

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

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Elysia, t } from 'elysia'
22
import { quoteService } from '@/src/module/quote/service'
3-
import { createQuoteSchema, getQuoteSchema } from '@/src/module/quote/type'
3+
import { createQuoteSchema, getQuoteSchema, updateQuoteSchema } from '@/src/module/quote/type'
44

55
export const quote = new Elysia({ prefix: '/quote' })
66
.get(
@@ -17,19 +17,30 @@ export const quote = new Elysia({ prefix: '/quote' })
1717
}
1818
}
1919
)
20-
.get(
21-
'/',
22-
async ({ body, cookie: { session } }) => {
23-
const response = await quoteService.getQuotes()
20+
.patch(
21+
'/:id',
22+
async ({ params, body, cookie: { session } }) => {
23+
const response = await quoteService.updateQuote(params.id, body.text)
2424
return response
2525
}, {
26+
params: getQuoteSchema,
27+
body: updateQuoteSchema,
28+
detail: {
29+
summary: 'Update a quote by id',
30+
description: 'Update a quote by id',
31+
tags: ['quote'],
32+
}
33+
})
34+
.get("/", async ({ cookie: { session } }) => {
35+
const response = await quoteService.getQuotes()
36+
return response
37+
}, {
2638
detail: {
2739
summary: 'Get all quotes',
2840
description: 'Get all quotes',
2941
tags: ['quote'],
3042
}
31-
}
32-
)
43+
})
3344
.post(
3445
'/',
3546
async ({ body, cookie: { session } }) => {

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,20 @@ class QuoteService {
2424
}).returning()
2525
return newQuote[0]
2626
}
27+
async updateQuote(id: number, text: string): Promise<Quote> {
28+
const updatedQuote = await db.update(table.quotes).set({
29+
text: text,
30+
updatedAt: new Date(),
31+
}).where(eq(table.quotes.id, id)).returning()
32+
return updatedQuote[0]
33+
}
34+
async deleteQuote(id: number): Promise<Quote> {
35+
const deletedQuote = await db.update(table.quotes).set({
36+
deletedAt: new Date(),
37+
isDeleted: true,
38+
}).where(eq(table.quotes.id, id)).returning()
39+
return deletedQuote[0]
40+
}
2741
}
2842

2943
export const quoteService = new QuoteService()

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,8 @@ export const createQuoteSchema = t.Object({
66

77
export const getQuoteSchema = t.Object({
88
id: t.Number(),
9+
})
10+
11+
export const updateQuoteSchema = t.Object({
12+
text: t.String(),
913
})

0 commit comments

Comments
 (0)