Skip to content
Snippets Groups Projects
Commit f220cfc5 authored by 조대희's avatar 조대희
Browse files

fix: reqeustId -> friendId로 변경 및 파라미터 순서 변경 (#8)

parent d223a47a
No related branches found
No related tags found
2 merge requests!31Develop,!9[#8] Friend 컨트롤러, 라우터 로직 개발
This commit is part of merge request !9. Comments created here will be created in the context of that merge request.
......@@ -44,8 +44,8 @@ const scheduleRoutes = require('./routes/schedule');
app.use('/api/schedule', scheduleRoutes);
// Friend 라우터
const friendRoutes = require('./routes/friendRoutes');
app.use('/api/friends', friendRoutes);
const friendRoutes = require('./routes/friend');
app.use('/api/friend', friendRoutes);
......
......@@ -3,12 +3,12 @@ const FriendService = require('../services/friendService');
class friendController {
/**
* 친구 요청 보내기
* POST /api/friend/request
* POST /api/friend/request/:friendId
*/
async sendRequest(req, res) {
try {
const userId = req.user.id;
const { friendId } = req.body;
const { friendId } = req.params;
const request = await FriendService.sendFriendRequest(userId, friendId);
return res.status(201).json({
......@@ -76,14 +76,14 @@ class friendController {
/**
* 친구 요청 수락
* POST /api/friend/request/:requestId/accept
* POST /api/friend/request/:friendId/accept
*/
async acceptRequest(req, res) {
try {
const userId = req.user.id;
const { requestId } = req.params;
const { friendId } = req.params;
const result = await FriendService.acceptFriendRequest(requestId, userId);
const result = await FriendService.acceptFriendRequest(userId, friendId);
return res.status(200).json({
success: true,
......@@ -102,14 +102,14 @@ class friendController {
/**
* 친구 요청 거절
* POST /api/friend/request/:requestId/reject
* POST /api/friend/request/:friendId/reject
*/
async rejectRequest(req, res) {
try {
const userId = req.user.id;
const { requesId } = req.params;
const { friendId } = req.params;
const result = await FriendService.rejectFriendRequest(requesId, userId);
const result = await FriendService.rejectFriendRequest(userId, friendId);
return res.status(200).json({
success: true,
......
......@@ -7,9 +7,9 @@ router.use(isLoggedIn);
/**
* 친구 요청 보내기
* POST /api/friend/request
* POST /api/friend/request/:friendId
*/
router.post('/request', FriendController.sendRequest);
router.post('/request/:friendId', FriendController.sendRequest);
/**
* 받은 친구 요청 목록 조회
......@@ -25,15 +25,15 @@ router.get('/requests/sent', FriendController.getSentRequests);
/**
* 친구 요청 수락
* POST /api/friend/request/:requestId/accept
* POST /api/friend/request/:friendId/accept
*/
router.post('/request/:requestId/accept', FriendController.acceptRequest);
router.post('/request/:friendId/accept', FriendController.acceptRequest);
/**
* 친구 요청 거절
* POST /api/friend/request/:requestId/reject
* POST /api/friend/request/:friendId/reject
*/
router.post('/request/:requestId/reject', FriendController.rejectRequest);
router.post('/request/:friendId/reject', FriendController.rejectRequest);
/**
* 친구 목록 조회
......
......@@ -75,7 +75,7 @@ class friendService {
},
include: [{
model: User,
as: 'friend',
as: 'user',
attributes: ['id', 'name', 'email']
}]
});
......@@ -84,10 +84,10 @@ class friendService {
/**
* 친구 요청 수락
*/
async acceptFriendRequest(requestId, userId) {
async acceptFriendRequest(userId, friendId) {
const request = await Friend.findOne({
where: {
id: requestId,
user_id: friendId,
friend_id: userId,
status: 'PENDING'
}
......@@ -103,17 +103,17 @@ class friendService {
/**
* 친구 요청 거절
*/
async rejectFriendRequest(requestId, userId) {
async rejectFriendRequest(userId, friendId) {
const result = await Friend.destroy({
where: {
id: requestId,
user_id: friendId,
friend_id: userId,
status: 'PENDING'
}
});
if (!result) {
throw new Error('Friend reqeust not found');
throw new Error('Friend request not found');
}
return result;
......@@ -123,23 +123,44 @@ class friendService {
* 친구 목록 조회
*/
async getFriendList(userId) {
return Friend.findAll({
const friends = await Friend.findAll({
where: {
[Op.or]: [
{ user_id: userId },
{ friend_id: userId }
],
status: 'ACCEPTED'
}
});
const friendIds = friends.map(friend =>
friend.user_id === userId ? friend.friend_id : friend.user_id
);
const friendUsers = await User.findAll({
where: {
id: friendIds
},
include: [{
model: User,
as: 'friend',
attributes: ['id', 'name', 'email']
}, {
model: User,
as: 'user',
attributes: ['id', 'name', 'email']
}]
});
const friendsMap = friendUsers.reduce((map, user) => {
map[user.id] = user;
return map;
}, {});
return friends.map(friend => {
const isSender = friend.user_id === userId;
const friendId = isSender ? friend.friend_id : friend.user_id;
return {
id: friend.id,
status: friend.status,
createdAt: friend.createdAt,
updatedAt: friend.updatedAt,
friendInfo: friendsMap[friendId],
relationshipType: isSender ? 'sent' : 'received'
};
});
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment