diff --git a/libs/api/openai.ts b/libs/api/openai.ts index b3ec401..cf84617 100644 --- a/libs/api/openai.ts +++ b/libs/api/openai.ts @@ -20,7 +20,7 @@ export class OpenAIAPI { role: "user", content: [ { - text: `ตอบกลับเป็น json\n{\n "word": "${word}",\n "thai": "...", //แปลคำว่า ${word} เป็นภาษาไทยของคำว่า \n "english": "...", //คำอ่านและการออกเสียง ${word} เป็นภาษาไทย\n "types": [...], //ประเภทของคำของคำว่า ${word}\n "examples": [...], //ตัวอย่างการใช้คำว่า ${word} ในประเภทคำต่างๆ\n}`, + text: `ตอบกลับเป็น json\n{\n "word": "${word}",\n "thai": "...", //แปลคำว่า ${word} เป็นภาษาไทยของคำว่า \n "english": "...", //คำอ่านและการออกเสียง ${word} เป็นภาษาไทย\n "types": [...], //ประเภทของคำของคำว่า ${word}\n "examples": [...], //ตัวอย่างการใช้คำว่า ${word} ในประเภทคำต่างๆ แค่ sentence เท่านั้น\n}`, type: "text", }, ], diff --git a/libs/mod/vocabulary/repository.ts b/libs/mod/vocabulary/repository.ts index 6206da5..2677ba1 100644 --- a/libs/mod/vocabulary/repository.ts +++ b/libs/mod/vocabulary/repository.ts @@ -7,7 +7,7 @@ import "reflect-metadata"; import type { TUpload, TVocabulary } from "./@types/index.ts"; export interface IVocabularyRepository { - findAll(): Promise; + findAll(): Promise; findByWord(word: string): Promise; insert(v: TVocabulary): Promise; upload(n: string, b: Buffer): Promise; @@ -27,6 +27,7 @@ export class VocabularyRepository implements IVocabularyRepository { const { data, error } = await this._db.storage .from("speech") .upload(`${name}`, buff, { + contentType: "audio/mpeg", cacheControl: "3600", }); if (error) { @@ -40,12 +41,12 @@ export class VocabularyRepository implements IVocabularyRepository { } } - public async findAll() { + public async findAll(): Promise { try { const { data, error } = await this._db.from(TableName).select("*"); if (error) { console.log(error); - return error; + throw new Error(`Failed to find all words: ${error.message}`); } return data; } catch (error) { @@ -56,6 +57,7 @@ export class VocabularyRepository implements IVocabularyRepository { public async findByWord(word: string) { try { + await this._db.auth.refreshSession(); const { data, error } = await this._db .from(TableName) .select("*") @@ -73,10 +75,10 @@ export class VocabularyRepository implements IVocabularyRepository { public async insert(v: TVocabulary) { try { - await this._db.auth.reauthenticate(); + await this._db.auth.refreshSession(); const { data, error } = await this._db .from(TableName) - .insert([{ ...v, type: "", remark: "" }]) + .insert([{ ...v }]) .select(); if (error) { console.error(error); diff --git a/libs/mod/vocabulary/routes.ts b/libs/mod/vocabulary/routes.ts index 22fcbe7..6672ac3 100644 --- a/libs/mod/vocabulary/routes.ts +++ b/libs/mod/vocabulary/routes.ts @@ -5,12 +5,17 @@ import authMiddleware from "../../utils/middleware/auth.middleware.ts"; const vocabularyRoutes = new Hono() .use("*", authMiddleware) - .post("/create", async (c) => { + .post("/", async (c) => { const body = await c.req.json<{ word: string }>(); const srv = container.get(Instances.VocabularyService); const res = await srv.newWord(body.word); return c.json(res); }) + .get("/", async (c) => { + const srv = container.get(Instances.VocabularyService); + const res = await srv.get(); + return c.json(res); + }) // .post("/speech", async (c) => { // const text = await c.req.query("text"); // if(!text) return c.json({ error: "text is required" }, 400); diff --git a/libs/mod/vocabulary/service.ts b/libs/mod/vocabulary/service.ts index 5bace4c..b4f2340 100644 --- a/libs/mod/vocabulary/service.ts +++ b/libs/mod/vocabulary/service.ts @@ -1,3 +1,4 @@ +import { Env } from '../../utils/config/index.ts'; import { injectable, inject } from "inversify"; import type { IVocabularyRepository } from "./repository.ts"; @@ -10,6 +11,7 @@ import type { TUpload, TVocabulary } from "./@types/index.ts"; export interface IVocabularyService { // insert(word: string): Promise; // speech(text: string): Promise; + get: () => Promise; newWord(word: string): Promise; } @@ -45,6 +47,7 @@ export class VocabularyService implements IVocabularyService { private async _insert(data: TVocabulary): Promise { try { + console.log(data); return await this._repo.insert({ ...data, remark: "-", @@ -58,6 +61,7 @@ export class VocabularyService implements IVocabularyService { public async findByWord(word: string): Promise { try { const v = await this._repo.findByWord(word); + // console.log("find by word",v); return v; } catch (error) { console.error(error); @@ -65,14 +69,28 @@ export class VocabularyService implements IVocabularyService { } } + public async get(): Promise { + try { + return await this._repo.findAll(); + } catch (error) { + console.error(error); + throw new Error(`Failed to get all words: ${error}`); + } + } + public async newWord(word: string): Promise { try { + if (!/^[a-zA-Z]+$/.test(word)) { + throw new Error("Word must contain only A-Z and a-z characters"); + } const v = await this.findByWord(word); if (v.length > 0) return v; const content = await this._askAiForMeening(word); + if(content === "") throw new Error("Failed to get content from AI"); const data: TVocabulary = JSON.parse(content); const speech = await this._askAiForSpeech(word); - const res = await this._insert({ ...data, speech_url: speech.fullPath }); + if (!speech.fullPath) throw new Error("Failed to get speech"); + const res = await this._insert({ ...data, content, speech_url: `${Env.supabaseBucketUrl}/${speech.fullPath}` }); return res; } catch (error) { console.error(error); diff --git a/libs/utils/config/index.ts b/libs/utils/config/index.ts index 1040984..846844a 100644 --- a/libs/utils/config/index.ts +++ b/libs/utils/config/index.ts @@ -7,4 +7,5 @@ export const Env = { supabaseToken: Deno.env.get("SUPABASE_TOKEN"), supabaseUser: Deno.env.get("SUPABASE_USER"), supabasePass: Deno.env.get("SUPABASE_PASS"), + supabaseBucketUrl: Deno.env.get("SUPABASE_BUCKET_URL"), };