From 76b1ddf0faf77f0c26c8b219c60f5f961e4c1794 Mon Sep 17 00:00:00 2001 From: ri7116 Date: Thu, 20 Feb 2025 20:55:32 +0900 Subject: [PATCH] =?UTF-8?q?=ED=9A=8C=EC=9B=90=20=EC=A0=95=EB=B3=B4=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/mypage.controller.js | 203 +++++++++++++------------ src/repositories/mypage.repository.js | 210 +++++++++++++------------- src/services/mypage.service.js | 160 ++++++++++---------- test.http | 13 ++ 4 files changed, 309 insertions(+), 277 deletions(-) diff --git a/src/controllers/mypage.controller.js b/src/controllers/mypage.controller.js index 513c727..26bc24b 100644 --- a/src/controllers/mypage.controller.js +++ b/src/controllers/mypage.controller.js @@ -1,130 +1,147 @@ -const MyPageService = require('../services/mypage.service'); -const MyPageRepository = require('../repositories/mypage.repository'); +const MyPageService = require("../services/mypage.service"); +const MyPageRepository = require("../repositories/mypage.repository"); const imageUploader = require("../middlewares/imageUploader.js"); const profileImageUploader = imageUploader("profile-image"); // 유저 정보 조회 const getMyPage = async (req, res) => { - try{ - const userId = req.userId; - const user = await MyPageService.findUserById(userId); - res.status(200).json({ - resultType: 'success', - message: '유저 정보 조회 성공', - data: user - }); - } catch(error) { - res.status(500).json({message: error.message}); - } + try { + const userId = req.userId; + const user = await MyPageService.findUserById(userId); + res.status(200).json({ + resultType: "success", + message: "유저 정보 조회 성공", + data: user, + }); + } catch (error) { + res.status(500).json({ message: error.message }); + } }; // 유저 정보 수정 const updateMyPage = async (req, res) => { - try{ - const userId = req.userId; - const { nickname, name, birth, phonenum, email } = req.body; - const user = await MyPageService.updateUserById(userId, { nickname, name, birth, phonenum, email }); - res.status(200).json({ - resultType: 'success', - message: '유저 정보 수정 성공', - data: user - }); - } catch(error) { - res.status(500).json({message: error.message}); + try { + const userId = req.userId; + let { nickname, name, birth, phonenum, email } = req.body; + + // birth 값이 존재하고 문자열 형식이면 변환 + if (birth && typeof birth === "string") { + birth = new Date(`${birth}T00:00:00.000Z`); // 날짜 변환 + if (isNaN(birth.getTime())) { + throw new Error("유효하지 않은 날짜 형식입니다."); + } } + + const user = await MyPageService.updateUserById(userId, { + nickname, + name, + birth, + phonenum, + email, + }); + res.status(200).json({ + resultType: "success", + message: "유저 정보 수정 성공", + data: user, + }); + } catch (error) { + res.status(500).json({ message: error.message }); + } }; // 보관 글 목록 조회 const getStoragedPost = async (req, res) => { - try{ - const userId = req.userId; - const posts = await MyPageService.findStoragedPost(userId); - res.status(200).json({ - resultType: 'success', - message: '보관 글 목록 조회 성공', - data: posts - }); - } catch(error) { - res.status(500).json({message: error.message}); - } + try { + const userId = req.userId; + const posts = await MyPageService.findStoragedPost(userId); + res.status(200).json({ + resultType: "success", + message: "보관 글 목록 조회 성공", + data: posts, + }); + } catch (error) { + res.status(500).json({ message: error.message }); + } }; // 보관 글 상태 수정(보관->전체공개) const updateStoragePost = async (req, res) => { - try{ - const postId = req.params.postId; - const post = await MyPageService.updateStoragePost(postId); - res.status(200).json({ - resultType: 'success', - message: '보관 글 상태 수정 성공', - data: post - }); - } catch(error) { - res.status(500).json({message: error.message}); - } + try { + const postId = req.params.postId; + const post = await MyPageService.updateStoragePost(postId); + res.status(200).json({ + resultType: "success", + message: "보관 글 상태 수정 성공", + data: post, + }); + } catch (error) { + res.status(500).json({ message: error.message }); + } }; -// 프로필 사진 등록/수정 +// 프로필 사진 등록/수정 const uploadUserImage = async (req, res) => { - const userId = req.userId; + const userId = req.userId; - profileImageUploader.single("images")(req, res, (err) => { - if (err) { - return res.status(500).json({ message: "파일 업로드 중 오류 발생", error: err.message }); - } + profileImageUploader.single("images")(req, res, (err) => { + if (err) { + return res + .status(500) + .json({ message: "파일 업로드 중 오류 발생", error: err.message }); + } - if (!req.file) { - return res.status(400).json({ message: "파일이 없어요.." }); - } + if (!req.file) { + return res.status(400).json({ message: "파일이 없어요.." }); + } + + const fileUrl = req.file.location; + MyPageService.registerProfileImage(userId, fileUrl); - const fileUrl = req.file.location; - MyPageService.registerProfileImage(userId, fileUrl); - - return res.status(200).json({ message: "파일 업로드 성공", fileUrl }); - }) + return res.status(200).json({ message: "파일 업로드 성공", fileUrl }); + }); }; // 프로필 사진 삭제 const deleteUserImage = async (req, res) => { - try { - const userId = req.userId; - const result = await MyPageService.deleteProfileImage(userId); - - if (result.message === "등록된 이미지가 없습니다.") { - return res.status(400).json(result); - } + try { + const userId = req.userId; + const result = await MyPageService.deleteProfileImage(userId); - return res.status(200).json(result); - } catch (error) { - res.status(500).json({ message: "서버 오류", error: error.message }); + if (result.message === "등록된 이미지가 없습니다.") { + return res.status(400).json(result); } + + return res.status(200).json(result); + } catch (error) { + res.status(500).json({ message: "서버 오류", error: error.message }); + } }; // 프로필 사진 조회 const getUserImage = async (req, res) => { - try { - const userId = req.userId; - const image = await MyPageRepository.findPostImages(userId); - console.log(`image: `, image); - - if (!image) { - return res.status(400).json({ message: "등록된 배경사진이 없습니다." }); - } - - return res.status(200).json({ - data: image.file_name - }); - } catch (error) { - res.status(500).json({ message: "서버 오류", error: error.message }); + try { + const userId = req.userId; + const image = await MyPageRepository.findPostImages(userId); + console.log(`image: `, image); + + if (!image) { + return res.status(400).json({ message: "등록된 배경사진이 없습니다." }); } + + return res.status(200).json({ + data: image.file_name, + }); + } catch (error) { + res.status(500).json({ message: "서버 오류", error: error.message }); + } }; -module.exports = { - getMyPage, - updateMyPage, - getStoragedPost, - updateStoragePost, - uploadUserImage, - deleteUserImage, - getUserImage, -}; \ No newline at end of file +module.exports = { + getMyPage, + updateMyPage, + getStoragedPost, + updateStoragePost, + uploadUserImage, + deleteUserImage, + getUserImage, +}; diff --git a/src/repositories/mypage.repository.js b/src/repositories/mypage.repository.js index bd4839d..01630bf 100644 --- a/src/repositories/mypage.repository.js +++ b/src/repositories/mypage.repository.js @@ -1,114 +1,118 @@ -const {PrismaClient} = require('@prisma/client'); +const { PrismaClient } = require("@prisma/client"); const prisma = new PrismaClient(); class MyPageRepository { - // 유저 정보 조회 - async findUserById(userId){ - return await prisma.user.findFirst({ - where: {user_id: userId}, - select: { - user_id: true, - nickname: true, - password: true, - name: true, - birth: true, - phonenum: true, - email: true, - } - }) - }; + // 유저 정보 조회 + async findUserById(userId) { + return await prisma.user.findFirst({ + where: { user_id: userId }, + select: { + user_id: true, + nickname: true, + password: true, + name: true, + birth: true, + phonenum: true, + email: true, + }, + }); + } - // 유저 정보 수정 - async updateUserById(userId, userData){ - return await prisma.user.update({ - where: {user_id: userId}, - data: userData, - select: { - user_id: true, - nickname: true, - name: true, - birth: true, - phonenum: true, - email: true, - } - }) - }; + // 유저 정보 수정 + async updateUserById(userId, userData) { + if (userData.birth && typeof userData.birth === "string") { + userData.birth = new Date(`${userData.birth}T00:00:00.000Z`); // 변환 + } - // 보관 글 목록 조회 - async findStoragedPost(userId){ - return await prisma.post.findMany({ - where: {user_id: userId, storage: 2}, - select: { - post_id: true, - title: true, - created_at: true, - updated_at: true, - } - }) - }; + return await prisma.user.update({ + where: { user_id: userId }, + data: userData, + select: { + user_id: true, + nickname: true, + name: true, + birth: true, + phonenum: true, + email: true, + }, + }); + } - // 보관 글 상태 수정(보관->전체공개) - async updateStoragePost(postId){ - return await prisma.post.update({ - where: {post_id: postId}, - data: {storage: 0}, - select: { - post_id: true, - updated_at: true, - } - }) - } + // 보관 글 목록 조회 + async findStoragedPost(userId) { + return await prisma.post.findMany({ + where: { user_id: userId, storage: 2 }, + select: { + post_id: true, + title: true, + created_at: true, + updated_at: true, + }, + }); + } + + // 보관 글 상태 수정(보관->전체공개) + async updateStoragePost(postId) { + return await prisma.post.update({ + where: { post_id: postId }, + data: { storage: 0 }, + select: { + post_id: true, + updated_at: true, + }, + }); + } - // 프로필 이미지 등록/수정 - 원래 이미지가 있는지 확인 - async checkProfileImage(userId) { - return prisma.user_image.findUnique({ - where: { - user_id: userId - } - }) - }; + // 프로필 이미지 등록/수정 - 원래 이미지가 있는지 확인 + async checkProfileImage(userId) { + return prisma.user_image.findUnique({ + where: { + user_id: userId, + }, + }); + } - // 프로필 이미지 등록/수정 - db에 데이터 생성 - async createImage(userId, fileUrl) { - return prisma.user_image.create({ - data: { - user_id: userId, - file_name: fileUrl, - } - }); - }; + // 프로필 이미지 등록/수정 - db에 데이터 생성 + async createImage(userId, fileUrl) { + return prisma.user_image.create({ + data: { + user_id: userId, + file_name: fileUrl, + }, + }); + } - // 프로필 이미지 등록/수정 - db에 데이터 수정 - async updateImage(userId, fileUrl) { - return prisma.user_image.update({ - where: { - user_id: userId - }, - data: { - file_name: fileUrl - } - }); - }; + // 프로필 이미지 등록/수정 - db에 데이터 수정 + async updateImage(userId, fileUrl) { + return prisma.user_image.update({ + where: { + user_id: userId, + }, + data: { + file_name: fileUrl, + }, + }); + } - // 프로필 이미지 삭제 - 이미지 가져오기 - async findPostImages(userId) { - return prisma.user_image.findUnique({ - where: { - user_id: userId - }, - select: { - file_name: true, - } - }); - }; + // 프로필 이미지 삭제 - 이미지 가져오기 + async findPostImages(userId) { + return prisma.user_image.findUnique({ + where: { + user_id: userId, + }, + select: { + file_name: true, + }, + }); + } - // 프로필 이미지 삭제 - 이미지 삭제 - async deleteImageDB(userId) { - return prisma.user_image.delete({ - where: { - user_id: userId, - } - }); - }; -}; -module.exports = new MyPageRepository(); \ No newline at end of file + // 프로필 이미지 삭제 - 이미지 삭제 + async deleteImageDB(userId) { + return prisma.user_image.delete({ + where: { + user_id: userId, + }, + }); + } +} +module.exports = new MyPageRepository(); diff --git a/src/services/mypage.service.js b/src/services/mypage.service.js index e3c8eb5..931d4a2 100644 --- a/src/services/mypage.service.js +++ b/src/services/mypage.service.js @@ -1,84 +1,82 @@ -const { request } = require('express'); -const MyPageRepository = require('../repositories/mypage.repository'); +const { request } = require("express"); +const MyPageRepository = require("../repositories/mypage.repository"); const { deleteImage } = require("../middlewares/deleteImage.js"); class MyPageService { - // 유저 정보 조회 - async findUserById(userId){ - const user = await MyPageRepository.findUserById(userId); - if(!user){ - throw new Error('유저 정보가 없습니다.'); - } - - return user; - }; - - // 유저 정보 수정 - async updateUserById(userId, userData){ - const user = await MyPageRepository.updateUserById(userId, userData); - if(!user){ - throw new Error('유저 정보 수정에 실패했습니다.'); - } - - return user; - }; - - // 보관 글 목록 조회 - async findStoragedPost(userId){ - const posts = await MyPageRepository.findStoragedPost(userId); - if(!posts || posts.length === 0){ - throw new Error('보관 글 목록 조회에 실패했습니다.'); - } - - return posts; - }; - - // 보관 글 상태 수정(보관->전체공개) - async updateStoragePost(postId){ - const post = await MyPageRepository.updateStoragePost(postId); - if(!post){ - throw new Error('보관 글 상태 수정에 실패했습니다.'); - } - - return post; - }; - - // 프로필 이미지 등록/수정 - async registerProfileImage(userId, fileUrl) { - // 1. 원래 프로필 이미지가 있는지 확인 - const checkImage = await MyPageRepository.checkProfileImage(userId); - - // 2. 이미지가 없다면 생성 / 있다면 데이터 바꾸기 - let image; - - if (!checkImage) { - image = await MyPageRepository.createImage(userId, fileUrl); - } - else { - image = await MyPageRepository.updateImage(userId, fileUrl); - - await deleteImage("profile-image", [checkImage.file_name]); - } - - return image.file_name; - }; - - // 프로필 이미지 삭제 - async deleteProfileImage(userId) { - // 이미지 조회 - const image = await MyPageRepository.findPostImages(userId); - - if (!image) { - return { message: "등록된 이미지가 없습니다." }; - } - - // S3에서 이미지 삭제 - await deleteImage("profile-image", [image.file_name]); - - // DB에서 이미지 삭제 - await MyPageRepository.deleteImageDB(userId); - - return { message: "이미지 삭제 완료" }; - }; -}; -module.exports = new MyPageService(); \ No newline at end of file + // 유저 정보 조회 + async findUserById(userId) { + const user = await MyPageRepository.findUserById(userId); + if (!user) { + throw new Error("유저 정보가 없습니다."); + } + + return user; + } + + // 유저 정보 수정 + async updateUserById(userId, userData) { + if (userData.birth && typeof userData.birth === "string") { + userData.birth = new Date(`${userData.birth}T00:00:00.000Z`); // ISO 8601 변환 + } + + return await MyPageRepository.updateUserById(userId, userData); + } + + // 보관 글 목록 조회 + async findStoragedPost(userId) { + const posts = await MyPageRepository.findStoragedPost(userId); + if (!posts || posts.length === 0) { + throw new Error("보관 글 목록 조회에 실패했습니다."); + } + + return posts; + } + + // 보관 글 상태 수정(보관->전체공개) + async updateStoragePost(postId) { + const post = await MyPageRepository.updateStoragePost(postId); + if (!post) { + throw new Error("보관 글 상태 수정에 실패했습니다."); + } + + return post; + } + + // 프로필 이미지 등록/수정 + async registerProfileImage(userId, fileUrl) { + // 1. 원래 프로필 이미지가 있는지 확인 + const checkImage = await MyPageRepository.checkProfileImage(userId); + + // 2. 이미지가 없다면 생성 / 있다면 데이터 바꾸기 + let image; + + if (!checkImage) { + image = await MyPageRepository.createImage(userId, fileUrl); + } else { + image = await MyPageRepository.updateImage(userId, fileUrl); + + await deleteImage("profile-image", [checkImage.file_name]); + } + + return image.file_name; + } + + // 프로필 이미지 삭제 + async deleteProfileImage(userId) { + // 이미지 조회 + const image = await MyPageRepository.findPostImages(userId); + + if (!image) { + return { message: "등록된 이미지가 없습니다." }; + } + + // S3에서 이미지 삭제 + await deleteImage("profile-image", [image.file_name]); + + // DB에서 이미지 삭제 + await MyPageRepository.deleteImageDB(userId); + + return { message: "이미지 삭제 완료" }; + } +} +module.exports = new MyPageService(); diff --git a/test.http b/test.http index b7274bf..08a54e3 100644 --- a/test.http +++ b/test.http @@ -12,6 +12,19 @@ Content-Type: application/json; charset=UTF-8 "email": "rrrr@naver.com" } +### 마이페이지 정보 수정 +PATCH http://localhost:4000/mypage +Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjEyMzQiLCJpYXQiOjE3NDAwNTEyODAsImV4cCI6MTc0MDA4NzI4MH0.pjYNBM9lhsUjN_JVa_cvrq9r5QJL1BjFC1iZvBhn060 +Content-Type: application/json + +{ + "nickname" : "qqqq", + "name" : "qqqq", + "birth" : "1980-02-15", + "phonenum" : "qqqq", + "email" : "qqqq" +} + ###로그인15.165.117.176 POST http://15.165.117.176:4000/login Content-Type: application/json; charset=UTF-8