Merge branch 'backend' into be/Lecture

This commit is contained in:
김기창 2024-07-17 17:53:57 +09:00 committed by GitHub
commit fd9d5b0717
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 253 additions and 2 deletions

View File

@ -1,11 +1,14 @@
package com.edufocus.edufocus.lecture.entity;
import com.edufocus.edufocus.user.model.entity.User;
import jakarta.persistence.*;
import lombok.Getter;
import lombok.Setter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Entity
@Getter
@ -13,7 +16,8 @@ import java.util.Date;
public class Lecture {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
@Column
@Column(name="lecture_id")
private Long id;
@ManyToOne
@ -41,4 +45,5 @@ public class Lecture {
private boolean online;
}

View File

@ -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<Qna> 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<Qna> 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<Qna> deleteQna(@PathVariable Long id) {
try {
qnaService.deleteQna(id);
return new ResponseEntity<>(HttpStatus.ACCEPTED);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
@GetMapping("/{id}")
public ResponseEntity<Qna> 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<List<Qna>> getAllQna(@PathVariable Long id) {
try {
System.out.print("@@@@@@@@@@@@@@@@@@@@@@@>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
List<Qna> 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);
}
}
}

View File

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

View File

@ -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<Qna, Long> {
@Query(value = "SELECT * FROM qna WHERE lecture_id = :lectureId", nativeQuery = true)
List<Qna> findLecture(@Param("lectureId") Long lectureId);
}

View File

@ -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<Qna> getAllQnasByLecture(Long lectureId) throws SQLException;
}

View File

@ -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<Qna> 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<Qna> getAllQnasByLecture(Long lectureId) {
System.out.printf(lectureId+"!!!!!!!!!!!!!!!!!!!!!!");
return qnaRepository.findLecture(lectureId);
}
}

View File

@ -22,7 +22,7 @@ public class UserController {
@PostMapping("/join")
public ResponseEntity<String> join(@RequestBody User user) throws Exception {
System.out.println("!!!");
userService.join(user);
return ResponseEntity.ok("User registered successfully");
}

View File

@ -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,8 +20,10 @@ public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY) // 자동 증가 설정
@Column(name = "id")
private Long id;
@Column(name = "user_id", unique = true, nullable = false)
private String userId;
private String email;
@ -26,4 +33,5 @@ public class User {
}