Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Q & A 프론트 코드 추가 #101

Merged
merged 4 commits into from
Nov 14, 2024
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
Expand Up @@ -36,7 +36,9 @@ SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
"/walletList",
"/qrcodes/**",
"/notice",
"/notice/**"
"/notice/**",
"/qna",
"/qna/**"
).permitAll();
// 그 외의 모든 요청은 인증 필요
auth.anyRequest().authenticated();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package com.devcard.devcard.mypage.controller.page;

import com.devcard.devcard.auth.entity.Member;
import com.devcard.devcard.auth.repository.MemberRepository;
import com.devcard.devcard.mypage.service.NoticeService;
import com.devcard.devcard.mypage.service.QnAService;
import org.springframework.security.core.Authentication;
import org.springframework.security.oauth2.core.user.OAuth2User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
Expand All @@ -9,9 +14,14 @@
@Controller
public class MyPageController {
private final NoticeService noticeService;
private final QnAService qnAService;
private final MemberRepository memberRepository;

public MyPageController(NoticeService noticeService) {
public MyPageController(NoticeService noticeService, QnAService qnAService,
MemberRepository memberRepository) {
this.noticeService = noticeService;
this.qnAService = qnAService;
this.memberRepository = memberRepository;
}

@GetMapping("/mypage/notice")
Expand All @@ -25,4 +35,34 @@ public String getNotice(@PathVariable(name = "id") Long id, Model model) {
model.addAttribute("notice", noticeService.getNotice(id));
return "notice-detail";
}

@GetMapping("/mypage/qna")
public String getQnAList(Model model) {
model.addAttribute("qnaList", qnAService.getQnAList());
return "qna-list";
}

@GetMapping("/mypage/qna/{id}")
public String getQnA(@PathVariable(name = "id") Long id, Model model) {
model.addAttribute("qna", qnAService.getQnA(id));
return "qna-detail";
}

@GetMapping("/mypage/qna/create")
public String getQnACreate(Model model, Authentication authentication) {
OAuth2User oAuth2User = (OAuth2User) authentication.getPrincipal();
String githubId = String.valueOf(oAuth2User.getAttributes().get("id"));

// GitHub ID로 사용자를 찾아서 등록 여부 확인
Member member = memberRepository.findByGithubId(githubId);

model.addAttribute("member", member);
return "qna-create-new";
}

@GetMapping("/mypage/qna/create/{id}")
public String getQnAUpdate(@PathVariable(name = "id") Long id, Model model) {
model.addAttribute("qna", qnAService.getQnA(id));
return "qna-create-update";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.devcard.devcard.mypage.controller.rest;

import com.devcard.devcard.mypage.dto.NoticeRequest;
import com.devcard.devcard.mypage.dto.NoticeResponse;
import com.devcard.devcard.mypage.dto.NoticeUpdateRequest;
import com.devcard.devcard.mypage.dto.QnAListResponse;
import com.devcard.devcard.mypage.dto.QnARequest;
import com.devcard.devcard.mypage.dto.QnAResponse;
import com.devcard.devcard.mypage.dto.QnAUpdateRequest;
import com.devcard.devcard.mypage.service.QnAService;
import java.util.List;
import org.springframework.http.ResponseEntity;
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.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/qna")
public class QnAController {

private final QnAService qnAService;

public QnAController(QnAService qnAService) {
this.qnAService = qnAService;
}

@GetMapping
public ResponseEntity<List<QnAListResponse>> getAllQnA(){
return ResponseEntity.status(201).body(qnAService.getQnAList());
}

@GetMapping("/{id}")
public ResponseEntity<QnAResponse> getQnA(@PathVariable("id") Long id){
return ResponseEntity.ok(qnAService.getQnA(id));
}

@PostMapping("")
public ResponseEntity<QnAResponse> addQnA(@RequestBody QnARequest qnARequest){
return ResponseEntity.ok(qnAService.addQnA(qnARequest));
}

@PutMapping("")
public ResponseEntity<QnAResponse> updateQnA(@RequestBody QnAUpdateRequest qnAUpdateRequest){
return ResponseEntity.ok(qnAService.updateQnA(qnAUpdateRequest));
}

@DeleteMapping("/{id}")
public ResponseEntity<QnAResponse> deleteQnA(@PathVariable("id") Long id){
return ResponseEntity.ok(qnAService.deleteQnA(id));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.devcard.devcard.mypage.dto;

public record QnAAnswerDTO(Long id, String answer) {

}
11 changes: 11 additions & 0 deletions src/main/java/com/devcard/devcard/mypage/dto/QnAListResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.devcard.devcard.mypage.dto;

import java.time.LocalDateTime;

public record QnAListResponse(Long id,
String name,
String questionTitle,
boolean answerCompleted,
LocalDateTime questionTimestamp) {

}
5 changes: 5 additions & 0 deletions src/main/java/com/devcard/devcard/mypage/dto/QnARequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.devcard.devcard.mypage.dto;

public record QnARequest(String name, String questionTitle, String questionContent) {

}
13 changes: 13 additions & 0 deletions src/main/java/com/devcard/devcard/mypage/dto/QnAResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.devcard.devcard.mypage.dto;

import java.time.LocalDateTime;

public record QnAResponse(Long id,
String name,
String questionTitle,
String questionContent,
String answer,
LocalDateTime questionTimestamp,
LocalDateTime answerTimestamp,
boolean answerCompleted) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.devcard.devcard.mypage.dto;

public record QnAUpdateRequest(Long id, String questionTitle, String questionContent) {

}
84 changes: 84 additions & 0 deletions src/main/java/com/devcard/devcard/mypage/entity/QnA.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package com.devcard.devcard.mypage.entity;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import java.time.LocalDateTime;

@Entity(name = "qna")
public class QnA {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

private String name;

private String questionTitle;

private String questionContent;

private String answer;

private LocalDateTime questionTimestamp;

private LocalDateTime answerTimestamp;

private boolean answerCompleted;

public QnA(){

}

public QnA(String name, String questionTitle, String questionContent, LocalDateTime questionTimestamp) {
this.name = name;
this.questionTitle = questionTitle;
this.questionContent = questionContent;
this.questionTimestamp = questionTimestamp;
this.answerCompleted = false;
}

public Long getId() {
return id;
}

public String getName() {
return name;
}

public String getQuestionTitle() {
return questionTitle;
}

public String getQuestionContent() {
return questionContent;
}

public String getAnswer() {
return answer;
}

public LocalDateTime getQuestionTimestamp() {
return questionTimestamp;
}

public LocalDateTime getAnswerTimestamp() {
return answerTimestamp;
}

public boolean isAnswerCompleted() {
return answerCompleted;
}

public void updateByRequest(String updateQuestionTitle, String updateQuestionContent){
this.questionTitle = updateQuestionTitle;
this.questionContent = updateQuestionContent;
}

public void updateAnswer(String updateAnswer){
this.answer = updateAnswer;
this.answerTimestamp = LocalDateTime.now();
this.answerCompleted = true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.devcard.devcard.mypage.repository;

import com.devcard.devcard.mypage.entity.QnA;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;

public interface QnARepository extends JpaRepository<QnA, Long> {
List<QnA> findAllByOrderByQuestionTimestampDesc();

QnA findQnAById(Long id);
}
62 changes: 62 additions & 0 deletions src/main/java/com/devcard/devcard/mypage/service/QnAService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.devcard.devcard.mypage.service;

import com.devcard.devcard.mypage.dto.QnAAnswerDTO;
import com.devcard.devcard.mypage.dto.QnAListResponse;
import com.devcard.devcard.mypage.dto.QnARequest;
import com.devcard.devcard.mypage.dto.QnAResponse;
import com.devcard.devcard.mypage.dto.QnAUpdateRequest;
import com.devcard.devcard.mypage.entity.QnA;
import com.devcard.devcard.mypage.repository.QnARepository;
import java.time.LocalDateTime;
import java.util.List;
import org.springframework.stereotype.Service;

@Service
public class QnAService {
private final QnARepository qnARepository;

public QnAService(QnARepository qnARepository) {
this.qnARepository = qnARepository;
}
public List<QnAListResponse> getQnAList() {
List<QnA> qnaList = qnARepository.findAllByOrderByQuestionTimestampDesc();
return qnaList.stream().map(qna -> new QnAListResponse(
qna.getId(),
qna.getName(),
qna.getQuestionTitle(),
qna.isAnswerCompleted(),
qna.getQuestionTimestamp()
)).toList();
}

public QnAResponse getQnA(Long id) {
QnA qna = qnARepository.findQnAById(id);
return new QnAResponse(qna.getId(), qna.getName(), qna.getQuestionTitle(), qna.getQuestionContent(), qna.getAnswer(), qna.getQuestionTimestamp(), qna.getAnswerTimestamp(), qna.isAnswerCompleted());
}

public QnAResponse addQnA(QnARequest qnARequest) {
QnA qna = qnARepository.save(new QnA(qnARequest.name(), qnARequest.questionTitle(), qnARequest.questionContent(),
LocalDateTime.now()));
return new QnAResponse(qna.getId(), qna.getName(), qna.getQuestionTitle(), qna.getQuestionContent(), qna.getAnswer(), qna.getQuestionTimestamp(), qna.getAnswerTimestamp(), qna.isAnswerCompleted());
}

public QnAResponse updateQnA(QnAUpdateRequest qnAUpdateRequest) {
QnA qna = qnARepository.findQnAById(qnAUpdateRequest.id());
qna.updateByRequest(qnAUpdateRequest.questionTitle(), qnAUpdateRequest.questionContent());
qnARepository.save(qna);
return new QnAResponse(qna.getId(), qna.getName(), qna.getQuestionTitle(), qna.getQuestionContent(), qna.getAnswer(), qna.getQuestionTimestamp(), qna.getAnswerTimestamp(), qna.isAnswerCompleted());
}

public QnAResponse deleteQnA(Long id) {
QnA qna = qnARepository.findQnAById(id);
qnARepository.delete(qna);
return new QnAResponse(qna.getId(), qna.getName(), qna.getQuestionTitle(), qna.getQuestionContent(), qna.getAnswer(), qna.getQuestionTimestamp(), qna.getAnswerTimestamp(), qna.isAnswerCompleted());
}

public QnAResponse updateAnswer(QnAAnswerDTO qnAAnswerDTO) {
QnA qna = qnARepository.findQnAById(qnAAnswerDTO.id());
qna.updateAnswer(qnAAnswerDTO.answer());
return new QnAResponse(qna.getId(), qna.getName(), qna.getQuestionTitle(), qna.getQuestionContent(), qna.getAnswer(), qna.getQuestionTimestamp(), qna.getAnswerTimestamp(), qna.isAnswerCompleted());
}

}
35 changes: 35 additions & 0 deletions src/main/resources/static/css/mypage/qna-common.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
:root {
--cnu-light-blue: #3f8efc;
--text-color-dark: #333;
--text-color-light: #888;
--background-color: #f9f9f9;
--border-radius: 8px;
--box-shadow: 0px 4px 8px rgba(0, 0, 0, 0.1);
}

body {
margin: 0;
font-family: Arial, sans-serif;
color: var(--text-color-dark);
background-color: var(--background-color);
}

/* 공통 헤더 스타일 */
.header {
display: flex;
align-items: center;
font-size: 1.5rem;
padding: 1rem;
color: white;
background-color: var(--cnu-light-blue);
gap: 0.5rem;
}

.header #backButton {
cursor: pointer;
font-size: 1.5rem;
}

.header .title {
margin-left: 1.5rem;
}
Loading
Loading