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
33c5c6b7
Commit
33c5c6b7
authored
5 months ago
by
조대희
Browse files
Options
Downloads
Plain Diff
[
#7
] Friend 서비스 로직 개발
parents
f8501f60
059ea81f
Branches
Branches containing commit
No related tags found
2 merge requests
!31
Develop
,
!8
[#7] Friend 서비스 로직 개발
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
models/Friend.js
+9
-8
9 additions, 8 deletions
models/Friend.js
services/friendService.js
+168
-0
168 additions, 0 deletions
services/friendService.js
with
177 additions
and
8 deletions
models/Friend.js
+
9
−
8
View file @
33c5c6b7
...
...
@@ -5,19 +5,20 @@ const sequelize = require('../config/sequelize');
const
User
=
require
(
'
./User
'
);
const
Friend
=
sequelize
.
define
(
'
Friend
'
,
{
type
:
{
type
:
DataTypes
.
ENUM
(
'
NORMAL
'
,
'
SPECIAL
'
),
allowNull
:
false
,
},
status
:
{
type
:
DataTypes
.
ENUM
(
'
PENDING
'
,
'
ACCEPTED
'
),
allowNull
:
false
,
defaultValue
:
'
PENDING
'
}
},
{
tableName
:
'
Friends
'
,
timestamps
:
true
,
tableName
:
'
Friends
'
,
timestamps
:
true
,
});
Friend
.
belongsTo
(
User
,
{
foreignKey
:
'
user_id
'
,
as
:
'
user
'
});
Friend
.
belongsTo
(
User
,
{
foreignKey
:
'
friend_id
'
,
as
:
'
friend
'
});
User
.
hasMany
(
Friend
,
{
foreignKey
:
'
user_id
'
,
as
:
'
friends
'
});
User
.
hasMany
(
Friend
,
{
foreignKey
:
'
friend_id
'
,
as
:
'
friend
Of
'
});
User
.
hasMany
(
Friend
,
{
foreignKey
:
'
friend_id
'
,
as
:
'
friend
Requests
'
});
module
.
exports
=
Friend
;
module
.
exports
=
Friend
;
\ No newline at end of file
This diff is collapsed.
Click to expand it.
services/friendService.js
0 → 100644
+
168
−
0
View file @
33c5c6b7
const
{
Op
}
=
require
(
'
sequelize
'
);
const
Friend
=
require
(
'
../models/Friend
'
);
const
User
=
require
(
'
../models/user
'
);
class
friendService
{
/**
* User 존재 여부 유효성 검사
*/
async
validUser
(
userId
)
{
const
user
=
await
User
.
findByPk
(
userId
);
if
(
!
user
)
{
throw
new
Error
(
'
User not found
'
);
}
return
user
;
}
/**
* 친구 요청 보내기
* 나 자신에게 보내기 or 이미 존재하는 친구 -> X
* 이후, PENDING 상태로 변환 -> 수락/거절에 따라 변화
*/
async
sendFriendRequest
(
userId
,
friendId
)
{
await
this
.
validUser
(
userId
);
await
this
.
validUser
(
friendId
);
if
(
userId
===
friendId
)
{
throw
new
Error
(
'
Cannot send friend request to yourself
'
);
}
const
existingFriend
=
await
Friend
.
findOne
({
where
:
{
[
Op
.
or
]:
[
{
user_id
:
userId
,
friend_id
:
friendId
},
{
user_id
:
friendId
,
friend_id
:
userId
}
]
}
});
if
(
existingFriend
)
{
throw
new
Error
(
'
Friend request already exists
'
);
}
return
Friend
.
create
({
user_id
:
userId
,
friend_id
:
friendId
,
status
:
'
PENDING
'
});
}
/**
* 받은 친구 요청 목록 조회
*/
async
getReceivedRequests
(
userId
)
{
return
Friend
.
findAll
({
where
:
{
friend_id
:
userId
,
status
:
'
PENDING
'
},
include
:
[{
model
:
User
,
as
:
'
user
'
,
attributes
:
[
'
id
'
,
'
name
'
,
'
email
'
]
}]
});
}
/**
* 보낸 친구 요청 목록 조회
*/
async
getSentRequests
(
userId
)
{
return
Friend
.
findAll
({
where
:
{
user_id
:
userId
,
status
:
'
PENDING
'
},
include
:
[{
model
:
User
,
as
:
'
friend
'
,
attributes
:
[
'
id
'
,
'
name
'
,
'
email
'
]
}]
});
}
/**
* 친구 요청 수락
*/
async
acceptFriendRequest
(
requestId
,
userId
)
{
const
request
=
await
Friend
.
findOne
({
where
:
{
id
:
requestId
,
friend_id
:
userId
,
status
:
'
PENDING
'
}
});
if
(
!
request
)
{
throw
new
Error
(
'
Friend request not found
'
);
}
return
request
.
update
({
status
:
'
ACCEPTED
'
});
}
/**
* 친구 요청 거절
*/
async
rejectFriendRequest
(
requestId
,
userId
)
{
const
result
=
await
Friend
.
destroy
({
where
:
{
id
:
requestId
,
friend_id
:
userId
,
status
:
'
PENDING
'
}
});
if
(
!
result
)
{
throw
new
Error
(
'
Friend reqeust not found
'
);
}
return
result
;
}
/**
* 친구 목록 조회
*/
async
getFriendList
(
userId
)
{
return
Friend
.
findAll
({
where
:
{
[
Op
.
or
]:
[
{
user_id
:
userId
},
{
friend_id
:
userId
}
],
status
:
'
ACCEPTED
'
},
include
:
[{
model
:
User
,
as
:
'
friend
'
,
attributes
:
[
'
id
'
,
'
name
'
,
'
email
'
]
},
{
model
:
User
,
as
:
'
user
'
,
attributes
:
[
'
id
'
,
'
name
'
,
'
email
'
]
}]
});
}
/**
* 친구 삭제
*/
async
deleteFriend
(
userId
,
friendId
)
{
const
result
=
await
Friend
.
destroy
({
where
:
{
[
Op
.
or
]:
[
{
user_id
:
userId
,
friend_id
:
friendId
},
{
user_id
:
friendId
,
friend_id
:
userId
}
],
status
:
'
ACCEPTED
'
}
});
if
(
!
result
)
{
throw
new
Error
(
'
Friend relationship not found
'
);
}
return
result
;
}
}
module
.
exports
=
new
friendService
();
\ No newline at end of file
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