|
| 1 | +import { PrismaClient } from "@prisma/client"; |
| 2 | +const prisma = new PrismaClient(); |
| 3 | + |
| 4 | +export const createArticle = async (req, res) => { |
| 5 | + try { |
| 6 | + const { title, content, author, likeCount } = req.body; |
| 7 | + const article = await prisma.article.create({ |
| 8 | + data: { |
| 9 | + title, |
| 10 | + content, |
| 11 | + author: author || "익명", |
| 12 | + likeCount, |
| 13 | + }, |
| 14 | + }); |
| 15 | + res.status(201).json(article); |
| 16 | + } catch (err) { |
| 17 | + console.error("❌ 게시글 등록 실패:", err); |
| 18 | + res.status(500).json({ message: "게시글 등록 실패", error: err.message }); |
| 19 | + } |
| 20 | +}; |
| 21 | + |
| 22 | + |
| 23 | +export const getArticle = async (req, res) => { |
| 24 | + try { |
| 25 | + const article = await prisma.article.findUnique({ |
| 26 | + where: { id: Number(req.params.id) }, |
| 27 | + select: { |
| 28 | + id: true, |
| 29 | + title: true, |
| 30 | + content: true, |
| 31 | + author: true, |
| 32 | + likeCount: true, |
| 33 | + createdAt: true, |
| 34 | + updatedAt: true, |
| 35 | + }, |
| 36 | + }); |
| 37 | + if (!article) return res.status(404).json({ message: "게시글 없음" }); |
| 38 | + res.status(200).json(article); |
| 39 | + } catch (err) { |
| 40 | + console.error("❌ 단건 조회 실패:", err); |
| 41 | + res.status(500).json({ message: "조회 실패", error: err.message }); |
| 42 | + } |
| 43 | +}; |
| 44 | + |
| 45 | + |
| 46 | +export const getArticles = async (req, res) => { |
| 47 | + try { |
| 48 | + const { page = 1, limit = 10, search = "", sort = "recent" } = req.query; |
| 49 | + const skip = (page - 1) * limit; |
| 50 | + |
| 51 | + const orderBy = |
| 52 | + sort === "like" ? { likeCount: "desc" } : { createdAt: "desc" }; |
| 53 | + |
| 54 | + const where = search |
| 55 | + ? { |
| 56 | + OR: [ |
| 57 | + { title: { contains: search, mode: "insensitive" } }, |
| 58 | + { content: { contains: search, mode: "insensitive" } }, |
| 59 | + { author: { contains: search, mode: "insensitive" } }, |
| 60 | + ], |
| 61 | + } |
| 62 | + : {}; |
| 63 | + |
| 64 | + const articles = await prisma.article.findMany({ |
| 65 | + where, |
| 66 | + orderBy, |
| 67 | + skip: Number(skip), |
| 68 | + take: Number(limit), |
| 69 | + select: { |
| 70 | + id: true, |
| 71 | + title: true, |
| 72 | + content: true, |
| 73 | + author: true, |
| 74 | + likeCount: true, |
| 75 | + createdAt: true, |
| 76 | + }, |
| 77 | + }); |
| 78 | + |
| 79 | + res.status(200).json(articles); |
| 80 | + } catch (err) { |
| 81 | + console.error("❌ 목록 조회 실패:", err); |
| 82 | + res.status(500).json({ message: "목록 조회 실패", error: err.message }); |
| 83 | + } |
| 84 | +}; |
| 85 | + |
| 86 | +export const updateArticle = async (req, res) => { |
| 87 | + try { |
| 88 | + const { title, content, author, likeCount } = req.body; |
| 89 | + const updated = await prisma.article.update({ |
| 90 | + where: { id: Number(req.params.id) }, |
| 91 | + data: { title, content, author, likeCount }, |
| 92 | + }); |
| 93 | + res.status(200).json(updated); |
| 94 | + } catch (err) { |
| 95 | + console.error("❌ 수정 실패:", err); |
| 96 | + res.status(500).json({ message: "수정 실패", error: err.message }); |
| 97 | + } |
| 98 | +}; |
| 99 | + |
| 100 | + |
| 101 | +export const deleteArticle = async (req, res) => { |
| 102 | + try { |
| 103 | + await prisma.article.delete({ where: { id: Number(req.params.id) } }); |
| 104 | + res.status(200).json({ message: "게시글 삭제 완료" }); |
| 105 | + } catch (err) { |
| 106 | + console.error("❌ 삭제 실패:", err); |
| 107 | + res.status(500).json({ message: "삭제 실패", error: err.message }); |
| 108 | + } |
| 109 | +}; |
0 commit comments