-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
327c4a2
commit ab98218
Showing
6 changed files
with
140 additions
and
1 deletion.
There are no files selected for viewing
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
44 changes: 44 additions & 0 deletions
44
.../daon/onjung/suggestion/application/controller/command/SuggestionCommandV1Controller.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,44 @@ | ||
package com.daon.onjung.suggestion.application.controller.command; | ||
|
||
import com.daon.onjung.core.annotation.security.AccountID; | ||
import com.daon.onjung.core.dto.ResponseDto; | ||
import com.daon.onjung.suggestion.application.dto.request.CreateBoardRequestDto; | ||
import com.daon.onjung.suggestion.application.dto.request.CreateCommentRequestDto; | ||
import com.daon.onjung.suggestion.application.usecase.CreateBoardUseCase; | ||
import com.daon.onjung.suggestion.application.usecase.CreateCommentUseCase; | ||
import jakarta.validation.Valid; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.web.bind.annotation.*; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import java.util.UUID; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
public class SuggestionCommandV1Controller { | ||
|
||
private final CreateBoardUseCase createBoardUseCase; | ||
private final CreateCommentUseCase createCommentUseCase; | ||
|
||
@PostMapping(value = "/api/v1/boards", consumes = {MediaType.MULTIPART_FORM_DATA_VALUE, MediaType.APPLICATION_JSON_VALUE}) | ||
public ResponseDto<Void> createBoard( | ||
@AccountID UUID accountId, | ||
@RequestPart(value = "file", required = false) MultipartFile file, | ||
@RequestPart("body") @Valid CreateBoardRequestDto requestDto | ||
) { | ||
createBoardUseCase.execute(accountId, file, requestDto); | ||
return ResponseDto.created(null); | ||
} | ||
|
||
@PostMapping("/api/v1/boards/{id}/comments") | ||
public ResponseDto<Void> createComment( | ||
@AccountID UUID accountId, | ||
@PathVariable Long id, | ||
@RequestBody @Valid CreateCommentRequestDto requestDto | ||
) { | ||
createCommentUseCase.execute(accountId, id, requestDto); | ||
return ResponseDto.created(null); | ||
} | ||
|
||
} |
9 changes: 9 additions & 0 deletions
9
...main/java/com/daon/onjung/suggestion/application/dto/request/CreateCommentRequestDto.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,9 @@ | ||
package com.daon.onjung.suggestion.application.dto.request; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
|
||
public record CreateCommentRequestDto( | ||
@JsonProperty("content") | ||
String content | ||
) { | ||
} |
50 changes: 50 additions & 0 deletions
50
src/main/java/com/daon/onjung/suggestion/application/service/CreateCommentService.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,50 @@ | ||
package com.daon.onjung.suggestion.application.service; | ||
|
||
import com.daon.onjung.account.domain.User; | ||
import com.daon.onjung.account.repository.mysql.UserRepository; | ||
import com.daon.onjung.core.exception.error.ErrorCode; | ||
import com.daon.onjung.core.exception.type.CommonException; | ||
import com.daon.onjung.suggestion.application.dto.request.CreateCommentRequestDto; | ||
import com.daon.onjung.suggestion.application.usecase.CreateCommentUseCase; | ||
import com.daon.onjung.suggestion.domain.Board; | ||
import com.daon.onjung.suggestion.domain.Comment; | ||
import com.daon.onjung.suggestion.domain.service.CommentService; | ||
import com.daon.onjung.suggestion.repository.mysql.BoardRepository; | ||
import com.daon.onjung.suggestion.repository.mysql.CommentRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.util.UUID; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class CreateCommentService implements CreateCommentUseCase { | ||
|
||
private final UserRepository userRepository; | ||
private final BoardRepository boardRepository; | ||
private final CommentRepository commentRepository; | ||
|
||
private final CommentService commentService; | ||
|
||
@Override | ||
@Transactional | ||
public void execute(UUID accountId, Long boardId, CreateCommentRequestDto requestDto) { | ||
|
||
// 유저 조회 | ||
User user = userRepository.findById(accountId) | ||
.orElseThrow(() -> new CommonException(ErrorCode.NOT_FOUND_RESOURCE)); | ||
|
||
// 게시글 조회 | ||
Board board = boardRepository.findById(boardId) | ||
.orElseThrow(() -> new CommonException(ErrorCode.NOT_FOUND_RESOURCE)); | ||
|
||
// 댓글 생성 | ||
Comment comment = commentService.createComment( | ||
requestDto.content(), | ||
user, | ||
board | ||
); | ||
commentRepository.save(comment); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/com/daon/onjung/suggestion/application/usecase/CreateCommentUseCase.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,11 @@ | ||
package com.daon.onjung.suggestion.application.usecase; | ||
|
||
import com.daon.onjung.core.annotation.bean.UseCase; | ||
import com.daon.onjung.suggestion.application.dto.request.CreateCommentRequestDto; | ||
|
||
import java.util.UUID; | ||
|
||
@UseCase | ||
public interface CreateCommentUseCase { | ||
void execute(UUID accountId, Long boardId, CreateCommentRequestDto requestDto); | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/com/daon/onjung/suggestion/domain/service/CommentService.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 |
---|---|---|
@@ -1,7 +1,22 @@ | ||
package com.daon.onjung.suggestion.domain.service; | ||
|
||
import com.daon.onjung.account.domain.User; | ||
import com.daon.onjung.suggestion.domain.Board; | ||
import com.daon.onjung.suggestion.domain.Comment; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
public class CommentService { | ||
|
||
public Comment createComment( | ||
String content, | ||
User user, | ||
Board board | ||
) { | ||
return Comment.builder() | ||
.content(content) | ||
.user(user) | ||
.board(board) | ||
.build(); | ||
} | ||
} |