This repository has been archived by the owner on Nov 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
40 additions
and
0 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
src/main/java/com/example/daemawiki/domain/editor/service/AddEditor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.example.daemawiki.domain.editor.service; | ||
|
||
import com.example.daemawiki.domain.document.component.facade.DocumentFacade; | ||
import com.example.daemawiki.domain.document.repository.DocumentRepository; | ||
import com.example.daemawiki.domain.editor.dto.AddEditorRequest; | ||
import com.example.daemawiki.domain.editor.model.Editor; | ||
import com.example.daemawiki.domain.user.service.facade.UserFacade; | ||
import com.example.daemawiki.global.exception.h403.NoPermissionUserException; | ||
import org.springframework.stereotype.Service; | ||
import reactor.core.publisher.Mono; | ||
|
||
@Service | ||
public class AddEditor { | ||
private final UserFacade userFacade; | ||
private final DocumentFacade documentFacade; | ||
private final DocumentRepository documentRepository; | ||
|
||
public AddEditor(UserFacade userFacade, DocumentFacade documentFacade, DocumentRepository documentRepository) { | ||
this.userFacade = userFacade; | ||
this.documentFacade = documentFacade; | ||
this.documentRepository = documentRepository; | ||
} | ||
|
||
public Mono<Void> execute(AddEditorRequest request) { | ||
return userFacade.currentUser() | ||
.flatMap(user -> documentFacade.findDocumentById(user.getDocumentId()) | ||
.filter(document -> document.getEditor().getCreatedUser().id().equals(user.getId())) | ||
.switchIfEmpty(Mono.error(NoPermissionUserException.EXCEPTION)) | ||
.zipWith(userFacade.findByEmailNotNull(request.email()), (document, user2) -> { | ||
document.getEditor().getCanEdit().add(Editor.builder() | ||
.user(user2.getEmail()) | ||
.id(user2.getId()) | ||
.build()); | ||
return document; | ||
}) | ||
.flatMap(documentRepository::save)) | ||
.then(); | ||
} | ||
|
||
} |