-
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 #54 from dd-jiyun/main
์ง๋ฌธ๊ฒ์ํ CR
- Loading branch information
Showing
11 changed files
with
435 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,4 +35,5 @@ out/ | |
|
||
### VS Code ### | ||
.vscode/ | ||
|
||
.DS_Store |
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
107 changes: 107 additions & 0 deletions
107
.../src/main/java/com/example/titto_backend/questionBoard/controller/QuestionController.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,107 @@ | ||
package com.example.titto_backend.questionBoard.controller; | ||
|
||
import com.example.titto_backend.questionBoard.dto.QuestionDTO; | ||
import com.example.titto_backend.questionBoard.service.QuestionService; | ||
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.PageRequest; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.data.domain.Sort; | ||
import org.springframework.data.domain.Sort.Direction; | ||
import org.springframework.data.web.PageableDefault; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
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.ResponseBody; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/questions") | ||
@Tag(name = "Question Controller", description = "์ง๋ฌธ ๊ฒ์ํ ๊ด๋ จ API") | ||
public class QuestionController { | ||
|
||
private final QuestionService questionService; | ||
|
||
|
||
@PostMapping("/create") | ||
@Operation( | ||
summary = "์ง๋ฌธ ์์ฑ", | ||
description = "์ง๋ฌธ์ ์์ฑํฉ๋๋ค", | ||
responses = { | ||
@ApiResponse(responseCode = "201", description = "์ง๋ฌธ ์์ฑ ์ฑ๊ณต"), | ||
@ApiResponse(responseCode = "500", description = "๊ด๋ฆฌ์ ๋ฌธ์") | ||
}) | ||
public ResponseEntity<QuestionDTO.Response> createQuestion(@RequestBody QuestionDTO.Request request, | ||
Principal principal) { | ||
String email = principal.getName(); // ์ฌ์ฉ์์ ์ด๋ฉ์ผ์ ๊ฐ์ ธ์ด | ||
|
||
QuestionDTO.Response savedQuestion = questionService.save(email, request); | ||
return ResponseEntity.status(HttpStatus.CREATED).body(savedQuestion); | ||
// try { | ||
// QuestionDTO.Response savedQuestion = questionService.save(email, request); | ||
// return ResponseEntity.status(HttpStatus.CREATED).body(savedQuestion); | ||
// } catch (Exception e) { | ||
// System.out.println(e.getMessage()); | ||
// return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build(); | ||
// } | ||
} | ||
|
||
@ResponseBody | ||
@GetMapping("/posts") | ||
@Operation( | ||
summary = "์ง๋ฌธ ๋ชฉ๋ก ์กฐํ", | ||
description = "์ง๋ฌธ ๋ชฉ๋ก์ ์กฐํํฉ๋๋ค", | ||
responses = { | ||
@ApiResponse(responseCode = "200", description = "์์ฒญ ์ฑ๊ณต"), | ||
@ApiResponse(responseCode = "500", description = "๊ด๋ฆฌ์ ๋ฌธ์") | ||
}) | ||
public ResponseEntity<Page<QuestionDTO.Response>> getAllQuestions(@PageableDefault(size=20, sort="createdAt", direction = Sort.Direction.DESC) @Parameter(hidden = true) Pageable pageable) { | ||
Page<QuestionDTO.Response> questions = questionService.findAll(pageable); | ||
return ResponseEntity.ok(questions); | ||
} | ||
|
||
@ResponseBody | ||
@GetMapping("/{postId}") | ||
@Operation( | ||
summary = "์ง๋ฌธ ์์ธ ์กฐํ", | ||
description = "์ง๋ฌธ ์์ธ ๋ด์ฉ์ ์กฐํํฉ๋๋ค", | ||
responses = { | ||
@ApiResponse(responseCode = "200", description = "์์ฒญ ์ฑ๊ณต"), | ||
@ApiResponse(responseCode = "404", description = "์ง๋ฌธ์ ์ฐพ์ ์ ์์") | ||
}) | ||
public ResponseEntity<QuestionDTO.Response> getQuestionById(@PathVariable("postId") Long postId) { | ||
try { | ||
QuestionDTO.Response question = questionService.findById(postId); | ||
return ResponseEntity.ok(question); | ||
} catch (Exception e) { | ||
return ResponseEntity.status(HttpStatus.NOT_FOUND).build(); | ||
} | ||
} | ||
|
||
@ResponseBody | ||
@GetMapping("/category/{category}") | ||
@Operation( | ||
summary = "์นดํ ๊ณ ๋ฆฌ๋ณ ์ง๋ฌธ ์กฐํ", | ||
description = "์นดํ ๊ณ ๋ฆฌ๋ณ ์ง๋ฌธ์ ์กฐํํฉ๋๋ค", | ||
responses = { | ||
@ApiResponse(responseCode = "200", description = "์์ฒญ ์ฑ๊ณต"), | ||
@ApiResponse(responseCode = "404", description = "์ง๋ฌธ์ ์ฐพ์ ์ ์์") | ||
}) | ||
public ResponseEntity<Page<QuestionDTO.Response>> getQuestionsByCategory(@PathVariable("category") String category, | ||
Pageable pageable) { | ||
Page<QuestionDTO.Response> questions = questionService.findByCategory(pageable, category); | ||
return ResponseEntity.ok(questions); | ||
} | ||
// Update method here | ||
// Delete method here | ||
} |
37 changes: 37 additions & 0 deletions
37
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,37 @@ | ||
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.Getter; | ||
import lombok.Setter; | ||
|
||
@Entity | ||
@Getter | ||
@Setter | ||
public class Answer extends BaseEntity { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
|
||
@JoinColumn(name = "author") | ||
@ManyToOne | ||
private User author; | ||
|
||
@Column(name = "answer_content", nullable = false, columnDefinition = "TEXT") | ||
private String content; | ||
|
||
@JoinColumn(name = "question_id") | ||
@ManyToOne | ||
private Question question; | ||
|
||
//์ฑํ ์ฌ๋ถ | ||
@Column(name = "is_adopted") | ||
private boolean isAdopted; | ||
} |
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
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
112 changes: 112 additions & 0 deletions
112
Titto_Backend/src/main/java/com/example/titto_backend/questionBoard/dto/QuestionDTO.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,112 @@ | ||
package com.example.titto_backend.questionBoard.dto; | ||
|
||
import com.example.titto_backend.questionBoard.domain.Department; | ||
import com.example.titto_backend.questionBoard.domain.Question; | ||
import com.example.titto_backend.questionBoard.domain.Status; | ||
import com.fasterxml.jackson.annotation.JsonInclude; | ||
import io.swagger.v3.oas.annotations.media.Schema; | ||
import java.time.LocalDateTime; | ||
import java.util.List; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
|
||
public class QuestionDTO { | ||
|
||
@Getter | ||
@Setter | ||
@Builder | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
@Schema(description = "์ง๋ฌธ ๊ธ ์์ฑ") | ||
public static class Request { | ||
|
||
@Schema(description = "์ ๋ชฉ") | ||
private String title; | ||
|
||
@Schema(description = "๋ด์ฉ") | ||
private String content; | ||
|
||
@Schema(description = "์ด๋ฏธ์ง") | ||
private List<String> imageList; | ||
|
||
@Schema(description = "์นดํ ๊ณ ๋ฆฌ") | ||
private String department; | ||
|
||
@Schema(description = "์ํ", example = "ACTIVE or INACTIVE") | ||
private String status; | ||
|
||
@Schema(description = "์กฐํ์", defaultValue = "1") | ||
private int view; | ||
|
||
public Question toEntity() { | ||
return Question.builder() | ||
.title(title) | ||
.content(content) | ||
.department(Department.valueOf(department.toUpperCase())) | ||
.status(Status.valueOf(status.toUpperCase())) | ||
.build(); | ||
} | ||
} | ||
|
||
@Getter | ||
@Setter | ||
@JsonInclude(JsonInclude.Include.NON_NULL) | ||
@Schema(description = "์ง๋ฌธ ๊ธ ์กฐํ") | ||
public static class Response { | ||
|
||
@Schema(description = "์ง๋ฌธ ID") | ||
private Long id; | ||
|
||
@Schema(description = "์ง๋ฌธ ์์ฑ์ ID") | ||
private Long authorId; | ||
|
||
@Schema(description = "์ง๋ฌธ ์์ฑ์ ๋๋ค์") | ||
private String authorNickname; | ||
|
||
@Schema(description = "์นดํ ๊ณ ๋ฆฌ") | ||
private String department; | ||
|
||
@Schema(description = "์ํ") | ||
private String status; | ||
|
||
@Schema(description = "์ ๋ชฉ") | ||
private String title; | ||
|
||
@Schema(description = "๋ด์ฉ") | ||
private String content; | ||
|
||
@Schema(description = "์์ฑ์ผ") | ||
private LocalDateTime createdDate; | ||
|
||
public Response(Question question) { | ||
this.id = question.getId(); | ||
this.authorId = question.getAuthor().getId(); | ||
this.authorNickname = question.getAuthor().getNickname(); | ||
this.department = question.getDepartment().toString(); | ||
this.status = question.getStatus().toString(); | ||
this.title = question.getTitle(); | ||
this.content = question.getContent(); | ||
} | ||
} | ||
|
||
@Getter | ||
@Setter | ||
@Builder | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
@Schema(description = "์ง๋ฌธ ๊ธ ์์ ") | ||
public static class Update { | ||
@Schema(description = "์ ๋ชฉ") | ||
private String title; | ||
@Schema(description = "๋ด์ฉ") | ||
private String content; | ||
// private List<String> imageList; | ||
@Schema(description = "์นดํ ๊ณ ๋ฆฌ") | ||
private Department department; | ||
@Schema(description = "์ํ") | ||
private Status status; | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
...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,8 @@ | ||
package com.example.titto_backend.questionBoard.repository; | ||
|
||
import com.example.titto_backend.questionBoard.domain.Answer; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface AnswerRepository extends JpaRepository<Answer, Long> { | ||
|
||
} |
11 changes: 11 additions & 0 deletions
11
.../src/main/java/com/example/titto_backend/questionBoard/repository/QuestionRepository.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.example.titto_backend.questionBoard.repository; | ||
|
||
import com.example.titto_backend.questionBoard.domain.Department; | ||
import com.example.titto_backend.questionBoard.domain.Question; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface QuestionRepository extends JpaRepository<Question, Long> { | ||
Page<Question> findByDepartment(Pageable pageable, Department category); | ||
} |
Oops, something went wrong.