Skip to content

Commit

Permalink
Merge pull request #2 from chanwit-y/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
chanwit-y authored Nov 28, 2024
2 parents 626cc7e + cdb9061 commit 3d63b89
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 8 deletions.
2 changes: 1 addition & 1 deletion libs/api/openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
],
Expand Down
12 changes: 7 additions & 5 deletions libs/mod/vocabulary/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import "reflect-metadata";
import type { TUpload, TVocabulary } from "./@types/index.ts";

export interface IVocabularyRepository {
findAll(): Promise<unknown>;
findAll(): Promise<TVocabulary[]>;
findByWord(word: string): Promise<TVocabulary[]>;
insert(v: TVocabulary): Promise<TVocabulary[]>;
upload(n: string, b: Buffer): Promise<TUpload>;
Expand All @@ -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) {
Expand All @@ -40,12 +41,12 @@ export class VocabularyRepository implements IVocabularyRepository {
}
}

public async findAll() {
public async findAll(): Promise<TVocabulary[]> {
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) {
Expand All @@ -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("*")
Expand All @@ -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);
Expand Down
7 changes: 6 additions & 1 deletion libs/mod/vocabulary/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<IVocabularyService>(Instances.VocabularyService);
const res = await srv.newWord(body.word);
return c.json(res);
})
.get("/", async (c) => {
const srv = container.get<IVocabularyService>(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);
Expand Down
20 changes: 19 additions & 1 deletion libs/mod/vocabulary/service.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Env } from '../../utils/config/index.ts';
import { injectable, inject } from "inversify";
import type { IVocabularyRepository } from "./repository.ts";

Expand All @@ -10,6 +11,7 @@ import type { TUpload, TVocabulary } from "./@types/index.ts";
export interface IVocabularyService {
// insert(word: string): Promise<TVocabulary[]>;
// speech(text: string): Promise<TUpload>;
get: () => Promise<TVocabulary[]>;
newWord(word: string): Promise<TVocabulary[]>;
}

Expand Down Expand Up @@ -45,6 +47,7 @@ export class VocabularyService implements IVocabularyService {

private async _insert(data: TVocabulary): Promise<TVocabulary[]> {
try {
console.log(data);
return await this._repo.insert({
...data,
remark: "-",
Expand All @@ -58,21 +61,36 @@ export class VocabularyService implements IVocabularyService {
public async findByWord(word: string): Promise<TVocabulary[]> {
try {
const v = await this._repo.findByWord(word);
// console.log("find by word",v);
return v;
} catch (error) {
console.error(error);
throw new Error(`Failed to check word: ${error}`);
}
}

public async get(): Promise<TVocabulary[]> {
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<TVocabulary[]> {
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);
Expand Down
1 change: 1 addition & 0 deletions libs/utils/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
};

0 comments on commit 3d63b89

Please sign in to comment.