Skip to content
This repository has been archived by the owner on Nov 27, 2024. It is now read-only.

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
ori0o0p committed Mar 19, 2024
1 parent cf367e0 commit 8f68ee2
Show file tree
Hide file tree
Showing 110 changed files with 1,910 additions and 1,249 deletions.
10 changes: 8 additions & 2 deletions daemawiki-api/src/main/java/org/daemawiki/config/AppConfig.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
package org.daemawiki.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import reactor.core.scheduler.Scheduler;
import reactor.core.scheduler.Schedulers;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AppConfig {

/**
* boundedElastic 스케줄러를 빈으로 등록
*
* @return boundedElastic 스케줄러
*/
@Bean
public Scheduler scheduler() {
return Schedulers.boundedElastic();
}

}

Original file line number Diff line number Diff line change
@@ -1,44 +1,44 @@
package org.daemawiki.domain.auth.api;

import jakarta.validation.Valid;
import org.daemawiki.domain.auth.dto.request.LoginRequest;
import org.daemawiki.domain.auth.dto.request.ReissueRequest;
import org.daemawiki.domain.auth.dto.response.TokenResponse;
import org.daemawiki.domain.auth.dto.request.SignupRequest;
import org.daemawiki.domain.auth.service.Login;
import org.daemawiki.domain.auth.service.Reissue;
import org.daemawiki.domain.auth.service.Signup;
import jakarta.validation.Valid;
import org.daemawiki.domain.auth.dto.response.TokenResponse;
import org.daemawiki.domain.auth.usecase.ReissueUsecase;
import org.daemawiki.domain.auth.usecase.SigninUsecase;
import org.daemawiki.domain.auth.usecase.SignupUsecase;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;

