Skip to content

Commit

Permalink
Merge pull request #175 from YAPP-19th/feature/#168-공유_관련_수정
Browse files Browse the repository at this point in the history
[#168] 공유 관련 수정
  • Loading branch information
Ji-Ha authored Aug 21, 2022
2 parents 9f2beeb + 5129695 commit 61869b7
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 48 deletions.
67 changes: 33 additions & 34 deletions src/main/kotlin/com/yapp/web2/domain/account/entity/Account.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,34 +43,6 @@ class Account(
const val BASIC_IMAGE_URL: String = "https://yapp-bucket-test.s3.ap-northeast-2.amazonaws.com/basicImage.png"
}

constructor(email: String, password: String) : this(email) {
this.password = password
}

constructor(email: String, encryptPassword: String, fcmToken: String, name: String) : this(email) {
this.password = encryptPassword
this.fcmToken = fcmToken
this.name = name
}

constructor(email: String, image: String, nickname: String, socialType: String, fcmToken: String) : this(email) {
this.image = image
this.name = nickname
this.socialType = socialType
this.fcmToken = fcmToken
}

fun addAccountFolder(accountFolder: AccountFolder) {
this.accountFolderList.add(accountFolder)
}

@Transactional
fun isInsideAccountFolder(accountFolder: AccountFolder): Boolean {
accountFolderList.forEach {
if (it.folder.id == accountFolder.folder.id) return true
}
return false
}

@Column(nullable = true)
var password: String? = null
Expand Down Expand Up @@ -99,9 +71,38 @@ class Account(
@Column
var deleted: Boolean = false

@OneToMany(mappedBy = "account", cascade = [CascadeType.ALL])
@OneToMany(mappedBy = "account", cascade = [CascadeType.ALL], orphanRemoval = true)
var accountFolderList: MutableList<AccountFolder> = mutableListOf()

constructor(email: String, password: String) : this(email) {
this.password = password
}

constructor(email: String, encryptPassword: String, fcmToken: String, name: String) : this(email) {
this.password = encryptPassword
this.fcmToken = fcmToken
this.name = name
}

constructor(email: String, image: String, nickname: String, socialType: String, fcmToken: String) : this(email) {
this.image = image
this.name = nickname
this.socialType = socialType
this.fcmToken = fcmToken
}

fun addAccountFolder(accountFolder: AccountFolder) {
this.accountFolderList.add(accountFolder)
}

@Transactional
fun isInsideAccountFolder(accountFolder: AccountFolder): Boolean {
accountFolderList.forEach {
if (it.folder.id == accountFolder.folder.id) return true
}
return false
}

@ApiModel(description = "소셜로그인 DTO")
class AccountProfile(
@ApiModelProperty(value = "이메일", required = true, example = "[email protected]")
Expand Down Expand Up @@ -168,11 +169,9 @@ class Account(
)

fun removeFolderInAccountFolder(folder: Folder) {
for(af in this.accountFolderList)
if (af.folder == folder) {
accountFolderList.remove(af)
return
}
this.accountFolderList.let {
it.removeIf { af -> af.folder == folder }
}
}

fun softDeleteAccount() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ class AccountService(
val folderId = jwtProvider.getIdFromToken(folderToken)
val rootFolder = folderService.findByFolderId(folderId)

if (rootFolder.rootFolderId != null) throw FolderNotRootException()
if (rootFolder.rootFolderId != folderId) throw FolderNotRootException()

val accountFolder = AccountFolder(account, rootFolder)
accountFolder.changeAuthority(Authority.INVITEE)
Expand All @@ -210,14 +210,18 @@ class AccountService(
rootFolder.folders?.add(accountFolder)
}


@Transactional
fun exitSharedFolder(folderId: Long, token: String) {
val account = jwtProvider.getAccountFromToken(token)
val folder = folderService.findByFolderId(folderId)
var exitFolder = folder.rootFolderId?.let {
folderService.findByFolderId(it)
} ?: folder

exitFolder.folders?.let {
it.removeIf { x -> x.account == account }
}

account.removeFolderInAccountFolder(exitFolder)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,14 @@ class SharedBookmarkService(
private val jwtProvider: JwtProvider
) {

@Transactional(readOnly = true)
fun checkAuthority(account: Account, folderId: Long) {
var folder = folderRepository.findFolderById(folderId) ?: throw FolderNotFoundException()

if (folder.rootFolderId != null) folder =
folderRepository.findFolderById(folder.rootFolderId!!) ?: throw FolderNotFoundException()

// 존재하면? 일단 공유 멤버이니까 return
for (af in account.accountFolderList)
if (af.folder == folder && af.authority > Authority.NONE) return
for (af in folder.folders!!)
if (af.account == account && af.authority > Authority.NONE) return

throw NoPermissionException()
}
Expand Down Expand Up @@ -132,6 +130,8 @@ class SharedBookmarkService(
val folderId = bookmark.folderId ?: throw RuntimeException("폴더에 속해있지 않습니다!")
val remind = Remind(account)

if(bookmark.isRemindExist(remind)) return

checkAuthority(account, folderId)

bookmark.remindOn(remind)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ class AccountFolder(
var folder: Folder
) {

fun changeAuthority(authority: Authority) {
this.authority = authority
}

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
var id: Long? = null

@Enumerated(EnumType.STRING)
var authority: Authority = Authority.NONE

fun changeAuthority(authority: Authority) {
this.authority = authority
}
}
3 changes: 2 additions & 1 deletion src/main/kotlin/com/yapp/web2/domain/folder/entity/Folder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ class Folder(
@JsonManagedReference
var children: MutableList<Folder>? = mutableListOf()

@OneToMany(mappedBy = "folder", cascade = [CascadeType.ALL])
// TODO: 2022/08/14 cascadeType.REMOVE도 포함되어 있기 때문에, delete문이 실행된다고함. 이를 빼고 진행할 수 있도록 그리고 orphanRemoval만 사용했을 때 좋은 이유를 확실히 알것
@OneToMany(mappedBy = "folder", cascade = [CascadeType.ALL], orphanRemoval = true)
var folders: MutableList<AccountFolder>? = mutableListOf()

// TODO: 2022/05/06 공유 북마크인지 확인하기 위해서 추가한 컬럼
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ class FolderService(
if(!folder.isRootFolder()) throw RuntimeException("보관함이 아닙니다! 공유를 할 수 없습니다.")

// 하위 폴더들 모두 rootFolderId 추가해주기
val sharedFolderIdList = folderToShared(folder, rootFolderId)
val sharedFolderIdList = changeFolderToShared(folder, rootFolderId)

// 하위 북마크들 모두 shared가 true인 상태로 변경해주기
changeBookmarkToShared(sharedFolderIdList)
Expand All @@ -365,14 +365,14 @@ class FolderService(
bookmark.changeSharedTrue()
}

private fun folderToShared(folder: Folder, rootFolderId: Long): List<Long?> {
private fun changeFolderToShared(folder: Folder, rootFolderId: Long): List<Long?> {
val folderIdList = mutableListOf<Long?>()
folder.folderToShared(rootFolderId)
folderIdList.add(folder.id)

folder.children?.let {
for (child in it) {
folderIdList.addAll(folderToShared(child, rootFolderId))
folderIdList.addAll(changeFolderToShared(child, rootFolderId))
}
}

Expand Down

0 comments on commit 61869b7

Please sign in to comment.