Skip to content

Commit

Permalink
Merge pull request #20 from mingoo36/master
Browse files Browse the repository at this point in the history
닉네임 중복검사, 작성자만 버튼 보이게 수정
  • Loading branch information
mingoo36 authored Dec 16, 2024
2 parents fe6b0d9 + c9e67ee commit d50fc00
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 18 deletions.
2 changes: 1 addition & 1 deletion config/db2.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const pool = mysql.createPool({
user: process.env.DB_USER,
password: process.env.DB_PASS,
database: process.env.DB_NAME,
//port: process.env.DB_PORT || 3306, // 포트를 환경 변수로 지정하거나 기본값 3306 사용
port: process.env.DB_PORT || 3306, // 포트를 환경 변수로 지정하거나 기본값 3306 사용
waitForConnections: true,
connectionLimit: 10,
queueLimit: 0
Expand Down
22 changes: 11 additions & 11 deletions controllers/commentController.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,23 +42,22 @@ exports.createComment = async (req, res) => {
}
};



exports.getComments = async (req, res) => {
const postId = req.params.postId;

try {
// 댓글 목록을 오래된 순으로 정렬하여 반환
const [comments] = await pool.execute(
`SELECT
c.comment_id,
c.content,
c.created_at,
u.username AS author,
u.image AS author_image
FROM comment c
JOIN user u ON c.user_id = u.user_id
WHERE c.post_id = ?
`SELECT
c.comment_id,
c.content,
c.created_at,
c.user_id AS author_id, -- 댓글 작성자 ID 추가
u.username AS author,
u.image AS author_image
FROM comment c
JOIN user u ON c.user_id = u.user_id
WHERE c.post_id = ?
ORDER BY c.created_at ASC`,
[postId]
);
Expand All @@ -72,6 +71,7 @@ exports.getComments = async (req, res) => {




// 댓글 수정 함수
exports.updateComment = async (req, res) => {
const commentId = req.params.commentId; // URL 파라미터에서 commentId 가져오기
Expand Down
7 changes: 4 additions & 3 deletions controllers/postController.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ exports.getPosts = async (req, res) => {
post.image,
post.created_at,
post.views, -- 조회수 추가
user.user_id AS author_id, -- 작성자 ID 추가
user.username AS author,
user.image AS author_image,
(SELECT COUNT(*) FROM comment WHERE comment.post_id = post.post_id) AS comment_count,
Expand All @@ -57,8 +58,6 @@ exports.getPosts = async (req, res) => {
ORDER BY post.created_at DESC`
);



res.status(200).json(posts);
} catch (error) {
console.error('게시글 목록 가져오기 오류:', error);
Expand All @@ -70,6 +69,7 @@ exports.getPosts = async (req, res) => {




// 특정 게시글 가져오기
exports.getPostById = async (req, res) => {
const { postId } = req.params;
Expand All @@ -82,7 +82,8 @@ exports.getPostById = async (req, res) => {
post.content,
post.image,
post.created_at,
post.updated_at,
post.updated_at,
user.user_id AS author_id,
user.username AS author,
user.image AS author_image
FROM post
Expand Down
34 changes: 32 additions & 2 deletions controllers/userController.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,19 @@ exports.registerUser = async (req, res) => {
}

try {
const [existingUser] = await pool.execute('SELECT * FROM user WHERE email = ?', [email]);
if (existingUser.length > 0) {
// 이메일 중복 검사
const [existingEmail] = await pool.execute('SELECT * FROM user WHERE email = ?', [email]);
if (existingEmail.length > 0) {
return res.status(400).json({ message: '이미 등록된 이메일입니다.' });
}

// username 중복 검사
const [existingUsername] = await pool.execute('SELECT * FROM user WHERE username = ?', [username]);
if (existingUsername.length > 0) {
return res.status(400).json({ message: '이미 사용 중인 닉네임입니다.' });
}

// 비밀번호 해싱 및 사용자 등록
const hashedPassword = await bcrypt.hash(password, 10);
const [result] = await pool.execute(
'INSERT INTO user (email, password, username, image) VALUES (?, ?, ?, ?)',
Expand All @@ -29,6 +37,28 @@ exports.registerUser = async (req, res) => {
}
};

// 닉네임 중복 검사 API
exports.checkUsername = async (req, res) => {
const { username } = req.query; // GET 요청으로 전달받음

if (!username) {
return res.status(400).json({ message: '사용자 이름을 입력해주세요.' });
}

try {
const [existingUsername] = await pool.execute('SELECT * FROM user WHERE username = ?', [username]);
if (existingUsername.length > 0) {
return res.status(409).json({ message: '이미 사용 중인 사용자 이름입니다.' }); // Conflict
}
res.status(200).json({ message: '사용 가능한 사용자 이름입니다.' });
} catch (error) {
console.error(error);
res.status(500).json({ message: '서버 오류가 발생했습니다.' });
}
};



// 로그인 API
exports.loginUser = async (req, res) => {
const { email, password } = req.body;
Expand Down
Binary file added default_images/default_post.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added default_images/default_profile.webp
Binary file not shown.
5 changes: 4 additions & 1 deletion routes/user.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
const express = require('express');
const { registerUser,updateSession ,loginUser, updateUserProfile, deleteUser, checkSession, logoutUser, updatePassword} = require('../controllers/userController');
const { registerUser,checkUsername,updateSession ,loginUser, updateUserProfile, deleteUser, checkSession, logoutUser, updatePassword} = require('../controllers/userController');
const { profileUpload, verifySession } = require('../middlewares/middleware');
const router = express.Router();

// 회원가입 API
router.post('/api/register', profileUpload.single('profilePic'), registerUser);

// 닉네임 중복 검사 API
router.get('/api/check-username', checkUsername);

// 로그인 API
router.post('/api/login', loginUser);

Expand Down

0 comments on commit d50fc00

Please sign in to comment.