Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,53 +1,58 @@
package com.example.interviewPrep.quiz.answer.controller;

import com.example.interviewPrep.quiz.answer.dto.AnswerDTO;
import com.example.interviewPrep.quiz.answer.dto.request.AnswerRequest;
import com.example.interviewPrep.quiz.answer.dto.response.AnswerResponse;
import com.example.interviewPrep.quiz.answer.dto.response.SolutionResponse;
import com.example.interviewPrep.quiz.answer.service.AnswerService;
import com.example.interviewPrep.quiz.response.ResultResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;


@RestController
@RequestMapping("/answer")
@RequiredArgsConstructor
@RequestMapping("/api/v1/answers")
@CrossOrigin(origins = "*")
public class AnswerController {

private final AnswerService answerService;

public AnswerController(AnswerService answerService){
this.answerService = answerService;
}

@PostMapping()
public ResultResponse<?> createAnswer(@RequestBody @Valid AnswerDTO answerDTO){
return ResultResponse.success(answerService.createAnswer(answerDTO));
public ResponseEntity<Void> createAnswer(@RequestBody AnswerRequest answerRequest){
answerService.createAnswer(answerRequest);
return ResponseEntity.status(HttpStatus.CREATED).build();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

개인적으로 성공응답은 그냥 다 200으로 내려도 된다고 생각해요. 성공 상태에 따라서 뭐 분기할 일이 없어서 ..

}


@GetMapping("/{id}")
public ResultResponse<?> readAnswer(@PathVariable Long id){
return ResultResponse.success(answerService.readAnswer(id));
public ResponseEntity<AnswerResponse> readAnswer(@PathVariable Long id){
return ResponseEntity.ok(answerService.readAnswer(id));
}


@DeleteMapping("/{id}")
public ResultResponse<?> deleteAnswer(@PathVariable Long id){
return ResultResponse.success(answerService.deleteAnswer(id));
public ResponseEntity<Void> deleteAnswer(@PathVariable Long id){
answerService.deleteAnswer(id);
return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
}


@GetMapping("/solution/{id}/{type}")
public ResultResponse<?> findSolutionAnswer(@PathVariable Long id, @PathVariable String type,
@PageableDefault(size=10) Pageable pageable){
return ResultResponse.success(answerService.getSolution(id, type, pageable));
@GetMapping("/solution/{id}/by-type/{type}")
public ResponseEntity<Page<SolutionResponse>> findSolutionAnswer(@PathVariable Long id, @PathVariable String type,
@PageableDefault(size=10) Pageable pageable){
return ResponseEntity.ok(answerService.getSolution(id, type, pageable));
}


@GetMapping("/solution/check/{id}")
public ResultResponse<?> checkMySolution(@PathVariable Long id){
public ResponseEntity<Void> checkMySolution(@PathVariable Long id){
answerService.checkMySolution(id);
return ResultResponse.success();
return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
}

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.example.interviewPrep.quiz.answer.controller;

import com.example.interviewPrep.quiz.answer.dto.req.CommentReq;
import com.example.interviewPrep.quiz.answer.dto.request.CommentRequest;
import com.example.interviewPrep.quiz.answer.dto.response.CommentResponse;
import com.example.interviewPrep.quiz.answer.service.CommentService;
import com.example.interviewPrep.quiz.response.ResultResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.*;
Expand All @@ -12,31 +13,33 @@


@RestController
@RequestMapping("/answer")
@RequiredArgsConstructor
@CrossOrigin(origins = "*")
public class CommentController {

private final CommentService commentService;

@GetMapping("/comment/{id}")
public ResultResponse<?> findAnswerComment(@PathVariable Long id, @PageableDefault(size=10) Pageable pageable){
public CommentController(CommentService commentService){
this.commentService = commentService;
}

@GetMapping("/api/v1/answer/comment/{id}")
public ResultResponse<Page<CommentResponse>> findAnswerComment(@PathVariable Long id, @PageableDefault(size=10) Pageable pageable){

return ResultResponse.success(commentService.findAnswerComment(id, pageable));
}

@PostMapping("/comment")
public ResultResponse<?> createAnswerComment(@RequestBody @Valid CommentReq commentReq){
return ResultResponse.success(commentService.createComment(commentReq));
@PostMapping("/api/v1/answer/comment")
public ResultResponse<CommentResponse> createAnswerComment(@RequestBody @Valid CommentRequest commentRequest){
return ResultResponse.success(commentService.createComment(commentRequest));
}

@PutMapping("/comment")
public ResultResponse<?> updateAnswerComment(@RequestBody @Valid CommentReq commentReq){
commentService.updateComment(commentReq);
@PutMapping("/api/v1/answer/comment")
public ResultResponse<?> updateAnswerComment(@RequestBody @Valid CommentRequest commentRequest){
commentService.updateComment(commentRequest);
return ResultResponse.success();
}

@DeleteMapping("/comment/{id}")
@DeleteMapping("/api/v1/answer/comment/{id}")
public ResultResponse<?> deleteAnswerComment(@PathVariable Long id){
commentService.deleteComment(id);
return ResultResponse.success();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,73 +2,92 @@

import com.example.interviewPrep.quiz.domain.BaseTimeEntity;

// import com.example.interviewPrep.quiz.exam.domain.ExamAnswer;
import com.example.interviewPrep.quiz.member.domain.Member;
import com.example.interviewPrep.quiz.question.domain.Question;
import com.example.interviewPrep.quiz.heart.exception.HeartExistException;
import com.fasterxml.jackson.annotation.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import javax.persistence.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import static javax.persistence.FetchType.LAZY;

@Entity
@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
@JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class)
public class Answer extends BaseTimeEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ANSWER_ID")
private Long id;

@Lob
private String content;

@ManyToOne(fetch = LAZY)
@JsonBackReference(value="question-answer")
@JoinColumn(name = "QUESTION_ID")
Question question;

@ManyToOne(fetch = LAZY)
@JsonBackReference(value="member-answer")
@JoinColumn(name = "MEMBER_ID")
Member member;

private int commentCnt;

private int heartCnt;

@Version
private Long version;

@Builder
public Answer(Long id, String content, Question question, Member member, int commentCnt, int heartCnt, Long version){

Objects.requireNonNull(id, "id가 null입니다.");
Objects.requireNonNull(question, "question이 null입니다.");
Objects.requireNonNull(member, "member이 null입니다.");

this.id = id;
this.content = content;
this.question = question;
this.member = member;
this.commentCnt = commentCnt;
this.heartCnt = heartCnt;
this.version = version;
}

public static Answer createAnswerEntity(Member member, Question question, String content){
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이건 왜있는걸까요 ?

return Answer.builder()
.member(member)
.question(question)
.content(content)
.build();
}


public void change(String content){
this.content = content;
}

public int increase() {
public synchronized int increase() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

synchronized가 이런식으로 걸리면 성능에 어떤 영향이 있을까요 ? AtomicInteger에 대해서 찾아보시면 좋을 것 같아요 ~

return ++this.heartCnt;
}

public int decrease() {
public synchronized int decrease() {
if (this.heartCnt <= 0) {
throw new HeartExistException("좋아요 수가 0보다 작아 좋아요 수를 감소시킬수 없습니다.");
}
return --this.heartCnt;
}

public int commentIncrease() { return ++this.commentCnt; }
public synchronized int commentIncrease() {
return ++this.commentCnt;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

동시에 동일한 답변에 코멘트가 달리는 경우, 이 메서드는 스레드세이프하게 동작할 수 있을까요 ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

스레드 세이프 하게 동작하지 않을 것 같습니다.
스레드 세이프 하게 동작하도록 수정해야 할 것 같습니다

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

반영 완료

}

public int commentDecrease() {return --this.commentCnt;}
public synchronized int commentDecrease() {
return --this.commentCnt;
}

// @OneToMany(mappedBy = "answer")
// List<ExamAnswer> exams = new ArrayList<>();
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
import com.example.interviewPrep.quiz.member.domain.Member;
import com.example.interviewPrep.quiz.notification.domain.Notification;
import java.util.List;
import lombok.AllArgsConstructor;
import java.util.Objects;

import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
Expand All @@ -17,7 +18,6 @@
@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class AnswerComment extends BaseTimeEntity {

@Id
Expand All @@ -33,8 +33,6 @@ public class AnswerComment extends BaseTimeEntity {
@JoinColumn(name = "MEMBER_ID")
Member member;

// Notification 엔티티와 @OneToMany 참조 관계를 설정하였습니다
// 연관관계의 주인을 Notification.comment로 설정하였습니다
@OneToMany(mappedBy = "comment")
List<Notification> notifications;

Expand All @@ -44,6 +42,21 @@ public class AnswerComment extends BaseTimeEntity {

private String comment;

public AnswerComment(Long id, Answer answer, Member member, List<Notification> notifications, Member answerWriter, String comment) {

Objects.requireNonNull(id, "id가 null입니다.");
Objects.requireNonNull(answer, "answer이 null입니다.");
Objects.requireNonNull(member, "member이 null입니다.");
Objects.requireNonNull(answerWriter, "answerWriter가 null입니다.");

this.id = id;
this.answer = answer;
this.member = member;
this.notifications = notifications;
this.answerWriter = answerWriter;
this.comment = comment;
}

public void change(String comment){
this.comment = comment;
}
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Loading