Skip to content

Commit 7e009e5

Browse files
committed
redesign api for more simple and intuitive operations
1 parent 37ab6fd commit 7e009e5

File tree

5 files changed

+103
-247
lines changed

5 files changed

+103
-247
lines changed

backend/src/app.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,19 +51,17 @@ 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/keywords", SearchController.searchRecipesWithKeywords)
55-
app.post("/api/recipes/search/query", SearchController.searchRecipesWithQuery)
5654

5755
app.get("/api/users/favorites", UserController.getFavorites)
5856
app.post("/api/users/favorites", UserController.addFavorite)
5957
app.delete("/api/users/favorites/:id", UserController.deleteFavorite)
6058
app.get("/api/users", UserController.getUser)
6159

62-
app.post("/api/recipes/:index", RecipeController.recreateIndex)
60+
app.post("/api/recipes/index/recreate", RecipeController.recreateIndex)
6361
app.post("/api/recipes", RecipeController.indexRecipe)
6462
app.post("/api/recipes/bulk", RecipeController.bulkIndexRecipes)
6563
app.delete("/api/recipes/:id", RecipeController.deleteRecipe)
6664
app.post("/api/recipes/new", RecipeController.createRecipe)
67-
app.post("/api/recipes/scrape", RecipeController.scrapeRecipe)
65+
// app.post("/api/recipes/scrape", RecipeController.scrapeRecipe)
6866

6967
export default app

backend/src/controllers/RecipeController.ts

Lines changed: 50 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ class RecipeController {
3737
}
3838
}
3939

40-
recreateIndex = async (req: Request, res: Response): Promise<void> => {
40+
recreateIndex = async (_req: Request, res: Response): Promise<void> => {
4141
try {
42-
const indexName = req.params.index
42+
const indexName = "recipes"
4343
const indexExists = await elasticSearchClient.indices.exists({
4444
index: indexName,
4545
})
@@ -452,59 +452,59 @@ class RecipeController {
452452
}
453453
}
454454

455-
scrapeRecipe = async (req: Request, res: Response): Promise<void> => {
456-
try {
457-
const userFromRequest = await extractUserFromRequest(req)
458-
if (!userFromRequest) {
459-
// res.status(401).json({ error: "Not authorized" })
460-
// return
461-
console.log("Not authorized")
462-
}
455+
// scrapeRecipe = async (req: Request, res: Response): Promise<void> => {
456+
// try {
457+
// const userFromRequest = await extractUserFromRequest(req)
458+
// if (!userFromRequest) {
459+
// // res.status(401).json({ error: "Not authorized" })
460+
// // return
461+
// console.log("Not authorized")
462+
// }
463463

464-
// const { sourceUrl } = req.body
464+
// // const { sourceUrl } = req.body
465465

466-
// const browser = await chromium.launch()
467-
// const context = await browser.newContext()
468-
// const page = await context.newPage()
469-
// // Ref) https://playwright.dev/docs/api/class-page#page-goto
470-
// await page.goto(sourceUrl, { waitUntil: "domcontentloaded" })
466+
// // const browser = await chromium.launch()
467+
// // const context = await browser.newContext()
468+
// // const page = await context.newPage()
469+
// // // Ref) https://playwright.dev/docs/api/class-page#page-goto
470+
// // await page.goto(sourceUrl, { waitUntil: "domcontentloaded" })
471471

472-
// const recipeData = await page.evaluate(() => {
473-
// // FIXME: 複数のタグがある場合に対応させる
474-
// const scriptTag = document.querySelector("script[type='application/ld+json']")
475-
// if (!scriptTag) {
476-
// return null
477-
// }
478-
// const recipeJson = JSON.parse(scriptTag.innerHTML)
479-
// if (recipeJson["@type"] !== "Recipe") {
480-
// return null
481-
// }
482-
// return recipeJson
483-
// })
472+
// // const recipeData = await page.evaluate(() => {
473+
// // // FIXME: 複数のタグがある場合に対応させる
474+
// // const scriptTag = document.querySelector("script[type='application/ld+json']")
475+
// // if (!scriptTag) {
476+
// // return null
477+
// // }
478+
// // const recipeJson = JSON.parse(scriptTag.innerHTML)
479+
// // if (recipeJson["@type"] !== "Recipe") {
480+
// // return null
481+
// // }
482+
// // return recipeJson
483+
// // })
484484

485-
// await browser.close()
485+
// // await browser.close()
486486

487-
// // console.log(recipeData)
488-
// if (!recipeData) {
489-
// res.status(400).json({ error: "Could not find structured recipe data" })
490-
// return
491-
// }
492-
// const recipeResponse = {
493-
// title: recipeData.name,
494-
// description: recipeData.description,
495-
// totalCookingTime: this.convertTotalCookingTimeToMinutes(recipeData.totalTime),
496-
// materials: recipeData.recipeIngredient,
497-
// keywords: this.convertKeywords(recipeData.keywords),
498-
// sourceUrl: sourceUrl,
499-
// foodImageUrl: recipeData.image[0],
500-
// dish: recipeData.recipeCategory,
501-
// }
502-
// res.status(200).json(recipeResponse)
503-
} catch (error) {
504-
console.error(error)
505-
res.status(500).json({ error: "Internal server error" })
506-
}
507-
}
487+
// // // console.log(recipeData)
488+
// // if (!recipeData) {
489+
// // res.status(400).json({ error: "Could not find structured recipe data" })
490+
// // return
491+
// // }
492+
// // const recipeResponse = {
493+
// // title: recipeData.name,
494+
// // description: recipeData.description,
495+
// // totalCookingTime: this.convertTotalCookingTimeToMinutes(recipeData.totalTime),
496+
// // materials: recipeData.recipeIngredient,
497+
// // keywords: this.convertKeywords(recipeData.keywords),
498+
// // sourceUrl: sourceUrl,
499+
// // foodImageUrl: recipeData.image[0],
500+
// // dish: recipeData.recipeCategory,
501+
// // }
502+
// // res.status(200).json(recipeResponse)
503+
// } catch (error) {
504+
// console.error(error)
505+
// res.status(500).json({ error: "Internal server error" })
506+
// }
507+
// }
508508

509509
// eslint-disable-next-line @typescript-eslint/no-explicit-any
510510
private isRecipe = (data: any): data is Recipes => {

backend/src/controllers/SearchController.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ class SearchController {
9191
}
9292
}
9393

94-
searchRecipesWithKeywords = async (req: Request, res: Response): Promise<void> => {
94+
// TODO: type=keywords
95+
private searchRecipesWithKeywords = async (req: Request, res: Response): Promise<void> => {
9596
try {
9697
const { keywords } = req.body
9798

@@ -122,7 +123,8 @@ class SearchController {
122123
}
123124
}
124125

125-
searchRecipesWithQuery = async (req: Request, res: Response): Promise<void> => {
126+
// TODO: type=query
127+
private searchRecipesWithQuery = async (req: Request, res: Response): Promise<void> => {
126128
try {
127129
const { query } = req.body
128130
const result = await elasticSearchClient.search({

openapi.json

Lines changed: 47 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -121,46 +121,22 @@
121121
}
122122
}
123123
},
124-
"/api/recipes": {
125-
"post": {
124+
"/api/elasticsearch/indices": {
125+
"get": {
126126
"description": "",
127-
"requestBody": {
128-
"content": {
129-
"application/json": {
130-
"schema": {
131-
"type": "object",
132-
"properties": {
133-
"recipe": {
134-
"example": "any"
135-
}
136-
}
137-
}
138-
}
139-
}
140-
},
141127
"responses": {
142-
"201": {
143-
"description": "Created"
128+
"200": {
129+
"description": "OK"
144130
},
145131
"500": {
146132
"description": "Internal Server Error"
147133
}
148134
}
149135
}
150136
},
151-
"/api/recipes/{id}": {
152-
"delete": {
137+
"/api/recipes/index/recreate": {
138+
"post": {
153139
"description": "",
154-
"parameters": [
155-
{
156-
"name": "id",
157-
"in": "path",
158-
"required": true,
159-
"schema": {
160-
"type": "string"
161-
}
162-
}
163-
],
164140
"responses": {
165141
"200": {
166142
"description": "OK"
@@ -171,73 +147,84 @@
171147
}
172148
}
173149
},
174-
"/api/recipes/search": {
150+
"/api/recipes": {
175151
"post": {
176-
"description": "",
152+
"description": "index recipe",
177153
"requestBody": {
178154
"content": {
179155
"application/json": {
180156
"schema": {
181157
"type": "object",
182158
"properties": {
183-
"materials": {
184-
"example": ["豚肉"]
185-
},
186-
"dish": {
187-
"example": "主菜"
188-
},
189-
"cookingTime": {
190-
"example": "なんでも"
159+
"recipe": {
160+
"example": "any"
191161
}
192162
}
193163
}
194164
}
195165
}
196166
},
197167
"responses": {
198-
"200": {
199-
"description": "OK"
200-
},
201-
"404": {
202-
"description": "Not Found"
168+
"201": {
169+
"description": "Created"
203170
},
204171
"500": {
205172
"description": "Internal Server Error"
206173
}
207174
}
208175
}
209176
},
210-
"/api/recipes/search/keywords": {
177+
"/api/recipes/bulk": {
211178
"post": {
212-
"description": "",
179+
"description": "index recipe in bulk",
213180
"requestBody": {
214181
"content": {
215182
"application/json": {
216183
"schema": {
217184
"type": "object",
218185
"properties": {
219-
"keywords": {
186+
"recipe": {
220187
"example": "any"
221188
}
222189
}
223190
}
224191
}
225192
}
226193
},
194+
"responses": {
195+
"201": {
196+
"description": "Created"
197+
},
198+
"500": {
199+
"description": "Internal Server Error"
200+
}
201+
}
202+
}
203+
},
204+
"/api/recipes/{id}": {
205+
"delete": {
206+
"description": "delete recipe",
207+
"parameters": [
208+
{
209+
"name": "id",
210+
"in": "path",
211+
"required": true,
212+
"schema": {
213+
"type": "string"
214+
}
215+
}
216+
],
227217
"responses": {
228218
"200": {
229219
"description": "OK"
230220
},
231-
"404": {
232-
"description": "Not Found"
233-
},
234221
"500": {
235222
"description": "Internal Server Error"
236223
}
237224
}
238225
}
239226
},
240-
"/api/recipes/search/query": {
227+
"/api/recipes/search": {
241228
"post": {
242229
"description": "",
243230
"requestBody": {
@@ -246,8 +233,14 @@
246233
"schema": {
247234
"type": "object",
248235
"properties": {
249-
"query": {
250-
"example": "any"
236+
"materials": {
237+
"example": ["豚肉"]
238+
},
239+
"dish": {
240+
"example": "主菜"
241+
},
242+
"cookingTime": {
243+
"example": "なんでも"
251244
}
252245
}
253246
}

0 commit comments

Comments
 (0)