|
| 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; |
0 commit comments