Skip to content

Commit 9964891

Browse files
authored
Merge pull request #148 from kc3hack/develop
Develop
2 parents c69a853 + cfe3f21 commit 9964891

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+972
-73
lines changed

backend/.env.example

+4
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,7 @@ GEMINI_API_KEY=
1313
S3_ACCESS_KEY_ID=
1414
S3_SECRET_ACCESS_KEY=
1515
S3_BUCKET_NAME=
16+
17+
CURRENTS_API_KEY=
18+
NEWS_POST_API_KEY=
19+
OUR_ID=

backend/src/config/env.ts

+4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ const envSchema = z.object({
88
FRONTEND_URL: z.string().url(),
99
PORT: z.coerce.number().optional(),
1010
GEMINI_API_KEY: z.string(),
11+
CURRENTS_API_KEY: z.string(),
12+
NEWS_POST_API_KEY: z.string(),
13+
NEWS_USER_ID: z.string(),
1114
RDB_HOST: z.string(),
1215
RDB_USER: z.string(),
1316
RDB_PASSWORD: z.string(),
@@ -18,6 +21,7 @@ const envSchema = z.object({
1821
S3_ACCESS_KEY_ID: z.string(),
1922
S3_SECRET_ACCESS_KEY: z.string(),
2023
S3_BUCKET_NAME: z.string(),
24+
OUR_ID: z.string(),
2125
});
2226

2327
export const env = envSchema.parse(process.env);

backend/src/controllers/Miyabi/createMiyabiHandler.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ const createMiyabiHandler: RouteHandler<typeof createMiyabiRoute, {}> = async (c
4040
200
4141
);
4242
} catch (err) {
43-
console.log('雅に失敗しました.');
43+
console.log('雅に失敗しました.' + err);
4444
return c.json(
4545
{
4646
message: '雅に失敗しました.',

backend/src/controllers/Miyabi/deleteMiyabiHandler.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ const deleteMiyabiHandler: RouteHandler<typeof deleteMiyabiRoute, {}> = async (c
4040
200
4141
);
4242
} catch (err) {
43-
console.log('雅の削除に失敗しました.');
43+
console.log('雅の削除に失敗しました.' + err);
4444
return c.json(
4545
{
4646
message: '雅の削除に失敗しました.',

backend/src/controllers/Miyabi/getMiyabiRankingHandler.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ const getMiyabiRankingHandler: RouteHandler<typeof getMiyabiRankingRoute, {}> =
6161
200
6262
);
6363
} catch (err) {
64-
console.log('雅ランキングの取得に失敗しました.');
64+
console.log('雅ランキングの取得に失敗しました.' + err);
6565
return c.json(
6666
{
6767
message: '雅ランキングの取得に失敗しました.',

backend/src/controllers/Post/deletePostHandler.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ const deletePostHandler: RouteHandler<typeof deletePostRoute, {}> = async (c: Co
5656
200
5757
);
5858
} catch (err) {
59-
console.log('投稿の削除に失敗しました.');
59+
console.log('投稿の削除に失敗しました.' + err);
6060
return c.json(
6161
{
6262
message: '投稿の削除に失敗しました.',
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import { z, type RouteHandler } from '@hono/zod-openapi';
2+
import type { Context } from 'hono';
3+
import db from '../db.js';
4+
import { getOnePostSchema } from '../../schema/Post/getOnePostSchema.js';
5+
import type { getOnePostRoute } from '../../routes/Post/getOnePostRoute.js';
6+
import { env } from '../../config/env.js';
7+
8+
type getOnePostSchema = z.infer<typeof getOnePostSchema>;
9+
10+
const getOnePostHandler: RouteHandler<typeof getOnePostRoute, {}> = async (c: Context) => {
11+
try {
12+
// 受け取ったjsonを各変数に格納 (post_idが指定なしなら,最新の投稿idになる)
13+
let { my_icon = null, post_id } = await c.req.json<getOnePostSchema>();
14+
15+
// 入力のpost_idがnullなら最新の投稿から取得,そうでなければその投稿よりも古いものを取得
16+
// sql文中の比較条件切り替え
17+
18+
// 投稿が存在するかチェック
19+
const checkSql = `SELECT * FROM ${env.POSTS_TABLE_NAME} WHERE id = :post_id;`;
20+
const existingPosts = await db.query(checkSql, { post_id });
21+
if (existingPosts.length == 0) {
22+
console.log('投稿が見つかりません.');
23+
return c.json(
24+
{
25+
message: '投稿が見つかりません.',
26+
statusCode: 404,
27+
error: 'Not Found',
28+
},
29+
404
30+
);
31+
}
32+
33+
let results;
34+
// ここからDBのpostテーブルから情報取得
35+
const sql = `
36+
SELECT
37+
post.id,
38+
post.original,
39+
post.tanka,
40+
post.image_path,
41+
post.created_at,
42+
post.user_name,
43+
post.user_icon,
44+
(SELECT COUNT(*) FROM ${env.MIYABI_TABLE_NAME} WHERE post_id = post.id) as miyabi_count,
45+
CASE WHEN miyabi.id IS NULL THEN false ELSE true END as is_miyabi
46+
FROM ${env.POSTS_TABLE_NAME} post
47+
LEFT JOIN ${env.MIYABI_TABLE_NAME} miyabi
48+
ON post.id = miyabi.post_id AND miyabi.user_icon = :my_icon
49+
WHERE post.id = :post_id;
50+
`;
51+
52+
results = await db.query(sql, { my_icon, post_id });
53+
54+
// is_miyabiをtrue or falseで返すための処理
55+
results = results.map((row: any) => ({
56+
...row,
57+
user_id: row.user_icon.match(/\/u\/(\d+)/)[1],
58+
is_miyabi: row.is_miyabi ? true : false,
59+
}));
60+
61+
//console.log(results);
62+
63+
// レスポンス
64+
console.log('投稿を取得しました.');
65+
return c.json(
66+
{
67+
message: '投稿を取得しました.',
68+
id: results[0].id,
69+
original: results[0].original,
70+
tanka: results[0].tanka,
71+
image_path: results[0].image_path,
72+
created_at: results[0].created_at,
73+
user_id: results[0].user_id,
74+
user_name: results[0].user_name,
75+
user_icon: results[0].user_icon,
76+
miyabi_count: results[0].miyabi_count,
77+
is_miyabi: results[0].is_miyabi,
78+
},
79+
200
80+
);
81+
} catch (err) {
82+
console.log('投稿の取得に失敗しました.' + err);
83+
return c.json(
84+
{
85+
message: '投稿の取得に失敗しました.',
86+
statusCode: 500,
87+
error: 'Internal Server Error',
88+
},
89+
500
90+
);
91+
}
92+
};
93+
94+
export default getOnePostHandler;

backend/src/controllers/Post/getPostHandler.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ const getPostHandler: RouteHandler<typeof getPostRoute, {}> = async (c: Context)
103103
200
104104
);
105105
} catch (err) {
106-
console.log('投稿の取得に失敗しました.');
106+
console.log('投稿の取得に失敗しました.' + err);
107107
return c.json(
108108
{
109109
message: '投稿の取得に失敗しました.',

backend/src/controllers/Profile/getProfileHandler.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ const getProfileHandler: RouteHandler<typeof getProfileRoute, {}> = async (c: Co
5454
200
5555
);
5656
} catch (err) {
57-
console.log('プロフィールの取得に失敗しました.');
57+
console.log('プロフィールの取得に失敗しました.' + err);
5858
return c.json(
5959
{
6060
message: 'プロフィールの取得に失敗しました.',
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { z, type RouteHandler } from '@hono/zod-openapi';
2+
import type { Context } from 'hono';
3+
import { sampleNewsTankaSchema } from '../../schema/Sample/sampleNewsTankaSchema.js';
4+
import type { sampleNewsTankaRoute } from '../../routes/Sample/sampleNewsTankaRoute.js';
5+
import postNews from '../../lib/postNews.js';
6+
7+
type sampleNewsTankaSchema = z.infer<typeof sampleNewsTankaSchema>;
8+
9+
const sampleNewsTankaHandler: RouteHandler<typeof sampleNewsTankaRoute, {}> = async (
10+
c: Context
11+
) => {
12+
const { apiKey } = await c.req.json<sampleNewsTankaSchema>();
13+
14+
/* --- 色々処理 --- */
15+
16+
const response = await postNews(apiKey);
17+
console.log(response);
18+
19+
// レスポンス
20+
return c.json(response, 200);
21+
};
22+
23+
export default sampleNewsTankaHandler;

backend/src/controllers/User/createUserHandler.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ const createUserHandler: RouteHandler<typeof createUserRoute, {}> = async (c: Co
5454
200
5555
);
5656
} catch (err) {
57-
console.log('ユーザの追加に失敗しました.');
57+
console.log('ユーザの追加に失敗しました.' + err);
5858
return c.json(
5959
{
6060
message: 'ユーザの追加に失敗しました.',
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import { z, type RouteHandler } from '@hono/zod-openapi';
2+
import type { Context } from 'hono';
3+
import { isOurAccountSchema } from '../schema/isOurAccountSchema.js';
4+
import type { isOurAccountRoute } from '../routes/isOurAccountRoute.js';
5+
import { env } from '../config/env.js';
6+
7+
type isOurAccountSchema = z.infer<typeof isOurAccountSchema>;
8+
9+
const isOurAccountHandler: RouteHandler<typeof isOurAccountRoute, {}> = async (c: Context) => {
10+
try {
11+
// 受け取ったjsonを変数に格納
12+
const { icon_url } = await c.req.json<isOurAccountSchema>();
13+
14+
// 数字列の抽出
15+
const match = icon_url.match(/\/u\/(\d+)/);
16+
17+
// githubのicon_urlか判定
18+
if (!match) {
19+
console.log('エラー: GitHubのicon_urlを入力してください.');
20+
return c.json(
21+
{
22+
message: 'エラー: GitHubのicon_urlを入力してください.',
23+
statusCode: 500,
24+
error: 'エラー: GitHubのicon_urlを入力してください.',
25+
},
26+
500
27+
);
28+
}
29+
30+
const id = match[1];
31+
const our_id_str = env.OUR_ID;
32+
const our_idArray = our_id_str.split(',').map((item) => String(item.trim()));
33+
34+
// envファイルのour_idに含まれる数字列かどうか判定
35+
if (!our_idArray.includes(id)) {
36+
// レスポンス
37+
console.log('私たちのアカウントではありません.');
38+
return c.json(
39+
{
40+
message: '私たちのアカウントではありません.',
41+
isOurAccount: false,
42+
},
43+
200
44+
);
45+
}
46+
47+
// レスポンス
48+
console.log('私たちのアカウントです.');
49+
return c.json(
50+
{
51+
message: '私たちのアカウントです.',
52+
isOurAccount: true,
53+
},
54+
200
55+
);
56+
} catch (err) {
57+
console.log('アカウントの判定に失敗しました.' + err);
58+
return c.json(
59+
{
60+
message: 'アカウントの判定に失敗しました.',
61+
statusCode: 500,
62+
error: 'アカウントの判定に失敗しました.',
63+
},
64+
500
65+
);
66+
}
67+
};
68+
69+
export default isOurAccountHandler;

backend/src/lib/gemini.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ const generateTanka = async (originalText: string): Promise<any> => {
106106
1. **五七五七七の形式厳守:** **読み(yomi0~yomi4)をカタカナに変換した上で**、五七五七七の音数になるように調整してください。字余りや字足らずは許されません。
107107
* **特にアルファベットや記号は、カタカナ読みに変換した上で音数を数えてください。** 例:「AI」→「エーアイ」(4音)
108108
* どうしても正確な音数に変換できない場合は、近い音に置き換える、または、前後の句で調整するなど、**短歌全体として五七五七七になるように工夫してください。**
109-
* 長音(ー)、促音(っ)、拗音(ゃゅょ)は1音として扱う
109+
* 長音(ー)、促音(っ)は1音として扱いますが、拗音(ゃゅょ)は1音として扱いません
110110
2. **面白さとユニークさの追求:**
111111
* **原投稿の要素を活かす:** 投稿内容のキーワード、感情、状況などを取り入れつつ、予想外の展開や表現につなげてください。
112112
* **言葉遊び:** 同音異義語、掛詞、比喩などを積極的に活用し、言葉の面白さを引き出してください。

backend/src/lib/getNews.ts

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import type { Context } from 'hono';
2+
import { env } from '../config/env.js';
3+
4+
const printLine = (): void => {
5+
console.log('--------------------------------');
6+
};
7+
8+
const getNews = async () => {
9+
try {
10+
const response = await fetch(
11+
`https://api.currentsapi.services/v1/latest-news?language=ja&apiKey=${env.CURRENTS_API_KEY}`
12+
);
13+
14+
// HTTPエラーが発生した場合
15+
if (!response.ok) {
16+
throw new Error(`HTTPエラー: ${response.status}`);
17+
}
18+
19+
const data = await response.json();
20+
21+
console.log(data);
22+
23+
// ニュースの取得に失敗した場合
24+
if (data.status !== 'ok') {
25+
throw new Error('ニュースの取得に失敗しました。');
26+
}
27+
28+
const newsArray = data.news;
29+
30+
let news;
31+
for (let i = 0; i < newsArray.length; i++) {
32+
const newsTemp = newsArray[i];
33+
if (newsTemp.title !== '' && newsTemp.description !== '' && newsTemp.url !== '') {
34+
news = newsTemp;
35+
break;
36+
}
37+
}
38+
39+
if (!news) {
40+
throw new Error('表示できるニュースがありません。');
41+
}
42+
return {
43+
isSuccess: true,
44+
news: {
45+
title: news.title,
46+
description: news.description,
47+
url: news.url,
48+
},
49+
};
50+
} catch (error: any) {
51+
console.error(error);
52+
return {
53+
isSuccess: false,
54+
message: error.message,
55+
};
56+
}
57+
};
58+
59+
export default getNews;

0 commit comments

Comments
 (0)