Skip to content

Commit c7a96b2

Browse files
committed
add a helpful api
1 parent 7e009e5 commit c7a96b2

File tree

3 files changed

+57
-14
lines changed

3 files changed

+57
-14
lines changed

backend/src/app.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ app.get("/api/elasticsearch/indices", async (_req, res) => {
5151
app.use("/api-docs", swaggerUi.serve, swaggerUi.setup(swaggerDocument))
5252

5353
app.post("/api/recipes/search", SearchController.searchRecipes)
54+
app.post("/api/recipes/search/some", SearchController.searchSomeRecipes)
5455

5556
app.get("/api/users/favorites", UserController.getFavorites)
5657
app.post("/api/users/favorites", UserController.addFavorite)

backend/src/controllers/SearchController.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,31 @@ class SearchController {
9191
}
9292
}
9393

94+
searchSomeRecipes = async (_req: Request, res: Response): Promise<void> => {
95+
try {
96+
const result = await elasticSearchClient.search({
97+
index: "recipes",
98+
body: {
99+
query: {
100+
match_all: {},
101+
},
102+
size: 30,
103+
},
104+
})
105+
106+
const hits = result.hits.hits
107+
if (hits.length === 0) {
108+
res.status(404).json({ error: "Not found" })
109+
return
110+
}
111+
const recipes = hits.map((hit) => hit._source)
112+
res.json(recipes)
113+
} catch (error) {
114+
console.error(error)
115+
res.status(500).json({ error: "Internal server error" })
116+
}
117+
}
118+
94119
// TODO: type=keywords
95120
private searchRecipesWithKeywords = async (req: Request, res: Response): Promise<void> => {
96121
try {

openapi.json

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
"title": {
2525
"type": "string"
2626
},
27+
"sourceUrl": {
28+
"type": "string"
29+
},
2730
"description": {
2831
"type": "string"
2932
},
@@ -36,15 +39,6 @@
3639
"type": "string"
3740
}
3841
},
39-
"keywords": {
40-
"type": "array",
41-
"items": {
42-
"type": "string"
43-
}
44-
},
45-
"sourceUrl": {
46-
"type": "string"
47-
},
4842
"foodImageUrl": {
4943
"type": "string"
5044
},
@@ -56,10 +50,6 @@
5650
},
5751
"cuisine": {
5852
"type": "string"
59-
},
60-
"createdAt": {
61-
"type": "string",
62-
"format": "date-time"
6353
}
6454
},
6555
"required": [
@@ -157,7 +147,18 @@
157147
"type": "object",
158148
"properties": {
159149
"recipe": {
160-
"example": "any"
150+
"example": {
151+
"id": 1,
152+
"title": "テストタイトル",
153+
"description": "豚肉の生姜焼きのレシピです。",
154+
"totalCookingTime": 30,
155+
"materials": ["豚肉", "生姜", "醤油", "みりん", "砂糖"],
156+
"sourceUrl": "https://google.com",
157+
"foodImageUrl": "https://google.com",
158+
"dish": "主菜",
159+
"category": "和食",
160+
"cuisine": "日本料理"
161+
}
161162
}
162163
}
163164
}
@@ -260,6 +261,22 @@
260261
}
261262
}
262263
},
264+
"/api/recipes/search/some": {
265+
"post": {
266+
"description": "",
267+
"responses": {
268+
"200": {
269+
"description": "OK"
270+
},
271+
"404": {
272+
"description": "Not Found"
273+
},
274+
"500": {
275+
"description": "Internal Server Error"
276+
}
277+
}
278+
}
279+
},
263280
"/api/users/favorites": {
264281
"get": {
265282
"description": "",

0 commit comments

Comments
 (0)