-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #69 from dd-jiyun/main
Answer ๊ธฐ๋ฅ ์์ฑ
- Loading branch information
Showing
11 changed files
with
345 additions
and
19 deletions.
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
102 changes: 102 additions & 0 deletions
102
...nd/src/main/java/com/example/titto_backend/questionBoard/controller/AnswerController.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,102 @@ | ||
package com.example.titto_backend.questionBoard.controller; | ||
|
||
import com.example.titto_backend.auth.domain.User; | ||
import com.example.titto_backend.auth.repository.UserRepository; | ||
import com.example.titto_backend.common.exception.CustomException; | ||
import com.example.titto_backend.common.exception.ErrorCode; | ||
import com.example.titto_backend.questionBoard.dto.AnswerDTO; | ||
import com.example.titto_backend.questionBoard.dto.AnswerDTO.Response; | ||
import com.example.titto_backend.questionBoard.service.AnswerService; | ||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.Parameter; | ||
import io.swagger.v3.oas.annotations.responses.ApiResponse; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import java.security.Principal; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.web.bind.annotation.DeleteMapping; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
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; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/answers") | ||
@Tag(name = "Answer Controller", description = "๊ฒ์๊ธ ๋ต๋ณ ๊ด๋ จ API") | ||
public class AnswerController { | ||
|
||
private final AnswerService answerService; | ||
private final UserRepository userRepository; | ||
|
||
// Create | ||
@PostMapping("/create") | ||
@Operation( | ||
summary = "๋ต๋ณ ์์ฑ", | ||
description = "๋ต๋ณ์ ์์ฑํฉ๋๋ค", | ||
responses = { | ||
@ApiResponse(responseCode = "201", description = "๋ต๋ณ ์์ฑ ์ฑ๊ณต"), | ||
@ApiResponse(responseCode = "500", description = "๊ด๋ฆฌ์ ๋ฌธ์") | ||
}) | ||
public ResponseEntity<AnswerDTO.Response> createAnswer(@RequestBody AnswerDTO.Request request, Principal principal) { | ||
String email = principal.getName(); | ||
AnswerDTO.Response savedAnswer = answerService.save(request, request.getPostId(), email); | ||
|
||
return ResponseEntity.status(201).body(savedAnswer); | ||
} | ||
|
||
// Read | ||
@GetMapping("/answers") | ||
@Operation( | ||
summary = "๋ต๋ณ ๋ชฉ๋ก ์กฐํ", | ||
description = "๋ต๋ณ ๋ชฉ๋ก์ ์กฐํํฉ๋๋ค", | ||
responses = { | ||
@ApiResponse(responseCode = "200", description = "์์ฒญ ์ฑ๊ณต"), | ||
@ApiResponse(responseCode = "500", description = "๊ด๋ฆฌ์ ๋ฌธ์") | ||
}) | ||
public ResponseEntity<Page<Response>> getAllQuestions(@Parameter(hidden = true) Pageable pageable) { | ||
Page<AnswerDTO.Response> answers = answerService.findAll(pageable); | ||
return ResponseEntity.ok(answers); | ||
} | ||
|
||
@GetMapping("/{answerId}") | ||
@Operation( | ||
summary = "๋ต๋ณ ์์ธ ์กฐํ", | ||
description = "๋ต๋ณ ์์ธ๋ฅผ ์กฐํํฉ๋๋ค", | ||
responses = { | ||
@ApiResponse(responseCode = "200", description = "์์ฒญ ์ฑ๊ณต"), | ||
@ApiResponse(responseCode = "404", description = "๋ต๋ณ ์์") | ||
}) | ||
public ResponseEntity<AnswerDTO.Response> getAnswerById(@PathVariable("answerId") Long answerId) { | ||
try { | ||
AnswerDTO.Response answer = answerService.findById(answerId); | ||
return ResponseEntity.ok(answer); | ||
} catch (Exception e) { | ||
return ResponseEntity.status(HttpStatus.NOT_FOUND).build(); | ||
} | ||
} | ||
|
||
// Delete | ||
@DeleteMapping("/{answerId}") | ||
@Operation( | ||
summary = "๋ต๋ณ ์ญ์ ", | ||
description = "๋ต๋ณ์ ์ญ์ ํฉ๋๋ค", | ||
responses = { | ||
@ApiResponse(responseCode = "204", description = "๋ต๋ณ ์ญ์ ์ฑ๊ณต"), | ||
@ApiResponse(responseCode = "500", description = "๊ด๋ฆฌ์ ๋ฌธ์") | ||
}) | ||
public ResponseEntity<Void> deleteAnswer(@PathVariable("answerId") Long answerId) { | ||
String currentEmail = SecurityContextHolder.getContext().getAuthentication().getName(); // ํ์ฌ ์ฌ์ฉ์์ ์ด๋ฉ์ผ ์ฃผ์ ๊ฐ์ ธ์ค๊ธฐ | ||
User currentUser = userRepository.findByEmail(currentEmail) | ||
.orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND)); // ํ์ฌ ์ฌ์ฉ์์ ID๋ฅผ ์ด๋ฉ์ผ ์ฃผ์๋ก ๊ฒ์ํ์ฌ ๊ฐ์ ธ์ค๊ธฐ | ||
answerService.delete(answerId, currentUser.getId()); // ํ์ฌ ์ฌ์ฉ์์ ID๋ฅผ ์ ๋ฌํ์ฌ ์ญ์ ๋ฉ์๋ ํธ์ถ | ||
return ResponseEntity.noContent().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
44 changes: 44 additions & 0 deletions
44
Titto_Backend/src/main/java/com/example/titto_backend/questionBoard/domain/Answer.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.example.titto_backend.questionBoard.domain; | ||
|
||
import com.example.titto_backend.auth.domain.User; | ||
import com.example.titto_backend.common.BaseEntity; | ||
import jakarta.persistence.Column; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.GenerationType; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.JoinColumn; | ||
import jakarta.persistence.ManyToOne; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Entity | ||
@Builder | ||
@Getter | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class Answer extends BaseEntity { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
|
||
@ManyToOne | ||
@JoinColumn(name = "question_id") | ||
private Question question; | ||
|
||
// ๋ต๊ธ ์์ฑ์ | ||
@ManyToOne | ||
@JoinColumn(name = "author") | ||
private User author; | ||
|
||
// ๋ต๊ธ ๋ด์ฉ | ||
@Column(name = "answer_content", nullable = false, columnDefinition = "TEXT") | ||
private String content; | ||
|
||
// ์ฑํ ์ฌ๋ถ | ||
@Column(name = "is_accepted") | ||
private boolean isAccepted; | ||
|
||
} |
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
60 changes: 60 additions & 0 deletions
60
Titto_Backend/src/main/java/com/example/titto_backend/questionBoard/dto/AnswerDTO.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,60 @@ | ||
package com.example.titto_backend.questionBoard.dto; | ||
|
||
import com.example.titto_backend.questionBoard.domain.Answer; | ||
import com.fasterxml.jackson.annotation.JsonInclude; | ||
import io.swagger.v3.oas.annotations.media.Schema; | ||
import jakarta.validation.constraints.NotBlank; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
public class AnswerDTO { | ||
|
||
@Data | ||
@Builder | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
@Schema(description = "์ง๋ฌธ ๊ธ์ ๋๊ธ ์์ฑ") | ||
public static class Request { | ||
|
||
@Schema(description = "์ง๋ฌธ ID") | ||
private Long postId; | ||
|
||
@Schema(description = "๋ต๋ณ ๋ด์ฉ") | ||
@NotBlank | ||
private String content; | ||
} | ||
|
||
@Data | ||
@JsonInclude(JsonInclude.Include.NON_NULL) | ||
@Schema(description = "์ง๋ฌธ ๊ธ์ ๋๊ธ ์กฐํ") | ||
public static class Response { | ||
@Schema(description = "๋ต๋ณ ID") | ||
private Long id; | ||
|
||
@Schema(description = "์ง๋ฌธ ID") | ||
private Long postId; | ||
|
||
@Schema(description = "๋ต๋ณ ์์ฑ์ ID") | ||
private String authorId; | ||
|
||
@Schema(description = "๋ต๋ณ ์์ฑ์ ๋๋ค์") | ||
private String authorNickname; | ||
|
||
@Schema(description = "๋ต๋ณ ๋ด์ฉ") | ||
private String content; | ||
|
||
@Schema(description = "์ฑํ ์ฌ๋ถ", defaultValue = "false") | ||
private boolean isAccepted; | ||
|
||
public Response(Answer answer) { | ||
this.id = answer.getId(); | ||
this.postId = answer.getQuestion().getId(); | ||
this.authorId = answer.getAuthor().getId().toString(); | ||
this.authorNickname = answer.getAuthor().getNickname(); | ||
this.content = answer.getContent(); | ||
this.isAccepted = answer.isAccepted(); | ||
} | ||
} | ||
} |
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
...nd/src/main/java/com/example/titto_backend/questionBoard/repository/AnswerRepository.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.example.titto_backend.questionBoard.repository; | ||
|
||
import com.example.titto_backend.questionBoard.domain.Answer; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface AnswerRepository extends JpaRepository<Answer, Long> { | ||
Page<Answer> findAllByOrderByCreateDateDesc(Pageable pageable); | ||
} |
Oops, something went wrong.