-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix/#42/게시물 api 수정 #43
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -1,7 +1,8 @@ | ||||||||
"use strict"; | ||||||||
|
||||||||
const Post = require("../models/Post"); | ||||||||
const { uploadBase64ImageToS3 } = require("../s3/s3Uploader"); | ||||||||
const Post = require("../models/Post"); | ||||||||
|
||||||||
|
||||||||
// 공통 응답 헬퍼 함수 | ||||||||
const sendResponse = (res, statusCode, success, message, data = null) => { | ||||||||
|
@@ -13,20 +14,20 @@ const sendResponse = (res, statusCode, success, message, data = null) => { | |||||||
// 게시물 생성 | ||||||||
const createPost = async (req, res) => { | ||||||||
try { | ||||||||
const { content, post_img } = req.body; | ||||||||
const { content, postImg } = req.body; | ||||||||
const userId = req.session.user.id; | ||||||||
|
||||||||
// S3에 업로드하고, 업로드된 이미지 URL을 반환 | ||||||||
if (!post_img) { | ||||||||
// postImg 없으면 오류 | ||||||||
if (!postImg) { | ||||||||
return sendResponse(res, 400, false, "게시물 이미지는 필수입니다."); | ||||||||
} | ||||||||
|
||||||||
const imageUrl = await uploadBase64ImageToS3(post_img); | ||||||||
|
||||||||
const post = new Post({ content, post_img: imageUrl }, userId); | ||||||||
const postId = await post.createPost(); // createPost 호출 (Poststorage와 연결) | ||||||||
|
||||||||
return sendResponse(res, 201, true, "게시물 생성 성공", { post_id: postId }); | ||||||||
// S3에 이미지 업로드 | ||||||||
const imageUrl = await uploadBase64ImageToS3(postImg); | ||||||||
|
||||||||
const post = new Post({ content, postImg: imageUrl }, userId); | ||||||||
const postId = await post.createPost(); // createPost 호출 (Poststorage와 연결) | ||||||||
return sendResponse(res, 201, true, "게시물 생성 성공", { postId }); | ||||||||
} catch (err) { | ||||||||
console.error("Create post error:", err); | ||||||||
return sendResponse(res, 500, false, "서버 오류가 발생했습니다."); | ||||||||
|
@@ -48,16 +49,24 @@ const getAllPosts = async (req, res) => { | |||||||
const updatePost = async (req, res) => { | ||||||||
try { | ||||||||
const { id } = req.params; | ||||||||
const { content, post_img } = req.body; | ||||||||
const { content, postImg } = req.body; | ||||||||
const userId = req.session.user.id; | ||||||||
|
||||||||
if (!id) return sendResponse(res, 400, false, "post_id는 필수입니다."); | ||||||||
if (!post_img) return sendResponse(res, 400, false, "게시물 이미지는 필수입니다."); | ||||||||
|
||||||||
// 이미지 업로드 | ||||||||
const imageUrl = await uploadBase64ImageToS3(post_img); | ||||||||
const existingPost = await Post.getPost(id); | ||||||||
|
||||||||
const result = await Post.updatePost(id, content, imageUrl, userId); | ||||||||
const existingImg = existingPost.postImg; | ||||||||
|
||||||||
// 새로운 이미지 업로드 | ||||||||
let imageUrl = existingImg; | ||||||||
if (postImg) { | ||||||||
imageUrl = await uploadBase64ImageToS3(postImg); | ||||||||
} | ||||||||
|
||||||||
const post = new Post({ content, postImg: imageUrl }, userId); | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Ryu-02 님한테도 말씀 드렸지만 나중에 확인했는데 명규님이 만들고 계시군요 ㅎㅎ.. |
||||||||
|
||||||||
const result = await post.updatePost(id); | ||||||||
|
||||||||
if (result) { | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
와 같이 early return 방식을 사용하면 불 필요한 조건문 하나를 없앨 수 있어서 |
||||||||
return sendResponse(res, 200, true, "게시물 수정 성공"); | ||||||||
|
@@ -70,6 +79,7 @@ const updatePost = async (req, res) => { | |||||||
} | ||||||||
}; | ||||||||
|
||||||||
|
||||||||
// 게시물 삭제 | ||||||||
const deletePost = async (req, res) => { | ||||||||
try { | ||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,12 @@ | ||
const validatePost = (action) => { | ||
return (req, res, next) => { | ||
const { post_img } = req.body; | ||
const validatePost = (req, res, next) => { | ||
const { postImg } = req.body; | ||
|
||
// 이미지 URL 검증 | ||
if (post_img && !String(post_img).startsWith('http')) { | ||
return res.status(400).json({ error: "유효하지 않은 이미지 URL입니다." }); | ||
// 이미지 인코딩 검증 | ||
if (postImg && !String(postImg).startsWith('data:image/')) { | ||
return res.status(400).json({ error: "유효하지 않은 인코딩 이미지입니다." }); | ||
} | ||
|
||
next(); | ||
}; | ||
}; | ||
|
||
module.exports = { validatePost }; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,39 @@ | ||
const PostStorage = require('../storages/postStorage'); | ||
|
||
class Post { | ||
constructor({ content, post_img }, user_id) { | ||
this.user_id = user_id; | ||
constructor({ content, postImg }, userId, existingImg) { | ||
this.userId = userId; | ||
this.content = content; | ||
this.post_img = post_img; | ||
this.postImg = postImg || existingImg; | ||
} | ||
|
||
static async getAllPosts() { | ||
return PostStorage.getAllPosts(); | ||
} | ||
|
||
|
||
static async getPost(postId) { | ||
return PostStorage.getPost(postId); | ||
} | ||
|
||
// 게시물 생성 | ||
async createPost() { | ||
const postData = { user_id: this.user_id, content: this.content, post_img: this.post_img }; | ||
const postData = { userId: this.userId, content: this.content, postImg: this.postImg }; | ||
return PostStorage.createPost(postData); | ||
} | ||
|
||
// 게시물 수정 | ||
async updatePost(post_id) { | ||
const postData = { post_id, content: this.content, post_img: this.post_img }; | ||
return PostStorage.updatePost(post_id, postData); | ||
async updatePost(postId) { | ||
const postData = { content: this.content, postImg: this.postImg }; | ||
return PostStorage.updatePost(postId, this.userId, postData); | ||
} | ||
|
||
// 게시물 삭제 | ||
static async deletePost(post_id) { | ||
return PostStorage.deletePost(post_id); | ||
static async deletePost(postId, userId) { | ||
return PostStorage.deletePost(postId, userId); | ||
} | ||
|
||
|
||
} | ||
|
||
|
||
module.exports = Post; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
createPost 호출은 const Post = require("../models/Post"); 여기서 하는 중인데
주석 오타가 난 것인가요 ?