Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
W
WebBack
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
websystem
WebBack
Commits
dcb0f956
Commit
dcb0f956
authored
3 months ago
by
조대희
Browse files
Options
Downloads
Plain Diff
[
#8
] Friend 컨트롤러, 라우터 로직 개발
parents
33c5c6b7
f220cfc5
No related branches found
No related tags found
2 merge requests
!31
Develop
,
!9
[#8] Friend 컨트롤러, 라우터 로직 개발
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
app.js
+10
-2
10 additions, 2 deletions
app.js
controllers/friendController.js
+184
-0
184 additions, 0 deletions
controllers/friendController.js
routes/friend.js
+50
-0
50 additions, 0 deletions
routes/friend.js
services/friendService.js
+46
-25
46 additions, 25 deletions
services/friendService.js
with
290 additions
and
27 deletions
app.js
+
10
−
2
View file @
dcb0f956
...
...
@@ -7,7 +7,7 @@ const express = require('express');
const
session
=
require
(
'
express-session
'
);
const
passport
=
require
(
'
./passport
'
);
// 변경된 경로
const
flash
=
require
(
'
connect-flash
'
);
const
{
initScheduleCleaner
}
=
require
(
'
./utils/scheduler
'
);
const
{
initScheduleCleaner
}
=
require
(
'
./utils/scheduler
'
);
// 유동 스케줄 자동 삭제 유틸
const
app
=
express
();
...
...
@@ -32,7 +32,10 @@ app.use(passport.session());
// 플래시 메시지 (선택 사항)
app
.
use
(
flash
());
// 라우트 설정
/**
* 라우터 등록
*/
// 로그인 라우터
const
authRoutes
=
require
(
'
./routes/auth
'
);
app
.
use
(
'
/auth
'
,
authRoutes
);
...
...
@@ -40,6 +43,11 @@ app.use('/auth', authRoutes);
const
scheduleRoutes
=
require
(
'
./routes/schedule
'
);
app
.
use
(
'
/api/schedule
'
,
scheduleRoutes
);
// Friend 라우터
const
friendRoutes
=
require
(
'
./routes/friend
'
);
app
.
use
(
'
/api/friend
'
,
friendRoutes
);
initScheduleCleaner
();
...
...
This diff is collapsed.
Click to expand it.
controllers/friendController.js
+
184
−
0
View file @
dcb0f956
const
FriendService
=
require
(
'
../services/friendService
'
);
class
friendController
{
/**
* 친구 요청 보내기
* POST /api/friend/request/:friendId
*/
async
sendRequest
(
req
,
res
)
{
try
{
const
userId
=
req
.
user
.
id
;
const
{
friendId
}
=
req
.
params
;
const
request
=
await
FriendService
.
sendFriendRequest
(
userId
,
friendId
);
return
res
.
status
(
201
).
json
({
success
:
true
,
data
:
request
});
}
catch
(
error
)
{
return
res
.
status
(
400
).
json
({
success
:
false
,
error
:
{
message
:
error
.
message
,
code
:
'
FRIEND_REQUEST_ERROR
'
}
});
}
}
/**
* 받은 친구 요청 목록 조회
* GET /api/friend/requests/received
*/
async
getReceivedRequests
(
req
,
res
)
{
try
{
const
userId
=
req
.
user
.
id
;
const
requests
=
await
FriendService
.
getReceivedRequests
(
userId
);
return
res
.
status
(
200
).
json
({
success
:
true
,
data
:
requests
});
}
catch
(
error
)
{
return
res
.
status
(
500
).
json
({
success
:
false
,
error
:
{
message
:
error
.
message
,
code
:
'
FETCH_ERROR
'
}
});
}
}
/**
* 보낸 친구 요청 목록 조회
* GET /api/friend/requests/sent
*/
async
getSentRequests
(
req
,
res
)
{
try
{
const
userId
=
req
.
user
.
id
;
const
requests
=
await
FriendService
.
getSentRequests
(
userId
);
return
res
.
status
(
200
).
json
({
success
:
true
,
data
:
requests
});
}
catch
(
error
)
{
return
res
.
status
(
500
).
json
({
success
:
false
,
error
:
{
message
:
error
.
message
,
code
:
'
FETCH_ERROR
'
}
});
}
}
/**
* 친구 요청 수락
* POST /api/friend/request/:friendId/accept
*/
async
acceptRequest
(
req
,
res
)
{
try
{
const
userId
=
req
.
user
.
id
;
const
{
friendId
}
=
req
.
params
;
const
result
=
await
FriendService
.
acceptFriendRequest
(
userId
,
friendId
);
return
res
.
status
(
200
).
json
({
success
:
true
,
data
:
result
});
}
catch
(
error
)
{
return
res
.
status
(
400
).
json
({
success
:
false
,
error
:
{
message
:
error
.
message
,
code
:
'
REQUEST_ACCEPT_ERROR
'
}
});
}
}
/**
* 친구 요청 거절
* POST /api/friend/request/:friendId/reject
*/
async
rejectRequest
(
req
,
res
)
{
try
{
const
userId
=
req
.
user
.
id
;
const
{
friendId
}
=
req
.
params
;
const
result
=
await
FriendService
.
rejectFriendRequest
(
userId
,
friendId
);
return
res
.
status
(
200
).
json
({
success
:
true
,
data
:
result
});
}
catch
(
error
)
{
return
res
.
status
(
400
).
json
({
success
:
false
,
error
:
{
message
:
error
.
message
,
code
:
'
REQUEST_REJECT_ERROR
'
}
});
}
}
/**
* 친구 목록 조회
* GET /api/friend/all
*/
async
getFriendList
(
req
,
res
)
{
try
{
const
userId
=
req
.
user
.
id
;
const
friends
=
await
FriendService
.
getFriendList
(
userId
);
return
res
.
status
(
200
).
json
({
success
:
true
,
data
:
friends
});
}
catch
(
error
)
{
return
res
.
status
(
500
).
json
({
success
:
false
,
error
:
{
message
:
error
.
message
,
code
:
'
FETCH_ERROR
'
}
});
}
}
/**
* 친구 삭제
* DELETE /api/friend/:friendId
*/
async
deleteFriend
(
req
,
res
)
{
try
{
const
userId
=
req
.
user
.
id
;
const
{
friendId
}
=
req
.
params
;
const
result
=
await
FriendService
.
deleteFriend
(
userId
,
friendId
);
return
res
.
status
(
200
).
json
({
success
:
true
,
data
:
{
message
:
'
Friend deleted successfully
'
,
result
:
result
}
});
}
catch
(
error
)
{
return
res
.
status
(
400
).
json
({
success
:
false
,
error
:
{
message
:
error
.
message
,
code
:
'
FRIEND_DELETE_ERROR
'
}
});
}
}
}
module
.
exports
=
new
friendController
();
\ No newline at end of file
This diff is collapsed.
Click to expand it.
routes/friend.js
+
50
−
0
View file @
dcb0f956
const
express
=
require
(
'
express
'
);
const
router
=
express
.
Router
();
const
{
isLoggedIn
}
=
require
(
'
../middlewares/auth
'
);
const
FriendController
=
require
(
'
../controllers/friendController
'
);
router
.
use
(
isLoggedIn
);
/**
* 친구 요청 보내기
* POST /api/friend/request/:friendId
*/
router
.
post
(
'
/request/:friendId
'
,
FriendController
.
sendRequest
);
/**
* 받은 친구 요청 목록 조회
* GET /api/friend/requests/received
*/
router
.
get
(
'
/requests/received
'
,
FriendController
.
getReceivedRequests
);
/**
* 보낸 친구 요청 목록 조회
* GET /api/friend/requests/sent
*/
router
.
get
(
'
/requests/sent
'
,
FriendController
.
getSentRequests
);
/**
* 친구 요청 수락
* POST /api/friend/request/:friendId/accept
*/
router
.
post
(
'
/request/:friendId/accept
'
,
FriendController
.
acceptRequest
);
/**
* 친구 요청 거절
* POST /api/friend/request/:friendId/reject
*/
router
.
post
(
'
/request/:friendId/reject
'
,
FriendController
.
rejectRequest
);
/**
* 친구 목록 조회
* GET /api/friend/all
*/
router
.
get
(
'
/all
'
,
FriendController
.
getFriendList
);
/**
* 친구 삭제
* DELETE /api/friend/:friendId
*/
router
.
delete
(
'
/:friendId
'
,
FriendController
.
deleteFriend
);
module
.
exports
=
router
;
\ No newline at end of file
This diff is collapsed.
Click to expand it.
services/friendService.js
+
46
−
25
View file @
dcb0f956
const
{
Op
}
=
require
(
'
sequelize
'
);
const
Friend
=
require
(
'
../models/Friend
'
);
const
User
=
require
(
'
../models/
u
ser
'
);
const
User
=
require
(
'
../models/
U
ser
'
);
class
friendService
{
...
...
@@ -75,7 +75,7 @@ class friendService {
},
include
:
[{
model
:
User
,
as
:
'
friend
'
,
as
:
'
user
'
,
attributes
:
[
'
id
'
,
'
name
'
,
'
email
'
]
}]
});
...
...
@@ -84,38 +84,38 @@ class friendService {
/**
* 친구 요청 수락
*/
async
acceptFriendRequest
(
requestId
,
user
Id
)
{
async
acceptFriendRequest
(
userId
,
friend
Id
)
{
const
request
=
await
Friend
.
findOne
({
where
:
{
id
:
request
Id
,
user_id
:
friend
Id
,
friend_id
:
userId
,
status
:
'
PENDING
'
}
});
if
(
!
request
)
{
throw
new
Error
(
'
Friend request not found
'
);
}
return
request
.
update
({
status
:
'
ACCEPTED
'
});
return
request
.
update
({
status
:
'
ACCEPTED
'
});
}
/**
* 친구 요청 거절
*/
async
rejectFriendRequest
(
requestId
,
user
Id
)
{
async
rejectFriendRequest
(
userId
,
friend
Id
)
{
const
result
=
await
Friend
.
destroy
({
where
:
{
id
:
request
Id
,
friend_id
:
userId
,
user_id
:
friend
Id
,
friend_id
:
userId
,
status
:
'
PENDING
'
}
});
if
(
!
result
)
{
throw
new
Error
(
'
Friend req
e
ust not found
'
);
throw
new
Error
(
'
Friend requ
e
st 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
}
{
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
'
]
}]
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
'
};
});
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment