Skip to content

Commit e0c47b0

Browse files
committed
feat: カテゴリIDに基づいてカテゴリ情報を取得するAPIエンドポイントを追加し、アイテム登録時にカテゴリ情報を使用
1 parent 5e7bd34 commit e0c47b0

3 files changed

Lines changed: 35 additions & 2 deletions

File tree

frontend/src/app/api/category/getbycategoryid/route.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { getCosmosItemsByCategoryId } from "../../../../util/cosmos/document";
99
*/
1010
export const GET = async (req: NextRequest) => {
1111
try {
12-
const { searchParams } = new URL(req.url);
12+
const { searchParams } = req.nextUrl; // 修正: req.url から req.nextUrl に変更
1313
const categoryId = searchParams.get("categoryId");
1414

1515
if (!categoryId || typeof categoryId !== "string") {

frontend/src/app/api/document/route.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { registerItem } from "../../../util/cosmos/document";
33
import { getEmbedding, getChatCompletions } from "../../../util/openai";
44
import { v4 as uuidv4 } from "uuid";
55
import { deleteItem } from "../../../util/cosmos/document"; // 修正: deleteItemをインポート
6+
import { getCategoryById } from "../../../util/cosmos/category"
67

78
/**
89
* POSTメソッドで新しいアイテムを登録するAPIエンドポイント
@@ -19,8 +20,13 @@ export const POST = async (req: NextRequest) => {
1920
return NextResponse.json({ message: "Invalid input" }, { status: 400 });
2021
}
2122

23+
console.log("🚀Registering item with category ID:", category_id, "and content:", content);
24+
25+
// カテゴリIDからカテゴリ情報を取得
26+
const category = await getCategoryById(category_id);
27+
2228
// OpenAIのAPIを使用してコンテンツのベクトルを取得
23-
const contentVector = await getEmbedding(content);
29+
const contentVector = await getEmbedding(`${category?.category} \n${content}`);
2430

2531
// OpenAIのAPIを使用してドキュメントタイトルを取得
2632
const systemMessage = `あなたは優秀なライターです。与えられた内容からタイトルを生成してください。\n\n- タイトルのみを出力してください。「」など不要です。`;

frontend/src/util/cosmos/category.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,33 @@ export const getAllCategories = async (): Promise<CategoryItem[]> => {
2222
});
2323
};
2424

25+
/**
26+
* categoryをIDで取得する
27+
* @param id - カテゴリのID
28+
* @returns - カテゴリ情報
29+
*/
30+
export const getCategoryById = async (id: string): Promise<CategoryItem | null> => {
31+
return new Promise(async (resolve, reject) => {
32+
const cosmosClient = new CosmosClient(process.env.COSMOS_CONNECTION_STRING!);
33+
const database = cosmosClient.database(process.env.COSMOS_DATABASE_NAME!);
34+
const container = database.container(process.env.COSMOS_CATEGORY_CONTAINER_NAME!);
35+
36+
try {
37+
const { resource } = await container.item(id, id).read<CategoryItem>();
38+
console.log("🚀Category retrieved successfully.");
39+
resolve(resource || null); // カテゴリが見つからない場合はnullを返す
40+
} catch (error) {
41+
if ((error as any).code === 404) {
42+
console.log("🚀Category not found.");
43+
resolve(null);
44+
} else {
45+
console.error("🚀Error retrieving category:", error);
46+
reject(error);
47+
}
48+
}
49+
});
50+
}
51+
2552
/**
2653
* categoryを登録する
2754
* @param category - 登録するカテゴリ

0 commit comments

Comments
 (0)