Skip to content

Commit

Permalink
Merge pull request #17 from olmangjolmang/OMJM-79-edit-DB
Browse files Browse the repository at this point in the history
티클 문답 DB명 수정
  • Loading branch information
lej8924 authored Jul 7, 2024
2 parents 75f9530 + 40e82e8 commit e829eba
Show file tree
Hide file tree
Showing 29 changed files with 315 additions and 321 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "GlobalController", description = "전체 설정을 위해서 필요한 api")
@Tag(name = "Global", description = "전체 설정을 위해서 필요한 api")
@RestController
@RequiredArgsConstructor
@RequestMapping("/global")
Expand All @@ -19,7 +19,7 @@ public class GlobalController {
@Value("${server.env}")
private String env;

@Operation(summary = "health check", description = "cicd를 위한 health check api")
@Operation(summary = "health check", description = "CI/CD를 위한 health check api")
@GetMapping("/health-check")
public ResponseEntity<String> healthCheck() {
return ResponseEntity
Expand Down
14 changes: 7 additions & 7 deletions src/main/java/com/ticle/server/mypage/dto/MyQuestionDto.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.ticle.server.mypage.dto;

import com.ticle.server.talk.domain.Talk;
import com.ticle.server.opinion.domain.Opinion;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
Expand All @@ -11,15 +11,15 @@
public class MyQuestionDto {
private Long questionId;
private String question;
private Long view;
private Long viewCount;
private Long commentCount;

public static MyQuestionDto toDto(Talk talk){
public static MyQuestionDto toDto(Opinion opinion){
return MyQuestionDto.builder()
.questionId(talk.getTalkId())
.question(talk.getQuestion())
.view(talk.getViewCount())
.commentCount(talk.getCommentCount())
.questionId(opinion.getOpinionId())
.question(opinion.getQuestion())
.viewCount(opinion.getViewCount())
.commentCount(opinion.getCommentCount())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.ticle.server.mypage.repository;

import com.ticle.server.talk.domain.Talk;
import com.ticle.server.opinion.domain.Opinion;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface QuestionRepository extends JpaRepository<Talk, Long> {
List<Talk> findByUserId(Long userId);
public interface QuestionRepository extends JpaRepository<Opinion, Long> {
List<Opinion> findByUserId(Long userId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import com.ticle.server.post.domain.Post;
import com.ticle.server.post.repository.PostRepository;
import com.ticle.server.scrapped.domain.Scrapped;
import com.ticle.server.talk.domain.Talk;
import com.ticle.server.opinion.domain.Opinion;
import com.ticle.server.user.domain.type.Category;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -50,7 +50,7 @@ public List<SavedTicleDto> getSavedArticlesByCategory(Long userId, Category cate
}

public List<MyQuestionDto> getMyQuestions(Long userId) {
List<Talk> questions = questionRepository.findByUserId(userId);
List<Opinion> questions = questionRepository.findByUserId(userId);
return questions.stream()
.map(MyQuestionDto::toDto)
.collect(toList());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
package com.ticle.server.talk.controller;
package com.ticle.server.opinion.controller;

import com.ticle.server.global.dto.ResponseTemplate;
import com.ticle.server.talk.domain.type.Order;
import com.ticle.server.talk.dto.request.CommentUploadRequest;
import com.ticle.server.talk.dto.response.CommentResponse;
import com.ticle.server.talk.dto.response.TalkResponseList;
import com.ticle.server.talk.service.TalkService;
import com.ticle.server.opinion.domain.type.Order;
import com.ticle.server.opinion.dto.request.CommentUploadRequest;
import com.ticle.server.opinion.dto.response.CommentResponse;
import com.ticle.server.opinion.dto.response.OpinionResponseList;
import com.ticle.server.opinion.service.OpinionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
Expand All @@ -25,22 +22,22 @@
import static com.ticle.server.global.dto.ResponseTemplate.EMPTY_RESPONSE;

@Slf4j
@Tag(name = "Talk", description = "물어봥 관련 API")
@Tag(name = "Opinion", description = "티클문답 관련 API")
@RestController
@RequiredArgsConstructor
@RequestMapping("/talk")
public class TalkController {
@RequestMapping("/opinion")
public class OpinionController {

private final TalkService talkService;
private final OpinionService opinionService;

@Operation(summary = "댓글 등록", description = "질문에 대한 댓글 등록하기")
@PostMapping("/{talkId}/comment")
@PostMapping("/{opinionId}/comment")
public ResponseEntity<ResponseTemplate<Object>> uploadComment(
@PathVariable Long talkId,
@PathVariable Long opinionId,
@AuthenticationPrincipal UserDetails userDetails,
@Valid @RequestBody CommentUploadRequest request) {

talkService.uploadComment(request, talkId, userDetails);
opinionService.uploadComment(request, opinionId, userDetails);

return ResponseEntity
.status(HttpStatus.OK)
Expand All @@ -49,12 +46,12 @@ public ResponseEntity<ResponseTemplate<Object>> uploadComment(

@Operation(summary = "좋아요/취소 기능", description = "댓글에 대하여 좋아요 및 취소 기능<br>" +
"유저가 좋아요 했던 댓글이라면 취소를, 좋아요 하지 않았던 댓글이라면 좋아요를 할 수 있습니다.")
@PostMapping("/comment/{commentId}/heart/{userId}")
@PostMapping("/comment/{commentId}/heart")
public ResponseEntity<ResponseTemplate<Object>> heartComment(
@PathVariable Long commentId,
@PathVariable Long userId) {
@AuthenticationPrincipal UserDetails userDetails) {

talkService.heartComment(commentId, userId);
opinionService.heartComment(commentId, userDetails);

return ResponseEntity
.status(HttpStatus.OK)
Expand All @@ -64,13 +61,13 @@ public ResponseEntity<ResponseTemplate<Object>> heartComment(
@Operation(summary = "질문에 대한 모든 댓글 가져오기", description = "질문에 대한 모든 댓글 가져오는 API입니다.<br>" +
"orderBy에는 시간순이라면 time, 좋아요순이라면 heart를 넣어주시면 됩니다.(default는 시간순) <br>" +
"isHeart는 해당 유저가 좋아요를 눌렀는지 유무를 나타냅니다.")
@GetMapping("/{talkId}/{userId}")
@GetMapping("/{opinionId}")
public ResponseEntity<ResponseTemplate<Object>> getComments(
@PathVariable Long talkId,
@PathVariable Long userId,
@PathVariable Long opinionId,
@AuthenticationPrincipal UserDetails userDetails,
@RequestParam(required = false, defaultValue = "TIME") Order orderBy) {

List<CommentResponse> responses = talkService.getCommentsByTalk(talkId, userId, orderBy);
List<CommentResponse> responses = opinionService.getCommentsByOpinion(opinionId, userDetails, orderBy);

return ResponseEntity
.status(HttpStatus.OK)
Expand All @@ -79,10 +76,10 @@ public ResponseEntity<ResponseTemplate<Object>> getComments(

@Operation(summary = "물어봥 질문 리스트 조회", description = "물어봥 모든 질문과 함께 인기댓글 2개 조회 가능")
@GetMapping()
public ResponseEntity<ResponseTemplate<Object>> getTalks(
@RequestParam(defaultValue = "0") int page) {
public ResponseEntity<ResponseTemplate<Object>> getOpinions(
@RequestParam(defaultValue = "1") int page) {

TalkResponseList response = talkService.getTalksByPage(page);
OpinionResponseList response = opinionService.getOpinionsByPage(page);

return ResponseEntity
.status(HttpStatus.OK)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.ticle.server.talk.domain;
package com.ticle.server.opinion.domain;

import com.ticle.server.global.domain.BaseTimeEntity;
import com.ticle.server.user.domain.User;
Expand Down Expand Up @@ -29,21 +29,21 @@ public class Comment extends BaseTimeEntity {
@ManyToOne(fetch = FetchType.LAZY)
private User user;

@JoinColumn(name = "talk_id")
@JoinColumn(name = "opinion_id")
@ManyToOne(fetch = FetchType.LAZY)
private Talk talk;
private Opinion opinion;

@OneToMany(mappedBy = "comment", cascade = CascadeType.ALL, orphanRemoval = true)
@OneToMany(mappedBy = "comment", cascade = CascadeType.REMOVE, orphanRemoval = true)
private List<Heart> hearts = new ArrayList<>();

@Column(name = "heart_count")
private Long heartCount;

@Builder
public Comment(String content, User user, Talk talk, List<Heart> hearts, Long heartCount) {
public Comment(String content, User user, Opinion opinion, List<Heart> hearts, Long heartCount) {
this.content = content;
this.user = user;
this.talk = talk;
this.opinion = opinion;
this.hearts = hearts;
this.heartCount = heartCount;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.ticle.server.talk.domain;
package com.ticle.server.opinion.domain;

import com.ticle.server.user.domain.User;
import jakarta.persistence.*;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.ticle.server.talk.domain;
package com.ticle.server.opinion.domain;

import com.ticle.server.global.domain.BaseTimeEntity;
import com.ticle.server.user.domain.User;
Expand All @@ -11,16 +11,16 @@
import java.util.ArrayList;
import java.util.List;

@Table(name = "talk")
@Table(name = "opinion")
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Entity
public class Talk extends BaseTimeEntity {
public class Opinion extends BaseTimeEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "talk_id")
private Long talkId;
@Column(name = "opinion_id")
private Long opinionId;

@Column(name = "question", length = 200)
private String question;
Expand All @@ -32,14 +32,14 @@ public class Talk extends BaseTimeEntity {
@ManyToOne(fetch = FetchType.LAZY)
private User user;

@OneToMany(mappedBy = "talk", cascade = CascadeType.ALL, orphanRemoval = true)
@OneToMany(mappedBy = "opinion", cascade = CascadeType.REMOVE, orphanRemoval = true)
private List<Comment> comments = new ArrayList<>();

@Column(name = "comment_count")
private Long commentCount;

@Builder
public Talk(String question, Long viewCount, User user, List<Comment> comments, Long commentCount) {
public Opinion(String question, Long viewCount, User user, List<Comment> comments, Long commentCount) {
this.question = question;
this.viewCount = viewCount;
this.user = user;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.ticle.server.talk.domain.type;
package com.ticle.server.opinion.domain.type;

import org.springframework.data.domain.Sort;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package com.ticle.server.talk.dto.request;
package com.ticle.server.opinion.dto.request;

import com.ticle.server.talk.domain.Comment;
import com.ticle.server.talk.domain.Talk;
import com.ticle.server.opinion.domain.Comment;
import com.ticle.server.opinion.domain.Opinion;
import com.ticle.server.user.domain.User;
import jakarta.validation.constraints.NotNull;

public record CommentUploadRequest(
@NotNull(message = "내용을 입력해주세요.")
String content
) {
public Comment toComment(Talk talk, User user) {
public Comment toComment(Opinion opinion, User user) {
return Comment
.builder()
.talk(talk)
.opinion(opinion)
.content(content)
.heartCount(0L)
.user(user)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package com.ticle.server.talk.dto.response;
package com.ticle.server.opinion.dto.response;

import com.ticle.server.talk.domain.Comment;
import com.ticle.server.opinion.domain.Comment;
import lombok.Builder;

@Builder
public record CommentResponse(
Long commentId,
String nickname,
String content,
Long heartCount,
Boolean isHeart
) {
public static CommentResponse of(Comment comment, Boolean isHeart) {
return CommentResponse.builder()
.commentId(comment.getCommentId())
.nickname(comment.getUser().getNickName())
.content(comment.getContent())
.heartCount(comment.getHeartCount())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.ticle.server.talk.dto.response;
package com.ticle.server.opinion.dto.response;

import com.ticle.server.talk.domain.Comment;
import com.ticle.server.opinion.domain.Comment;
import lombok.Builder;

@Builder
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.ticle.server.talk.dto.response;
package com.ticle.server.opinion.dto.response;

import com.ticle.server.talk.domain.Comment;
import com.ticle.server.talk.domain.Heart;
import com.ticle.server.opinion.domain.Comment;
import com.ticle.server.opinion.domain.Heart;
import com.ticle.server.user.domain.User;

public record HeartResponse(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
package com.ticle.server.talk.dto.response;
package com.ticle.server.opinion.dto.response;

import com.ticle.server.talk.domain.Talk;
import com.ticle.server.opinion.domain.Opinion;
import jakarta.validation.constraints.Size;
import lombok.Builder;

import java.util.List;

@Builder
public record TalkResponse(
public record OpinionResponse(
Long opinionId,
String question,
Long viewCount,
Long commentCount,
@Size(max = 2, message = "최대 2개의 인기 댓글만 표시할 수 있습니다.")
List<CommentShortResponse> comments
) {
public static TalkResponse from(Talk talk) {
return TalkResponse.builder()
.question(talk.getQuestion())
.viewCount(talk.getViewCount())
.commentCount(talk.getCommentCount())
.comments(talk.getComments().stream()
public static OpinionResponse from(Opinion opinion) {
return OpinionResponse.builder()
.opinionId(opinion.getOpinionId())
.question(opinion.getQuestion())
.viewCount(opinion.getViewCount())
.commentCount(opinion.getCommentCount())
.comments(opinion.getComments().stream()
.sorted((c1, c2) -> Long.compare(c2.getHeartCount(), c1.getHeartCount()))
.limit(2)
.map(CommentShortResponse::from)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.ticle.server.opinion.dto.response;

import lombok.Builder;

import java.util.List;

@Builder
public record OpinionResponseList(
List<OpinionResponse> opinionResponseList
) {

public static OpinionResponseList from(List<OpinionResponse> opinionList) {
return new OpinionResponseList(opinionList);
}
}
Loading

0 comments on commit e829eba

Please sign in to comment.