-
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.
feat: TalkRoom Comment 등록 API 구현 (#24)
- Loading branch information
Showing
7 changed files
with
161 additions
and
0 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
src/main/java/com/jisungin/api/comment/CommentController.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,31 @@ | ||
package com.jisungin.api.comment; | ||
|
||
import com.jisungin.api.ApiResponse; | ||
import com.jisungin.api.comment.request.CommentCreateRequest; | ||
import com.jisungin.api.oauth.Auth; | ||
import com.jisungin.api.oauth.AuthContext; | ||
import com.jisungin.application.comment.CommentService; | ||
import com.jisungin.application.comment.response.CommentResponse; | ||
import jakarta.validation.Valid; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RequiredArgsConstructor | ||
@RequestMapping("/v1") | ||
@RestController | ||
public class CommentController { | ||
|
||
private final CommentService commentService; | ||
|
||
@PostMapping("/talk-rooms/{talkRoomId}/comments") | ||
public ApiResponse<CommentResponse> writeComment(@PathVariable Long talkRoomId, | ||
@Valid @RequestBody CommentCreateRequest request, | ||
@Auth AuthContext authContext) { | ||
return ApiResponse.ok(commentService.writeComment(request.toService(), talkRoomId, authContext.getUserId())); | ||
} | ||
|
||
} |
27 changes: 27 additions & 0 deletions
27
src/main/java/com/jisungin/api/comment/request/CommentCreateRequest.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,27 @@ | ||
package com.jisungin.api.comment.request; | ||
|
||
import com.jisungin.application.comment.request.CommentCreateServiceRequest; | ||
import jakarta.validation.constraints.NotBlank; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@NoArgsConstructor | ||
public class CommentCreateRequest { | ||
|
||
@NotBlank(message = "내용은 필수 입니다.") | ||
private String content; | ||
|
||
@Builder | ||
private CommentCreateRequest(String content) { | ||
this.content = content; | ||
} | ||
|
||
public CommentCreateServiceRequest toService() { | ||
return CommentCreateServiceRequest.builder() | ||
.content(content) | ||
.build(); | ||
} | ||
|
||
} |
38 changes: 38 additions & 0 deletions
38
src/main/java/com/jisungin/application/comment/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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.jisungin.application.comment; | ||
|
||
import com.jisungin.application.comment.request.CommentCreateServiceRequest; | ||
import com.jisungin.application.comment.response.CommentResponse; | ||
import com.jisungin.domain.comment.Comment; | ||
import com.jisungin.domain.comment.repository.CommentRepository; | ||
import com.jisungin.domain.talkroom.TalkRoom; | ||
import com.jisungin.domain.talkroom.repository.TalkRoomRepository; | ||
import com.jisungin.domain.user.User; | ||
import com.jisungin.domain.user.repository.UserRepository; | ||
import com.jisungin.exception.BusinessException; | ||
import com.jisungin.exception.ErrorCode; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
|
||
@RequiredArgsConstructor | ||
@Service | ||
public class CommentService { | ||
|
||
private final CommentRepository commentRepository; | ||
private final TalkRoomRepository talkRoomRepository; | ||
private final UserRepository userRepository; | ||
|
||
public CommentResponse writeComment(CommentCreateServiceRequest request, Long talkRoomId, Long userId) { | ||
User user = userRepository.findById(userId) | ||
.orElseThrow(() -> new BusinessException(ErrorCode.USER_NOT_FOUND)); | ||
|
||
TalkRoom talkRoom = talkRoomRepository.findById(talkRoomId) | ||
.orElseThrow(() -> new BusinessException(ErrorCode.TALK_ROOM_NOT_FOUND)); | ||
|
||
Comment comment = Comment.create(request, user, talkRoom); | ||
|
||
commentRepository.save(comment); | ||
|
||
return CommentResponse.of(comment.getContent(), user.getName()); | ||
} | ||
|
||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/com/jisungin/application/comment/request/CommentCreateServiceRequest.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,18 @@ | ||
package com.jisungin.application.comment.request; | ||
|
||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@NoArgsConstructor | ||
public class CommentCreateServiceRequest { | ||
|
||
private String content; | ||
|
||
@Builder | ||
private CommentCreateServiceRequest(String content) { | ||
this.content = content; | ||
} | ||
|
||
} |
28 changes: 28 additions & 0 deletions
28
src/main/java/com/jisungin/application/comment/response/CommentResponse.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,28 @@ | ||
package com.jisungin.application.comment.response; | ||
|
||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@NoArgsConstructor | ||
public class CommentResponse { | ||
|
||
private String content; | ||
|
||
private String userName; | ||
|
||
@Builder | ||
private CommentResponse(String content, String userName) { | ||
this.content = content; | ||
this.userName = userName; | ||
} | ||
|
||
public static CommentResponse of(String content, String name) { | ||
return CommentResponse.builder() | ||
.content(content) | ||
.userName(name) | ||
.build(); | ||
} | ||
|
||
} |
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
10 changes: 10 additions & 0 deletions
10
src/main/java/com/jisungin/domain/comment/repository/CommentRepository.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,10 @@ | ||
package com.jisungin.domain.comment.repository; | ||
|
||
import com.jisungin.domain.comment.Comment; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
@Repository | ||
public interface CommentRepository extends JpaRepository<Comment, Long> { | ||
|
||
} |