Skip to content

Commit

Permalink
Merge pull request #54 from dd-jiyun/main
Browse files Browse the repository at this point in the history
์งˆ๋ฌธ๊ฒŒ์‹œํŒ CR
  • Loading branch information
dd-jiyun authored Feb 13, 2024
2 parents 58c6009 + db5cc49 commit a2d219f
Show file tree
Hide file tree
Showing 11 changed files with 435 additions and 8 deletions.
1 change: 1 addition & 0 deletions Titto_Backend/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@ out/

### VS Code ###
.vscode/

.DS_Store
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,13 @@ public enum ErrorCode {

/* 404 NOT_FOUND : ๋ฆฌ์†Œ์Šค๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Œ */
USER_NOT_FOUND(HttpStatus.NOT_FOUND, "์‚ฌ์šฉ์ž์˜ ์ •๋ณด๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."),
USER_NOT_MATCH(HttpStatus.NOT_FOUND, "์‚ฌ์šฉ์ž์˜ ์ •๋ณด๊ฐ€ ์ผ์น˜ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค."),
EMAIL_NOT_FOUND(HttpStatus.NOT_FOUND, "์‚ฌ์šฉ์ž์˜ ์ด๋ฉ”์ผ์„ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."),
PROFILE_IMAGE_NOT_FOUND(HttpStatus.NOT_FOUND, "์‚ฌ์šฉ์ž์˜ ํ”„๋กœํ•„ ์ด๋ฏธ์ง€๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.");
PROFILE_IMAGE_NOT_FOUND(HttpStatus.NOT_FOUND, "์‚ฌ์šฉ์ž์˜ ํ”„๋กœํ•„ ์ด๋ฏธ์ง€๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."),

QUESTION_NOT_FOUND(HttpStatus.NOT_FOUND, "์งˆ๋ฌธ์„ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.");

private final HttpStatus httpStatus;
private final String message;

}
}
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
}
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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

public enum Department {
HUMANITIES, // ์ธ๋ฌธ
MANAGEMANT, // ๊ฒฝ์˜
MANAGEMENT, // ๊ฒฝ์˜
SOCIETY, // ์‚ฌํšŒ
MEDIA_CONTENT, // ๋ฏธ๋””์–ด ์ฝ˜ํ…์ธ 
FUTURE_FUSION, // ๋ฏธ๋ž˜์œตํ•ฉ
SOFEWARE // ์†Œํ”„ํŠธ์›จ์–ด
SOFTWARE // ์†Œํ”„ํŠธ์›จ์–ด
}
Original file line number Diff line number Diff line change
@@ -1,28 +1,46 @@
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.CascadeType;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Entity
@Table(name = "questionPosts")
@Builder
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class QuestionPost extends BaseEntity {
public class Question extends BaseEntity {

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

@ManyToOne
@JoinColumn(name = "author")
private User author;

@Column(name = "status")
private Status status;

@Enumerated(EnumType.STRING)
@Column(name = "department")
private Department department;
Expand All @@ -33,7 +51,14 @@ public class QuestionPost extends BaseEntity {
@Column(name = "question_content", nullable = false, columnDefinition = "TEXT")
private String content;

@Column(name = "image_url")
private String imageUrl;
//TODO: ์ด๋ฏธ์ง€, ์กฐํšŒ์ˆ˜, ๋Œ“๊ธ€์€ ๋‚˜์ค‘์— ์ถ”๊ฐ€
// @Column(name = "image_url")
// private String imageUrl;
//
// @Column(name = "view")
// private int view;
//
// @OneToMany(mappedBy = "question", cascade = CascadeType.REMOVE)
// private List<Answer> answerList;

}
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;
}
}
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> {

}
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);
}
Loading

0 comments on commit a2d219f

Please sign in to comment.