diff --git a/backend/src/main/java/com/edufocus/edufocus/lecture/entity/Lecture.java b/backend/src/main/java/com/edufocus/edufocus/lecture/entity/Lecture.java index 5ae3972..b72e926 100644 --- a/backend/src/main/java/com/edufocus/edufocus/lecture/entity/Lecture.java +++ b/backend/src/main/java/com/edufocus/edufocus/lecture/entity/Lecture.java @@ -1,10 +1,13 @@ package com.edufocus.edufocus.lecture.entity; +import com.edufocus.edufocus.qna.entity.Qna; import jakarta.persistence.*; import lombok.Getter; import lombok.Setter; +import java.util.ArrayList; import java.util.Date; +import java.util.List; @Entity @Getter @@ -12,7 +15,8 @@ import java.util.Date; public class Lecture { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) - @Column + + @Column(name="lecture_id") private Long id; @Column(name = "teacher_id") @@ -38,5 +42,8 @@ public class Lecture { @Column private boolean online; +// +// @OneToMany(mappedBy = "lecture") +// private List qnas = new ArrayList<>(); } diff --git a/backend/src/main/java/com/edufocus/edufocus/qna/controller/QnaController.java b/backend/src/main/java/com/edufocus/edufocus/qna/controller/QnaController.java new file mode 100644 index 0000000..fb3356f --- /dev/null +++ b/backend/src/main/java/com/edufocus/edufocus/qna/controller/QnaController.java @@ -0,0 +1,83 @@ +package com.edufocus.edufocus.qna.controller; + +import com.edufocus.edufocus.qna.entity.Qna; +import com.edufocus.edufocus.qna.service.QnaService; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import okhttp3.Response; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +import java.sql.SQLException; +import java.util.List; + +@RestController +@RequestMapping("/qna") +@Slf4j +@RequiredArgsConstructor +public class QnaController { + private final QnaService qnaService; + + + @PostMapping + public ResponseEntity createQna(@RequestBody Qna qna) { + try{ + qnaService.createQna(qna); + return new ResponseEntity<>(qna, HttpStatus.CREATED); + + }catch (Exception e){ + throw new RuntimeException(e); + } + } + @PutMapping("/{id}") + public ResponseEntity updateQna(@PathVariable Long id, @RequestBody Qna qna) { + + try{ + qnaService.updateQna(id,qna); + return new ResponseEntity<>(qna, HttpStatus.ACCEPTED); + + }catch (Exception e) + { + throw new RuntimeException(e); } + } + + @DeleteMapping("/{id}") + public ResponseEntity deleteQna(@PathVariable Long id) { + try { + qnaService.deleteQna(id); + return new ResponseEntity<>(HttpStatus.ACCEPTED); + + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + + @GetMapping("/{id}") + public ResponseEntity getQna(@PathVariable Long id) { + try{ + Qna findQna= qnaService.getQna(id); + return new ResponseEntity<>(findQna, HttpStatus.ACCEPTED); + + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + + @GetMapping("/all/{id}") + public ResponseEntity> getAllQna(@PathVariable Long id) { + try { + + System.out.print("@@@@@@@@@@@@@@@@@@@@@@@>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"); + List qnaList= qnaService.getAllQnasByLecture(id); + for(Qna qna:qnaList) + { + System.out.print(qna.toString()); + } + return new ResponseEntity<>(qnaList, HttpStatus.ACCEPTED); + } catch (SQLException e) { + throw new RuntimeException(e); + + } + } +} diff --git a/backend/src/main/java/com/edufocus/edufocus/qna/entity/Qna.java b/backend/src/main/java/com/edufocus/edufocus/qna/entity/Qna.java new file mode 100644 index 0000000..ddbc743 --- /dev/null +++ b/backend/src/main/java/com/edufocus/edufocus/qna/entity/Qna.java @@ -0,0 +1,58 @@ +package com.edufocus.edufocus.qna.entity; + +import com.edufocus.edufocus.lecture.entity.Lecture; +import com.edufocus.edufocus.user.model.entity.User; +import jakarta.persistence.*; +import lombok.Getter; +import lombok.Setter; +import org.checkerframework.checker.units.qual.C; + +import java.util.Date; + +@Entity +@Getter +@Setter +public class Qna { + + // 연관관계 주인 + + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name= "qna_id") + private Long id; + + // user_id + // lecture_id + + + + @Column + private String title; + + @Column + private String content; + + + @Column(name = "created_at") + @Temporal(TemporalType.DATE) + private Date createdAt; + + @Column(name = "modified_at") + @Temporal(TemporalType.DATE) + private Date modifiedAt; + + @Column + private String answer; + + + @ManyToOne + @JoinColumn(name= "id") + private User user; + + @ManyToOne + @JoinColumn(name= "lecture_id") + private Lecture lecture; + + +} diff --git a/backend/src/main/java/com/edufocus/edufocus/qna/repository/QnaRepository.java b/backend/src/main/java/com/edufocus/edufocus/qna/repository/QnaRepository.java new file mode 100644 index 0000000..beeba54 --- /dev/null +++ b/backend/src/main/java/com/edufocus/edufocus/qna/repository/QnaRepository.java @@ -0,0 +1,18 @@ +package com.edufocus.edufocus.qna.repository; + +import com.edufocus.edufocus.qna.entity.Qna; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; +import org.springframework.stereotype.Repository; + +import java.util.List; + +@Repository +public interface QnaRepository extends JpaRepository { + + @Query(value = "SELECT * FROM qna WHERE lecture_id = :lectureId", nativeQuery = true) + List findLecture(@Param("lectureId") Long lectureId); + + +} diff --git a/backend/src/main/java/com/edufocus/edufocus/qna/service/QnaService.java b/backend/src/main/java/com/edufocus/edufocus/qna/service/QnaService.java new file mode 100644 index 0000000..a1b8d94 --- /dev/null +++ b/backend/src/main/java/com/edufocus/edufocus/qna/service/QnaService.java @@ -0,0 +1,19 @@ +package com.edufocus.edufocus.qna.service; + +import com.edufocus.edufocus.lecture.entity.Lecture; +import com.edufocus.edufocus.qna.entity.Qna; +import jakarta.transaction.Transactional; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; + +import java.sql.SQLException; +import java.util.List; +@Service +public interface QnaService { + + void createQna(Qna qna) throws SQLException; + void updateQna(Long id,Qna qna) throws SQLException; + void deleteQna(Long id) throws SQLException; + Qna getQna(Long id) throws SQLException; + List getAllQnasByLecture(Long lectureId) throws SQLException; +} diff --git a/backend/src/main/java/com/edufocus/edufocus/qna/service/QnaServiceImpl.java b/backend/src/main/java/com/edufocus/edufocus/qna/service/QnaServiceImpl.java new file mode 100644 index 0000000..18a59c1 --- /dev/null +++ b/backend/src/main/java/com/edufocus/edufocus/qna/service/QnaServiceImpl.java @@ -0,0 +1,60 @@ +package com.edufocus.edufocus.qna.service; + +import com.edufocus.edufocus.qna.entity.Qna; +import com.edufocus.edufocus.qna.repository.QnaRepository; +import jakarta.transaction.Transactional; +import lombok.RequiredArgsConstructor; +import lombok.Setter; +import org.springframework.stereotype.Service; + +import java.util.Date; +import java.util.List; +import java.util.Optional; + +@Service +@Transactional +@RequiredArgsConstructor +public class QnaServiceImpl implements QnaService{ + + private final QnaRepository qnaRepository; + + + @Override + public void createQna(Qna qna) { + + + qnaRepository.save(qna); + + } + + @Override + public void updateQna(Long id,Qna qna) { + + + Optional findQna = qnaRepository.findById(id); + + qna.setModifiedAt(new Date()); + qnaRepository.save(qna); + + + } + + @Override + public void deleteQna(Long id) { +qnaRepository.deleteById(id); + } + + @Override + public Qna getQna(Long id) { + return null; + } + + @Override + public List getAllQnasByLecture(Long lectureId) { + + + System.out.printf(lectureId+"!!!!!!!!!!!!!!!!!!!!!!"); + return qnaRepository.findLecture(lectureId); + + } +} diff --git a/backend/src/main/java/com/edufocus/edufocus/user/controller/UserController.java b/backend/src/main/java/com/edufocus/edufocus/user/controller/UserController.java index 19b306d..ee15c64 100644 --- a/backend/src/main/java/com/edufocus/edufocus/user/controller/UserController.java +++ b/backend/src/main/java/com/edufocus/edufocus/user/controller/UserController.java @@ -22,7 +22,7 @@ public class UserController { @PostMapping("/join") public ResponseEntity join(@RequestBody User user) throws Exception { - System.out.println("!!!"); + userService.join(user); return ResponseEntity.ok("User registered successfully"); } diff --git a/backend/src/main/java/com/edufocus/edufocus/user/model/entity/User.java b/backend/src/main/java/com/edufocus/edufocus/user/model/entity/User.java index 8914cd3..e8c64a9 100644 --- a/backend/src/main/java/com/edufocus/edufocus/user/model/entity/User.java +++ b/backend/src/main/java/com/edufocus/edufocus/user/model/entity/User.java @@ -1,10 +1,15 @@ package com.edufocus.edufocus.user.model.entity; +import com.edufocus.edufocus.qna.entity.Qna; import jakarta.persistence.*; import lombok.AllArgsConstructor; import lombok.Getter; import lombok.NoArgsConstructor; import lombok.Setter; +import org.checkerframework.checker.units.qual.A; + +import java.util.ArrayList; +import java.util.List; @Entity @Getter @@ -15,14 +20,19 @@ public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) // 자동 증가 설정 + @Column(name = "id") private Long id; - private String user_id; + + private String userId; private String email; private String password; @Enumerated(EnumType.STRING) // 혹은 EnumType.ORDINAL private UserRole role; +// +// @OneToMany(mappedBy = "user") +// private List qnas= new ArrayList<>(); }