@RestController
@RequestMapping("/api/auth")
public class AuthController {
private final Login loginService;
private final Signup signupService;
private final Reissue reissue;
private final SigninUsecase signinUsecase;
private final SignupUsecase signupUsecase;
private final ReissueUsecase reissueUsecase;

public AuthController(Login loginService, Signup signupService, Reissue reissue) {
this.loginService = loginService;
this.signupService = signupService;
this.reissue = reissue;
public AuthController(SigninUsecase signinUsecase, SignupUsecase signupUsecase, ReissueUsecase reissueUsecase) {
this.signinUsecase = signinUsecase;
this.signupUsecase = signupUsecase;
this.reissueUsecase = reissueUsecase;
}

@PostMapping("/login")
public Mono<TokenResponse> login(@Valid @RequestBody LoginRequest request) {
return loginService.execute(request);
return signinUsecase.signin(request);
}

@PostMapping("/signup")
@ResponseStatus(HttpStatus.CREATED)
public Mono<Void> signup(@Valid @RequestBody SignupRequest request) {
return signupService.execute(request);
return signupUsecase.signup(request);
}

@PostMapping("/reissue")
public Mono<TokenResponse> reissue(@Valid @RequestBody ReissueRequest request) {
return reissue.execute(request);
return reissueUsecase.reissue(request);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.daemawiki.domain.auth.usecase;

import org.daemawiki.domain.auth.dto.request.ReissueRequest;
import org.daemawiki.domain.auth.dto.response.TokenResponse;
import reactor.core.publisher.Mono;

public interface ReissueUsecase {
Mono<TokenResponse> reissue(ReissueRequest request);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.daemawiki.domain.auth.usecase;

import org.daemawiki.domain.auth.dto.request.LoginRequest;
import org.daemawiki.domain.auth.dto.response.TokenResponse;
import reactor.core.publisher.Mono;

public interface SigninUsecase {
Mono<TokenResponse> signin(LoginRequest request);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.daemawiki.domain.auth.usecase;

import org.daemawiki.domain.auth.dto.request.SignupRequest;
import reactor.core.publisher.Mono;

public interface SignupUsecase {
Mono<Void> signup(SignupRequest request);

}
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
package org.daemawiki.domain.auth.service;
package org.daemawiki.domain.auth.usecase.service;

import org.daemawiki.domain.auth.dto.request.ReissueRequest;
import org.daemawiki.domain.auth.dto.response.TokenResponse;
import org.daemawiki.domain.auth.usecase.ReissueUsecase;
import org.daemawiki.exception.h500.TokenReissueFailedException;
import org.daemawiki.security.Tokenizer;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Mono;
import org.daemawiki.security.Tokenizer;

@Service
public class Reissue {
public class Reissue implements ReissueUsecase {
private final Tokenizer tokenizer;

public Reissue(Tokenizer tokenizer) {
this.tokenizer = tokenizer;
}

public Mono<TokenResponse> execute(ReissueRequest request) {
@Override
public Mono<TokenResponse> reissue(ReissueRequest request) {
return tokenizer.reissue(request.token())
.map(TokenResponse::create)
.switchIfEmpty(Mono.error(TokenReissueFailedException.EXCEPTION));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,32 @@
package org.daemawiki.domain.auth.service;
package org.daemawiki.domain.auth.usecase.service;

import org.daemawiki.domain.auth.dto.request.LoginRequest;
import org.daemawiki.domain.auth.dto.response.TokenResponse;
import org.daemawiki.domain.auth.usecase.SigninUsecase;
import org.daemawiki.domain.user.application.GetUserPort;
import org.daemawiki.domain.user.model.User;
import org.daemawiki.domain.user.repository.UserRepository;
import org.daemawiki.exception.h401.PasswordMismatchException;
import org.daemawiki.exception.h404.UserNotFoundException;
import org.daemawiki.security.Tokenizer;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Mono;
import org.daemawiki.security.Tokenizer;

@Service
public class Login {
private final UserRepository userRepository;
public class SigninService implements SigninUsecase {
private final GetUserPort getUserPort;
private final PasswordEncoder passwordEncoder;
private final Tokenizer tokenizer;

public Login(UserRepository userRepository, PasswordEncoder passwordEncoder, Tokenizer tokenizer) {
this.userRepository = userRepository;
public SigninService(GetUserPort getUserPort, PasswordEncoder passwordEncoder, Tokenizer tokenizer) {
this.getUserPort = getUserPort;
this.passwordEncoder = passwordEncoder;
this.tokenizer = tokenizer;
}

public Mono<TokenResponse> execute(LoginRequest request) {
return userRepository.findByEmail(request.email())
@Override
public Mono<TokenResponse> signin(LoginRequest request) {
return getUserPort.findByEmail(request.email())
.switchIfEmpty(Mono.error(UserNotFoundException.EXCEPTION))
.flatMap(user -> validateUserAndPassword(user, request.password()))
.flatMap(user -> tokenizer.createToken(user.getEmail())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package org.daemawiki.domain.auth.service;
package org.daemawiki.domain.auth.usecase.service;

import org.daemawiki.domain.auth.dto.request.SignupRequest;
import org.daemawiki.domain.document.component.CreateDocumentByUser;
import org.daemawiki.domain.auth.usecase.SignupUsecase;
import org.daemawiki.domain.common.DefaultProfile;
import org.daemawiki.domain.mail.repository.AuthMailRepository;
import org.daemawiki.domain.document.usecase.CreateDocumentUsecase;
import org.daemawiki.domain.mail.application.mail.DeleteAuthMailPort;
import org.daemawiki.domain.mail.application.mail.GetAuthMailPort;
import org.daemawiki.domain.user.application.GetUserPort;
import org.daemawiki.domain.user.application.SaveUserPort;
import org.daemawiki.domain.user.model.User;
import org.daemawiki.domain.user.model.UserDetail;
import org.daemawiki.domain.user.repository.UserRepository;
import org.daemawiki.domain.user.type.major.component.GetMajorType;
import org.daemawiki.exception.h403.UnVerifiedEmailException;
import org.daemawiki.exception.h409.EmailAlreadyExistsException;
Expand All @@ -17,49 +20,54 @@
import reactor.core.scheduler.Scheduler;

@Service
public class Signup {
private final UserRepository userRepository;
private final AuthMailRepository authMailRepository;
public class Signup implements SignupUsecase {
private final GetUserPort getUserPort;
private final SaveUserPort saveUserPort;
private final GetAuthMailPort getAuthMailPort;
private final DeleteAuthMailPort deleteAuthMailPort;
private final PasswordEncoder passwordEncoder;
private final Scheduler scheduler;
private final GetMajorType getMajorType;
private final CreateDocumentByUser createDocumentByUser;
private final CreateDocumentUsecase createDocumentUsecase;
private final DefaultProfile defaultProfile;

public Signup(UserRepository userRepository,AuthMailRepository authMailRepository, PasswordEncoder passwordEncoder, Scheduler scheduler, GetMajorType getMajorType, CreateDocumentByUser createDocumentByUser, DefaultProfile defaultProfile) {
this.userRepository = userRepository;
this.authMailRepository = authMailRepository;
public Signup(GetUserPort getUserPort, SaveUserPort saveUserPort, GetAuthMailPort getAuthMailPort, DeleteAuthMailPort deleteAuthMailPort, PasswordEncoder passwordEncoder, Scheduler scheduler, GetMajorType getMajorType, CreateDocumentUsecase createDocumentUsecase, DefaultProfile defaultProfile) {
this.getUserPort = getUserPort;
this.saveUserPort = saveUserPort;
this.getAuthMailPort = getAuthMailPort;
this.passwordEncoder = passwordEncoder;
this.scheduler = scheduler;
this.getMajorType = getMajorType;
this.createDocumentByUser = createDocumentByUser;
this.deleteAuthMailPort = deleteAuthMailPort;
this.createDocumentUsecase = createDocumentUsecase;
this.defaultProfile = defaultProfile;
}

public Mono<Void> execute(SignupRequest request) {
return userRepository.findByEmail(request.email())
@Override
public Mono<Void> signup(SignupRequest request) {
return getUserPort.findByEmail(request.email())
.flatMap(user -> Mono.error(EmailAlreadyExistsException.EXCEPTION))
.switchIfEmpty(Mono.defer(() -> checkAndCreateUser(request)))
.then();
}

private Mono<Void> checkAndCreateUser(SignupRequest request) {
return authMailRepository.findByMail(request.email())
return getAuthMailPort.findByMail(request.email())
.filter(verified -> verified)
.switchIfEmpty(Mono.error(UnVerifiedEmailException.EXCEPTION))
.flatMap(verified -> Mono.fromSupplier(() -> passwordEncoder.encode(request.password()))
.subscribeOn(scheduler)
.flatMap(password -> createUser(request, password))
.flatMap(this::saveUserAndCreateDocument)
.then(authMailRepository.delete(request.email()))
.then(deleteAuthMailPort.delete(request.email()))
.onErrorMap(e -> ExecuteFailedException.EXCEPTION));
}

private Mono<User> saveUserAndCreateDocument(User user) {
return userRepository.save(user)
.flatMap(savedUser -> createDocumentByUser.execute(savedUser)
return saveUserPort.save(user)
.flatMap(savedUser -> createDocumentUsecase.createByUser(savedUser)
.doOnNext(document -> savedUser.setDocumentId(document.getId()))
.flatMap(document -> userRepository.save(savedUser)));
.flatMap(document -> saveUserPort.save(savedUser)));
}

private Mono<User> createUser(SignupRequest request, String password) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@
import org.daemawiki.domain.content.dto.DeleteContentRequest;
import org.daemawiki.domain.content.dto.EditContentTableTitleRequest;
import org.daemawiki.domain.content.dto.WriteContentRequest;
import org.daemawiki.domain.content.service.AddContentTable;
import org.daemawiki.domain.content.service.DeleteContentTable;
import org.daemawiki.domain.content.service.EditContentTableTitle;
import org.daemawiki.domain.content.service.WriteContent;
import org.daemawiki.domain.content.usecase.AddContentTableUsecase;
import org.daemawiki.domain.content.usecase.RemoveContentTableUsecase;
import org.daemawiki.domain.content.usecase.UpdateContentTableTitleUsecase;
import org.daemawiki.domain.content.usecase.WriteContentUsecase;
import org.daemawiki.domain.content.usecase.service.AddContentTableService;
import org.daemawiki.domain.content.usecase.service.RemoveContentTableService;
import org.daemawiki.domain.content.usecase.service.UpdateContentTableTitleService;
import org.daemawiki.domain.content.usecase.service.WriteContentService;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import org.springframework.http.HttpStatus;
Expand All @@ -17,40 +21,40 @@
@RestController
@RequestMapping("/api/documents/{documentId}/contents")
public class DocumentContentController {
private final WriteContent writeContentService;
private final AddContentTable addContentTableService;
private final DeleteContentTable deleteContentTableService;
private final EditContentTableTitle editContentTableTitleService;

public DocumentContentController(WriteContent writeContentService, AddContentTable addContentTableService, DeleteContentTable deleteContentTableService, EditContentTableTitle editContentTableTitleService) {
this.writeContentService = writeContentService;
this.addContentTableService = addContentTableService;
this.deleteContentTableService = deleteContentTableService;
this.editContentTableTitleService = editContentTableTitleService;
private final WriteContentUsecase writeContentUsecase;
private final AddContentTableUsecase addContentTableUsecase;
private final RemoveContentTableUsecase removeContentTableUsecase;
private final UpdateContentTableTitleUsecase updateContentTableTitleUsecase;

public DocumentContentController(WriteContentService writeContentUsecase, AddContentTableService addContentTableUsecase, RemoveContentTableService removeContentTableUsecase, UpdateContentTableTitleService updateContentTableTitleUsecase) {
this.writeContentUsecase = writeContentUsecase;
this.addContentTableUsecase = addContentTableUsecase;
this.removeContentTableUsecase = removeContentTableUsecase;
this.updateContentTableTitleUsecase = updateContentTableTitleUsecase;
}

@PatchMapping
@ResponseStatus(HttpStatus.NO_CONTENT)
public Mono<Void> writeContent(@Valid @RequestBody WriteContentRequest request, @NotBlank @PathVariable String documentId) {
return writeContentService.execute(request, documentId);
return writeContentUsecase.write(request, documentId);
}

@PatchMapping("/table")
@ResponseStatus(HttpStatus.NO_CONTENT)
public Mono<Void> addContentTable(@Valid @RequestBody AddContentRequest request, @NotBlank @PathVariable String documentId) {
return addContentTableService.execute(request, documentId);
return addContentTableUsecase.add(request, documentId);
}

@PatchMapping("/remove")
@ResponseStatus(HttpStatus.NO_CONTENT)
public Mono<Void> deleteContent(@Valid @RequestBody DeleteContentRequest request, @NotBlank @PathVariable String documentId) {
return deleteContentTableService.execute(request, documentId);
return removeContentTableUsecase.remove(request, documentId);
}

@PatchMapping("/title/edit")
@ResponseStatus(HttpStatus.NO_CONTENT)
public Mono<Void> editContentTitle(@Valid @RequestBody EditContentTableTitleRequest request, @PathVariable String documentId) {
return editContentTableTitleService.execute(request, documentId);
return updateContentTableTitleUsecase.update(request, documentId);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.daemawiki.domain.content.usecase;

import org.daemawiki.domain.content.dto.AddContentRequest;
import reactor.core.publisher.Mono;

public interface AddContentTableUsecase {
Mono<Void> add(AddContentRequest request, String documentId);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.daemawiki.domain.content.usecase;

import org.daemawiki.domain.content.dto.DeleteContentRequest;
import reactor.core.publisher.Mono;

public interface RemoveContentTableUsecase {
Mono<Void> remove(DeleteContentRequest request, String documentId);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.daemawiki.domain.content.usecase;

import org.daemawiki.domain.content.dto.EditContentTableTitleRequest;
import reactor.core.publisher.Mono;

public interface UpdateContentTableTitleUsecase {
Mono<Void> update(EditContentTableTitleRequest request, String documentId);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.daemawiki.domain.content.usecase;

import org.daemawiki.domain.content.dto.WriteContentRequest;
import reactor.core.publisher.Mono;

public interface WriteContentUsecase {
Mono<Void> write(WriteContentRequest request, String documentId);
}
Loading

0 comments on commit 8f68ee2

Please sign in to comment.