feat: Lecture

lecture 등록, 삭제 코드 수정
Quiz 엔티티 추가
This commit is contained in:
kgc9007 2024-07-17 17:37:49 +09:00
parent b00d446735
commit 9757378bd7
16 changed files with 206 additions and 33 deletions

View File

@ -1,6 +1,7 @@
package com.edufocus.edufocus.lecture.controller;
import com.edufocus.edufocus.lecture.entity.Lecture;
import com.edufocus.edufocus.lecture.entity.LectureRegist;
import com.edufocus.edufocus.lecture.service.LectureService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
@ -19,26 +20,28 @@ public class LectureController {
private final LectureService lectureService;
@PostMapping
public ResponseEntity<?> createLecture (@RequestBody Lecture lecture) {
lectureService.createLecture(lecture);
public ResponseEntity<?> createLecture (@RequestBody long userId, @RequestBody LectureRegist lectureRegist) {
lectureService.createLecture(userId, lectureRegist);
return new ResponseEntity<>(HttpStatus.CREATED);
}
@DeleteMapping("/{lectureId}")
public ResponseEntity<?> deleteLecture (@PathVariable long lectureId) {
lectureService.deleteLecture(lectureId);
public ResponseEntity<?> deleteLecture (@RequestBody long userId, @PathVariable long lectureId) {
if (!lectureService.deleteLecture(userId, lectureId)) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
@GetMapping
public ResponseEntity<?> findAllLecture () {
List<Lecture> lectures = lectureService.findAllLecture();
return new ResponseEntity<>(lectures, HttpStatus.OK);
}
@GetMapping("/teacherId/{teacherId}")
public ResponseEntity<?> findByTeacherId (@PathVariable String teacherId) {
List<Lecture> lectures = lectureService.findLectureByTeacherId(teacherId);
if (lectures.isEmpty()) {
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
return new ResponseEntity<>(lectures, HttpStatus.OK);
}
@ -53,4 +56,6 @@ public class LectureController {
return new ResponseEntity<>(lecture, HttpStatus.OK);
}
}

View File

@ -1,5 +1,6 @@
package com.edufocus.edufocus.lecture.entity;
import com.edufocus.edufocus.user.model.entity.User;
import jakarta.persistence.*;
import lombok.Getter;
import lombok.Setter;
@ -15,8 +16,9 @@ public class Lecture {
@Column
private Long id;
@Column(name = "teacher_id")
private String teacherId;
@ManyToOne
@JoinColumn(name = "user_id")
private User user;
@Column
private String title;

View File

@ -0,0 +1,27 @@
package com.edufocus.edufocus.lecture.entity;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import java.util.Date;
@Getter
@NoArgsConstructor
@AllArgsConstructor
public class LectureRegist {
private String title;
private String description;
@Temporal(TemporalType.DATE)
private Date startDate;
@Temporal(TemporalType.DATE)
private Date endDate;
private String plan;
}

View File

@ -2,6 +2,7 @@ package com.edufocus.edufocus.lecture.repository;
import com.edufocus.edufocus.lecture.entity.Lecture;
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;
@ -11,5 +12,4 @@ import java.util.List;
public interface LectureRepository extends JpaRepository<Lecture, Long> {
Lecture findByTitle(@Param("title") String title);
List<Lecture> findByTeacherId(@Param("teacherId") String teacherId);
}

View File

@ -1,8 +1,7 @@
package com.edufocus.edufocus.lecture.service;
import com.edufocus.edufocus.lecture.entity.Lecture;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import com.edufocus.edufocus.lecture.entity.LectureRegist;
import org.springframework.stereotype.Service;
import java.util.List;
@ -11,13 +10,12 @@ import java.util.List;
@Service
public interface LectureService {
void createLecture(Lecture lecture);
void createLecture(long userId, LectureRegist lectureRegist);
void deleteLecture(long lectureId);
boolean deleteLecture(long userId, long LectureId);
List<Lecture> findAllLecture();
Lecture findLectureByTitle(String title);
List<Lecture> findLectureByTeacherId(String teacherId);
List<Lecture> findAllLecture();
}

View File

@ -1,31 +1,57 @@
package com.edufocus.edufocus.lecture.service;
import com.edufocus.edufocus.lecture.entity.Lecture;
import com.edufocus.edufocus.lecture.entity.LectureRegist;
import com.edufocus.edufocus.lecture.repository.LectureRepository;
import com.edufocus.edufocus.user.model.entity.User;
import com.edufocus.edufocus.user.model.repository.UserRepository;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;
@Service
@Transactional
@RequiredArgsConstructor
public class LectureServiceImpl implements LectureService {
@Autowired
private final LectureRepository lectureRepository;
private final UserRepository userRepository;
@Override
public void createLecture(Lecture lecture) {
public void createLecture(long userId, LectureRegist lectureRegist) {
User user = userRepository.findById(userId).get();
Lecture lecture = new Lecture();
lecture.setUser(user);
lecture.setTitle(lectureRegist.getTitle());
lecture.setDescription(lectureRegist.getDescription());
lecture.setStartDate(lectureRegist.getStartDate());
lecture.setEndDate(lectureRegist.getEndDate());
lecture.setPlan(lectureRegist.getPlan());
lectureRepository.save(lecture);
}
@Override
public void deleteLecture(long lectureId) {
public boolean deleteLecture(long userId, long lectureId) {
Lecture lecture = lectureRepository.findById(lectureId).get();
if (lecture.getUser().getId() != userId) {
return false;
}
lectureRepository.deleteById(lectureId);
return true;
}
@Override
public List<Lecture> findAllLecture() {
return lectureRepository.findAll();
}
@Override
@ -33,13 +59,5 @@ public class LectureServiceImpl implements LectureService {
return lectureRepository.findByTitle(title);
}
@Override
public List<Lecture> findLectureByTeacherId(String teacherId) {
return lectureRepository.findByTeacherId(teacherId);
}
@Override
public List<Lecture> findAllLecture() {
return lectureRepository.findAll();
}
}

View File

@ -0,0 +1,4 @@
package com.edufocus.edufocus.quiz.controller;
public class QuizController {
}

View File

@ -0,0 +1,54 @@
package com.edufocus.edufocus.quiz.entity;
import jakarta.persistence.*;
import lombok.Getter;
import lombok.Setter;
@Entity
@Getter
@Setter
public class Quiz {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@ManyToOne
@JoinColumn(name = "quizset_id")
private QuizSet quizSet;
@Column
private String title;
@Column
private String description;
@Column
private String answer;
@Column (name = "is_single")
private boolean isSingle;
@Column
private String image;
@Column
private String choice1;
@Column
private String choice2;
@Column
private String choice3;
@Column
private String choice4;
public void setQuizSet(QuizSet quizSet) {
this.quizSet = quizSet;
if (!quizSet.getQuizzes().contains(this)) {
quizSet.getQuizzes().remove(this);
}
}
}

View File

@ -0,0 +1,40 @@
package com.edufocus.edufocus.quiz.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.List;
@Entity
@Getter
@Setter
public class QuizSet {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@ManyToOne
@JoinColumn(name = "user_id")
private User user;
@Column
private String title;
@Column
private String image;
@OneToMany(mappedBy = "quizSet")
private List<Quiz> quizzes = new ArrayList<Quiz>();
public void addQuiz(Quiz quiz) {
this.quizzes.add(quiz);
if (quiz.getQuizSet() != this) {
quiz.setQuizSet(this);
}
}
}

View File

@ -0,0 +1,4 @@
package com.edufocus.edufocus.quiz.repository;
public interface QuizRepository {
}

View File

@ -0,0 +1,4 @@
package com.edufocus.edufocus.quiz.repository;
public interface QuizSetRepository {
}

View File

@ -0,0 +1,4 @@
package com.edufocus.edufocus.quiz.service;
public interface QuizService {
}

View File

@ -0,0 +1,4 @@
package com.edufocus.edufocus.quiz.service;
public class QuizServiceImpl {
}

View File

@ -0,0 +1,4 @@
package com.edufocus.edufocus.quiz.service;
public interface QuizSetService {
}

View File

@ -0,0 +1,4 @@
package com.edufocus.edufocus.quiz.service;
public class QuizSetServiceImpl {
}

View File

@ -17,7 +17,8 @@ public class User {
@GeneratedValue(strategy = GenerationType.IDENTITY) // 자동 증가 설정
private Long id;
private String user_id;
@Column(name = "user_id", unique = true, nullable = false)
private String userId;
private String email;
private String password;
@Enumerated(EnumType.STRING) // 혹은 EnumType.ORDINAL